Also Like

Module 8: Positioning and Layout

Module 8: Positioning and Layout

CSS Positioning and Layout

Master the art of controlling element positioning and creating responsive layouts

Learning Outcomes

  • Understand and apply different position values: static, relative, fixed, absolute, sticky
  • Create responsive website layouts that adapt to different screen sizes
  • Implement advanced layouts using Flexbox and CSS Grid
  • Control element stacking with z-index
  • Apply modern layout techniques for real-world web applications

CSS Positioning

CSS positioning allows you to control exactly where elements are placed on a web page and how they interact with other elements.

Position Property Values

The position property specifies the type of positioning method used for an element. There are five different position values:

Static (default)

.element {
    position: static;
}

Elements render in order as they appear in the document flow.

1
2

Relative

.element {
    position: relative;
    top: 20px;
    left: 30px;
}

Positioned relative to its normal position. Other elements aren't affected.

1
2

Absolute

.element {
    position: absolute;
    top: 50px;
    right: 20px;
}

Positioned relative to its nearest positioned ancestor (not static).

1
2

Fixed

.element {
    position: fixed;
    bottom: 0;
    right: 0;
}

Positioned relative to the viewport. Stays in place during scrolling.

1
2

Sticky

.element {
    position: sticky;
    top: 0;
}

Toggles between relative and fixed based on scroll position.

Scroll down...
Sticky
Content...

Z-Index Property

The z-index property specifies the stack order of elements (which element appears in front).

.element1 {
    position: absolute;
    z-index: 1;
}

.element2 {
    position: absolute;
    z-index: 2; /* Appears above element1 */
}
1
2
3

Layout Techniques

Modern CSS provides powerful layout systems to create complex, responsive designs.

Flexbox Layout

Flexbox is a one-dimensional layout method for arranging items in rows or columns.

.container {
    display: flex;
    justify-content: center; /* Main axis alignment */
    align-items: center; /* Cross axis alignment */
    flex-wrap: wrap; /* Allow items to wrap */
}

.item {
    flex: 1; /* Grow and shrink as needed */
}
Item 1
Item 2
Item 3
Item A
Item B

CSS Grid Layout

CSS Grid is a two-dimensional layout system that handles both rows and columns.

.container {
    display: grid;
    grid-template-columns: 1fr 2fr 1fr;
    grid-template-rows: 100px auto;
    gap: 10px;
}

.item {
    grid-column: 1 / 3; /* Span columns */
    grid-row: 1; /* Position in row */
}
Header
Main
Sidebar
Footer

Responsive Layout Techniques

Create layouts that adapt to different screen sizes using media queries and flexible units.

/* Mobile-first approach */
.container {
    padding: 10px;
}

/* Tablet */
@media (min-width: 768px) {
    .container {
        display: grid;
        grid-template-columns: 1fr 2fr;
    }
}

/* Desktop */
@media (min-width: 1024px) {
    .container {
        grid-template-columns: 1fr 3fr 1fr;
    }
}
Responsive
Grid
Items

Interactive Playground

Experiment with different positioning and layout techniques:

Code Editor
Live Preview

Knowledge Check

1. Which position value takes the element out of the normal document flow but positions it relative to its closest positioned ancestor?

2. What CSS property would you use to create a layout with items that automatically resize to fill available space in a single row?

3. Which position value would you use to create a navigation bar that stays at the top of the viewport while scrolling?

4. In CSS Grid, what unit represents a fraction of the available space?

5. Which Flexbox property controls the alignment of items along the main axis?

6. What is the default value of the position property in CSS?

7. Which property controls the stacking order of positioned elements?

8. Which CSS Grid property defines the size of the columns?

9. What Flexbox property would you use to change the direction of the main axis?

10. Which position value would you use to create a tooltip that appears near a button when hovered?

© 2023 CSS Mastery Series | Designed for Web Developers