Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
CSS Layouts: Flexbox, Grid & Responsive
  1. 1.Introduction to CSS & Selectors
  2. 2.The CSS Box Model & Basic Styling
  3. 3.Display Property & Positioning Fundamentals
  4. 4.Units, Values & Responsive Basics
  5. 5.Flexbox Fundamentals
  6. 6.Advanced Flexbox & Real-world Patterns
  7. 7.CSS Grid Fundamentals
  8. 8.Advanced CSS Grid & Area Layouts
  9. 9.Responsive Design Strategies & Mobile First
  10. 10.CSS Transitions, Transforms & Animations
  11. 11.Advanced CSS Concepts & Performance
  12. 12.Layout Techniques for Complex UIs & Interview Prep
CSS Layouts: Flexbox, Grid & Responsive
03

CSS · Lesson 3 of 12 · 4 min

Display Property & Positioning Fundamentals

This is bread-and-butter CSS. Expect to explain the difference between relative and absolute positioning blindfolded.

After this, you'll know how elements are placed on a page by default, and how to override that placement for specific layouts like tooltips, modals, and sticky headers. This is non-negotiable knowledge for any frontend role.

Control Flow and Sizing with display

The display property sets an element's outer display type, which determines how it participates in the layout flow. You primarily need to know three values by heart: block, inline, and inline-block.

block elements (div, p, h1) start on a new line and expand to fill the available width. You can control their width, height, and margin.

inline elements (span, a, strong) flow with the text, do not start a new line, and only take up as much width as their content. width and height properties are ignored. Vertical margin has no effect on surrounding elements.

inline-block is the hybrid. It flows like an inline element (doesn't force a new line) but behaves like a block element regarding sizing (respects width, height, and full margin/padding). It's useful for things like navigation buttons that need to sit side-by-side but have specific dimensions.

In an interview, when asked to differentiate these, the key points are: 1) line-breaking behavior and 2) respect for width, height, and vertical margin. Mentioning all three shows you understand the practical implications.

Escape the Normal Flow with position

While display handles the flow, position lets you make exceptions. Every element starts as position: static, meaning it just follows the normal document flow. The other values let you override this.

position: relative is the first step. It shifts an element from its static position using top, left, bottom, and right. Crucially, the space it would have occupied remains empty. Other elements don't collapse into its old spot.

The trap: While you can use relative to nudge elements, its primary job in modern layouts is to create a "positioning context" for child elements.

position: absolute pulls an element completely out of the document flow. Other elements behave as if it isn't there. It is then positioned relative to its nearest positioned ancestor—an ancestor with a position value other than static. If none exists, it falls back to the initial containing block (usually the <html> or <body>).

This relative parent / absolute child pattern is fundamental for creating overlays, tooltips, or badges.

Position Relative to the Viewport

Two position values are special because they relate to the browser window, not the document flow.

position: fixed also removes an element from the document flow. It positions the element relative to the viewport (the visible part of the browser window). It does not move when the user scrolls. This is for sticky headers, footers, or modal dialogs.

position: sticky is a hybrid. An element is treated as relative until it hits a specified threshold (e.g., top: 0) during a scroll. Then, it becomes "stuck" and behaves like fixed until its parent element scrolls out of view.

The trap: position: sticky will not work if any of its parent elements have overflow: hidden, overflow: scroll, or overflow: auto. The element can only be sticky within the scrolling context of its nearest ancestor. This is a common "gotcha" that stumps developers.

Manage Stacking with z-index

When elements are positioned, they can overlap. The z-index property controls which one appears on top. A higher value means it's closer to the viewer.

The trap: z-index only works on positioned elements (anything not static). Applying it to a static element does nothing.

The real complexity comes from stacking contexts. A positioned element with a z-index value creates a new stacking context. Inside this context, child z-index values are only compared against each other. An element with z-index: 1000 inside a parent with z-index: 1 will still appear behind a sibling of that parent with z-index: 2. Understanding this is a sign of a seasoned developer.

The relative/absolute Pattern in Practice

This is the most common and important positioning pattern to master. A relative container provides a bounding box for an absolute child, allowing you to place it precisely without it affecting the layout of its siblings.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Relative & Absolute Positioning</title>
    <style>
        .card {
            /* Creates the positioning context for the badge. */
            position: relative;
            width: 300px;
            height: 200px;
            background-color: #f0f0f0;
            border: 1px solid #ccc;
            padding: 16px;
        }

        .badge {
            /* Taken out of flow, positioned relative to .card */
            position: absolute;
            top: -10px;   /* Positioned relative to the top edge of .card */
            right: -10px; /* Positioned relative to the right edge of .card */
            background-color: red;
            color: white;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 14px;
        }

        .card p {
            /* This text flows normally, ignoring the badge. */
            margin: 0;
        }
    </style>
</head>
<body>
    <div class="card">
        <div class="badge">New!</div>
        <p>This is the content of the card. The badge is positioned in the corner and does not push this text around.</p>
    </div>
</body>
</html>

Without position: relative on .card, the .badge would be positioned relative to the <body>, ending up in the top-right corner of the page.

Try it

Build a card component with an image. Add a semi-transparent caption overlay that sits flush with the bottom of the image. The card container must be the positioning context for the caption. This should take you 15 minutes.

Next: We'll cover Units, Values & Responsive Basics.

Lock it in — apply this lesson under interview conditions.

Try an AI mock interview
Previous

The CSS Box Model & Basic Styling

Next

Units, Values & Responsive Basics