CodeGym /Courses /Frontend SELF EN /Document Flow

Document Flow

Frontend SELF EN
Level 22 , Lesson 2
Available

8.1 Normal Flow

Positioning elements in CSS is key to managing their layout and interaction on a web page. It determines how elements are placed relative to each other and to their container. Let's explore how different types of positioning affect element flow.

Document Flow and Positioning

Elements in the normal document flow are positioned one after the other in the order dictated by the HTML markup. Block elements (like <div>, <p>, <h1>) are laid out vertically, taking up the full width of their parent container, while inline elements (like <span>, <a>, <em>) are placed horizontally, using only the necessary space.

Example:

CSS
    
      .block {
        background-color: lightblue;
        margin: 10px;
        padding: 10px;
      }

      .inline {
        background-color: lightcoral;
        margin: 5px;
        padding: 5px;
      }
    
  
HTML
    
      <div class="block">Block element</div>
      <span class="inline">Inline element</span>
      <span class="inline">Inline element</span>
    
  

8.2 Impact of Positioning on Document Flow

Different types of positioning can change the standard document flow. Let's see how this works:

Static Positioning

The static positioning is the default value and it doesn't change the normal document flow. Elements with position: static are placed in the order dictated by the HTML markup.

Impact on Flow:

  • Elements stay in the normal flow
  • Elements are positioned one after the other according to their order in HTML

Relative Positioning

Elements with position: relative stay in the normal flow but can be offset from their normal position using the top, right, bottom, left properties. The offset doesn't affect the position of other elements.

Impact on Flow:

  • Elements stay in the normal flow
  • Offsetting an element doesn't change the placement of neighboring elements

Absolute Positioning

Elements with position: absolute are removed from the normal document flow and are placed relative to their closest positioned ancestor. If no such ancestor exists, they're placed relative to the initial container (usually <html> or <body>).

Impact on Flow:

  • Elements are removed from the normal flow
  • Positioned relative to the nearest positioned ancestor
  • Other elements act as if these elements are not there

Fixed Positioning

Elements with position: fixed are removed from the normal flow and are positioned relative to the browser window. They remain in place during page scroll.

Impact on Flow:

  • Elements are removed from the normal flow
  • Positioned relative to the browser window
  • Stay in place during page scroll
  • Other elements act as if these elements are not there

Sticky Positioning

Elements with position: sticky act like relatively positioned until a given scroll threshold is reached, then they become fixed.

Impact on Flow:

  • Elements stay in the normal flow until the scroll threshold is reached
  • Once the threshold is hit, the element becomes fixed and no longer affects the normal flow

8.3 Visual Examples of Impact on Flow

1. Example with Fixed Positioning:

CSS
    
      .wrapper {
        height: 200px;
        overflow: auto;
      }
      .fixed {
        position: fixed;
        top: 10px;
        right: 10px;
        background-color: lightcoral;
        padding: 10px;
      }

      .content {
        background: linear-gradient(white, lightgray);
      }
    
  
HTML
    
      <div class="wrapper">
        <div class="fixed">Fixed element</div>
        <div class="content">Scrollable content
          Suspendisse tellus sem, sollicitudin ac sapien vitae, consectetur molestie nunc. Suspendisse gravida efficitur est, quis sagittis lectus tincidunt ut. Vestibulum ut tortor vel ligula laoreet fermentum ut quis orci. Nulla facilisi. Cras a vehicula ante. Nullam auctor magna sed justo fringilla condimentum. Aenean lacinia mauris ac neque rhoncus iaculis. Sed iaculis mattis ipsum sed facilisis. Ut non elit mi. Vestibulum tempus lectus eget turpis bibendum, id venenatis urna eleifend.
          Fusce at augue at tellus vehicula condimentum quis vitae massa. Vivamus faucibus lectus ut diam pharetra sollicitudin. Sed eu pretium lectus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Sed vitae venenatis purus. Nullam sed porttitor elit. Praesent condimentum sem sit amet odio tempus, at finibus ante feugiat.
          Morbi fringilla magna vitae tellus ultrices convallis. Donec pharetra mollis velit et elementum. Ut aliquam odio quis pellentesque eleifend. Nullam vel auctor magna. Sed mollis vitae odio non bibendum. Suspendisse ornare, tellus vitae vehicula mattis, justo lorem ullamcorper orci, elementum pretium augue justo ut ante. Nulla volutpat finibus porta.
          Suspendisse tellus sem, sollicitudin ac sapien vitae, consectetur molestie nunc. Suspendisse gravida efficitur est, quis sagittis lectus tincidunt ut. Vestibulum ut tortor vel ligula laoreet fermentum ut quis orci. Nulla facilisi. Cras a vehicula ante. Nullam auctor magna sed justo fringilla condimentum. Aenean lacinia mauris ac neque rhoncus iaculis. Sed iaculis mattis ipsum sed facilisis. Ut non elit mi. Vestibulum tempus lectus eget turpis bibendum, id venenatis urna eleifend.
          Fusce at augue at tellus vehicula condimentum quis vitae massa. Vivamus faucibus lectus ut diam pharetra sollicitudin. Sed eu pretium lectus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Sed vitae venenatis purus. Nullam sed porttitor elit. Praesent condimentum sem sit amet odio tempus, at finibus ante feugiat.
          Morbi fringilla magna vitae tellus ultrices convallis. Donec pharetra mollis velit et elementum. Ut aliquam odio quis pellentesque eleifend. Nullam vel auctor magna. Sed mollis vitae odio non bibendum. Suspendisse ornare, tellus vitae vehicula mattis, justo lorem ullamcorper orci, elementum pretium augue justo ut ante. Nulla volutpat finibus porta.
        </div>
      </div>
    
  

