Create Fixed Positioning Layout in CSS

CSSCSSBeginner
Practice Now

Introduction

In this lab, you will learn how to create a fixed positioning layout using CSS, a powerful technique for maintaining element placement on a web page. The lab will guide you through understanding the concept of fixed positioning, setting up an HTML project structure, and applying fixed positioning to side advertisements while exploring how elements can remain stationary during page scrolling.

By following the step-by-step instructions, you will gain practical skills in using CSS positioning properties like position: fixed, and learn how to precisely control element placement using top, bottom, left, and right properties. The lab provides hands-on experience in creating responsive and visually consistent layouts that maintain their position regardless of page scrolling.

Understand Fixed Positioning Concept

In this step, you'll learn about fixed positioning in CSS, a powerful layout technique that allows elements to remain in a fixed position on the screen, regardless of scrolling.

Fixed positioning is a CSS positioning method that makes an element stay in the exact same place on the screen, even when the page is scrolled. Unlike other positioning methods, fixed elements do not move when the user scrolls the page.

Let's explore the basic concept with a simple example:

<!doctype html>
<html>
  <head>
    <style>
      .fixed-element {
        position: fixed;
        top: 20px;
        right: 20px;
        background-color: #f1f1f1;
        padding: 10px;
        border: 1px solid #ddd;
      }
    </style>
  </head>
  <body>
    <div class="fixed-element">I'm a fixed element that stays in place!</div>
    <div style="height: 2000px;">
      Scroll down to see the fixed element remain in position
    </div>
  </body>
</html>

Key characteristics of fixed positioning:

  • Uses position: fixed; in CSS
  • Positioned relative to the browser window
  • Does not move when page is scrolled
  • Can be precisely placed using top, bottom, left, and right properties

Example output when viewing in a browser:

  • The .fixed-element will always remain 20 pixels from the top and right of the screen
  • Even if you scroll down, this element stays in the exact same screen position

Set Up HTML Project Structure

In this step, you'll create the project structure for our fixed positioning layout demonstration. We'll set up a simple HTML project in the ~/project directory that will serve as the foundation for our CSS positioning exercise.

First, navigate to the project directory and create the necessary files:

cd ~/project
mkdir fixed-positioning-demo
cd fixed-positioning-demo

Now, create the basic HTML file structure:

touch index.html
touch styles.css

Open the index.html file and add the following basic HTML structure:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Fixed Positioning Layout Demo</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <div class="content">
      <h1>Fixed Positioning Layout</h1>
      <p>This is a sample page to demonstrate fixed positioning in CSS.</p>

      <!-- We'll add more content in subsequent steps -->
    </div>
  </body>
</html>

Create a basic CSS file styles.css with some initial styling:

body {
  font-family: Arial, sans-serif;
  line-height: 1.6;
  margin: 0;
  padding: 0;
}

.content {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

Example output structure:

~/project/fixed-positioning-demo/
├── index.html
└── styles.css

This setup provides a clean, simple project structure that we'll build upon in the following steps to demonstrate fixed positioning techniques.

Apply Fixed Positioning to Side Advertisements

In this step, you'll learn how to create fixed-position side advertisements using CSS. We'll modify the index.html and styles.css files to add side advertisement elements that remain in place while scrolling.

Update the index.html file to include side advertisement elements:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Fixed Positioning Layout Demo</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <div class="left-ad">
      <h3>Left Side Ad</h3>
      <p>This ad stays in place!</p>
    </div>

    <div class="right-ad">
      <h3>Right Side Ad</h3>
      <p>Another fixed advertisement</p>
    </div>

    <div class="content">
      <h1>Fixed Positioning Layout</h1>
      <p>Scroll down to see how the side advertisements remain fixed.</p>

      <!-- Add more content to create scrolling effect -->
      <div style="height: 2000px;">Long content to demonstrate scrolling</div>
    </div>
  </body>
</html>

Modify the styles.css file to apply fixed positioning to the advertisements:

body {
  font-family: Arial, sans-serif;
  line-height: 1.6;
  margin: 0;
  padding: 0;
}

.content {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.left-ad,
.right-ad {
  position: fixed;
  width: 150px;
  background-color: #f4f4f4;
  padding: 10px;
  border: 1px solid #ddd;
}

.left-ad {
  left: 20px;
  top: 50%;
  transform: translateY(-50%);
}

.right-ad {
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
}

Key points about the fixed positioning:

  • position: fixed; keeps the ads in a constant position
  • left and right properties position the ads on the screen sides
  • top: 50% and transform: translateY(-50%) center the ads vertically

Example output in browser:

  • Two side advertisements will appear fixed on the screen
  • Advertisements remain in place when scrolling the page
  • Ads are centered vertically on the screen

Adjust Element Placement with Top and Right Properties

In this step, you'll learn how to precisely control the placement of fixed-position elements using top, right, bottom, and left properties. These CSS properties allow you to position fixed elements exactly where you want them on the screen.

Update the styles.css file to demonstrate different positioning techniques:

/* Previous styles remain the same */

.left-ad {
  position: fixed;
  top: 20px; /* 20 pixels from the top of the screen */
  left: 20px; /* 20 pixels from the left of the screen */
  width: 150px;
  background-color: #f4f4f4;
  padding: 10px;
  border: 1px solid #ddd;
}

.right-ad {
  position: fixed;
  top: 50%; /* Vertically centered */
  right: 20px; /* 20 pixels from the right of the screen */
  width: 150px;
  transform: translateY(-50%); /* Adjust for perfect centering */
  background-color: #e4e4e4;
  padding: 10px;
  border: 1px solid #ccc;
}

.bottom-ad {
  position: fixed;
  bottom: 20px; /* 20 pixels from the bottom of the screen */
  left: 50%; /* Horizontally centered */
  width: 300px;
  transform: translateX(-50%); /* Adjust for perfect centering */
  background-color: #d4d4d4;
  padding: 10px;
  text-align: center;
  border: 1px solid #bbb;
}

Update the index.html to include the new bottom advertisement:

<body>
  <div class="left-ad">
    <h3>Left Side Ad</h3>
    <p>Positioned 20px from top-left</p>
  </div>

  <div class="right-ad">
    <h3>Right Side Ad</h3>
    <p>Vertically centered</p>
  </div>

  <div class="bottom-ad">
    <h3>Bottom Ad</h3>
    <p>Centered at the bottom of the screen</p>
  </div>

  <div class="content">
    <h1>Fixed Positioning Layout</h1>
    <p>Explore different positioning techniques</p>

    <div style="height: 2000px;">Scroll to see fixed elements</div>
  </div>
</body>

Key positioning properties explained:

  • top: Distance from the top of the screen
  • right: Distance from the right of the screen
  • bottom: Distance from the bottom of the screen
  • left: Distance from the left of the screen
  • transform: Fine-tune positioning for perfect centering

Example output in browser:

  • Left advertisement: 20 pixels from top-left corner
  • Right advertisement: Vertically centered on the right side
  • Bottom advertisement: Centered at the bottom of the screen
  • All elements remain fixed during scrolling

Test Scrolling Behavior of Fixed Elements

In this step, you'll explore the unique scrolling behavior of fixed-position elements by adding more content to demonstrate how these elements remain stationary while the page scrolls.

Update the index.html file to include more content for scrolling:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Fixed Positioning Scroll Test</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <div class="left-ad">
      <h3>Left Ad</h3>
      <p>I stay in place!</p>
    </div>

    <div class="right-ad">
      <h3>Right Ad</h3>
      <p>Always visible</p>
    </div>

    <div class="bottom-ad">
      <h3>Bottom Ad</h3>
      <p>Fixed at bottom</p>
    </div>

    <div class="content">
      <h1>Scrolling Behavior Demonstration</h1>

      <div class="scroll-content">
        <h2>Scroll Down to See Fixed Elements</h2>

        <!-- Create long content to enable scrolling -->
        <div class="long-content">
          <h3>Long Content Section</h3>
          <p>
            This section will allow you to scroll and observe how fixed-position
            elements remain in place.
          </p>

          <!-- Repeat paragraphs to create scrollable content -->
          <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam in
            dui mauris. Vivamus hendrerit arcu sed erat molestie vehicula.
          </p>

          <!-- Add multiple repetitions to create significant scroll length -->
          <p>
            Sed auctor neque eu tellus rhoncus ut eleifend nibh porttitor. Ut in
            nulla enim. Phasellus molestie magna non est bibendum non venenatis
            nisl tempor.
          </p>

          <!-- Repeat similar paragraphs multiple times -->
          <p>
            Suspendisse dictum feugiat nisl ut dapibus. Mauris iaculis porttitor
            posuere. Praesent id metus massa, ut blandit odio.
          </p>

          <!-- Continue adding paragraphs to create scroll effect -->
          <p>
            Proin quis tortor orci. Etiam at risus et justo dignissim congue.
            Donec congue lacinia dui, a porttitor lectus condimentum laoreet.
          </p>

          <!-- More paragraphs to extend scrolling -->
          <p>
            Nunc dignissim risus id metus. Cras ornare tristique elit. Vivamus
            vestibulum nulla sit amet tristique reprehenderit.
          </p>
        </div>
      </div>
    </div>
  </body>
</html>

Update the styles.css to enhance the scrolling demonstration:

body {
  font-family: Arial, sans-serif;
  line-height: 1.6;
  margin: 0;
  padding: 0;
}

.content {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.long-content {
  background-color: #f9f9f9;
  padding: 20px;
}

/* Previous fixed positioning styles remain the same */

Key observations about fixed positioning:

  • Fixed elements stay in the same screen position
  • Scrolling does not affect their placement
  • Elements remain visible regardless of page scroll position

Example scrolling behavior:

  • Left, right, and bottom advertisements remain stationary
  • Content scrolls behind fixed elements
  • Fixed elements are always visible on the screen

Summary

In this lab, participants learned about fixed positioning in CSS, a powerful layout technique that allows elements to remain stationary on the screen regardless of page scrolling. By using the position: fixed property, developers can create elements that stay in a consistent location, such as side advertisements or navigation menus, which remain visible even when users scroll through content.

The lab guided learners through creating a practical example demonstrating fixed positioning, including setting up an HTML project structure and applying CSS properties like top and right to precisely control element placement. Participants explored key characteristics of fixed positioning, such as its reference to the browser window and its ability to maintain a constant screen position, providing hands-on experience with this essential CSS layout technique.

Other CSS Tutorials you may like