2. Example with Sticky Positioning:

CSS
    
      .wrapper {
        height: 200px;
        overflow: auto;
      }

      .sticky {
        position: sticky;
        top: 0;
        background-color: lightgreen;
        padding: 10px;
      }

      .content {
        background: linear-gradient(white, lightgray);
      }
    
  
HTML
    
      <div class="wrapper">
        <div class="sticky">Sticky element</div>
        <div class="content">
          Suspendisse tellus sem, sollicitudin ac sapien vitae, consectetur molestie nunc. Suspendisse gravida efficitur est, quis sagittis lectus tincidunt ut. Vestibulum ut tortor vel ligula laoreet fermentum ut quis orci. Nulla facilisi. Cras a vehicula ante. Nullam auctor magna sed justo fringilla condimentum. Aenean lacinia mauris ac neque rhoncus iaculis. Sed iaculis mattis ipsum sed facilisis. Ut non elit mi. Vestibulum tempus lectus eget turpis bibendum, id venenatis urna eleifend.
          Fusce at augue at tellus vehicula condimentum quis vitae massa. Vivamus faucibus lectus ut diam pharetra sollicitudin. Sed eu pretium lectus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Sed vitae venenatis purus. Nullam sed porttitor elit. Praesent condimentum sem sit amet odio tempus, at finibus ante feugiat.
          Morbi fringilla magna vitae tellus ultrices convallis. Donec pharetra mollis velit et elementum. Ut aliquam odio quis pellentesque eleifend. Nullam vel auctor magna. Sed mollis vitae odio non bibendum. Suspendisse ornare, tellus vitae vehicula mattis, justo lorem ullamcorper orci, elementum pretium augue justo ut ante. Nulla volutpat finibus porta.
          Suspendisse tellus sem, sollicitudin ac sapien vitae, consectetur molestie nunc. Suspendisse gravida efficitur est, quis sagittis lectus tincidunt ut. Vestibulum ut tortor vel ligula laoreet fermentum ut quis orci. Nulla facilisi. Cras a vehicula ante. Nullam auctor magna sed justo fringilla condimentum. Aenean lacinia mauris ac neque rhoncus iaculis. Sed iaculis mattis ipsum sed facilisis. Ut non elit mi. Vestibulum tempus lectus eget turpis bibendum, id venenatis urna eleifend.
          Fusce at augue at tellus vehicula condimentum quis vitae massa. Vivamus faucibus lectus ut diam pharetra sollicitudin. Sed eu pretium lectus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Sed vitae venenatis purus. Nullam sed porttitor elit. Praesent condimentum sem sit amet odio tempus, at finibus ante feugiat.
          Morbi fringilla magna vitae tellus ultrices convallis. Donec pharetra mollis velit et elementum. Ut aliquam odio quis pellentesque eleifend. Nullam vel auctor magna. Sed mollis vitae odio non bibendum. Suspendisse ornare, tellus vitae vehicula mattis, justo lorem ullamcorper orci, elementum pretium augue justo ut ante. Nulla volutpat finibus porta.
        </div>
      </div>
    
  
1
Task
Frontend SELF EN, level 22, lesson 2
Locked
Creating a Block Element
Creating a Block Element
1
Task
Frontend SELF EN, level 22, lesson 2
Locked
Creating Inline Elements
Creating Inline Elements
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION