Week 05: CSS Layout & Positioning

Unit II - Mastering Page Layouts

From chaos to order - controlling where things go!

What You'll Learn

  • Display property and its values
  • Position property (static, relative, absolute, fixed, sticky)
  • Flexbox - modern 1D layouts
  • CSS Grid - powerful 2D layouts

The City Planning Analogy

If HTML elements are buildings, CSS layout is city planning - deciding where buildings go, how streets flow, and creating organized neighborhoods from chaos!

Press → or click Next to continue

The Display Property

Every element has a default display value. Understanding display is fundamental to layouts!

Block Elements

display: block;
/* Takes full width
   Starts on new line
   Can set width/height */

Examples: <div>, <p>, <h1>, <section>

Inline Elements

display: inline;
/* Only takes needed width
   Stays in line with text
   Cannot set width/height */

Examples: <span>, <a>, <strong>

Inline-Block

display: inline-block;
/* Best of both worlds!
   Stays inline
   Can set width/height */

Common use: Navigation links, buttons

None

display: none;
/* Completely removes element
   Takes no space */

visibility: hidden hides but keeps space!

display: none removes completely!

The Position Property

Control exactly where elements appear on the page.

Value Behavior Use Case
static Default. Normal document flow. Most elements
relative Offset from its normal position. Space preserved. Minor adjustments, parent for absolute children
absolute Removed from flow. Positioned relative to nearest positioned ancestor. Overlays, tooltips, dropdowns
fixed Removed from flow. Positioned relative to viewport. Sticky headers, floating buttons
sticky Switches between relative and fixed based on scroll. Table headers, section titles

Position Offset Properties

Use with positioned elements: top, right, bottom, left

.tooltip {
    position: absolute;
    top: 100%;
    left: 50%;
}

Relative & Absolute Positioning

Relative

.box {
    position: relative;
    top: 20px;
    left: 30px;
}
/* Moves 20px down, 30px right
   Original space is preserved! */

Think of it as: "Move from where you normally would be"

Original position
Relative moved

Absolute

.parent {
    position: relative; /* Creates context */
}
.child {
    position: absolute;
    top: 0;
    right: 0;
}
/* Positioned to top-right of parent */
Absolute

Parent has position: relative

Key Rule: Always set position: relative on the parent when using absolute children!

Fixed & Sticky Positioning

Fixed

.header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 1000;
}
  • Stays in place when scrolling
  • Relative to the viewport (browser window)
  • Removed from document flow

Common uses:

  • Fixed navigation bars
  • Chat widgets
  • "Back to top" buttons
  • Cookie consent banners

Sticky

.section-header {
    position: sticky;
    top: 0;
    background: white;
}
  • Acts like relative until scroll threshold
  • Then "sticks" like fixed
  • Requires top/bottom/left/right value

Common uses:

  • Table headers
  • Section headings in long pages
  • Sidebar navigation

Note: Sticky won't work if any parent has overflow: hidden!

Flexbox - Flexible Box Layout

The most important CSS layout tool! Perfect for one-dimensional layouts (row OR column).

Enable Flexbox

.container {
    display: flex;
}
/* All direct children become
   flex items! */

Default Behavior

  • Items line up in a row
  • Items stretch to same height
  • Items don't wrap

Live Demo

1
2
3

display: flex applied to parent

Flexbox Axes

  • Main Axis: Direction items flow (default: horizontal)
  • Cross Axis: Perpendicular to main axis

Flexbox Container Properties

flex-direction

flex-direction: row;      /* default */
flex-direction: row-reverse;
flex-direction: column;
flex-direction: column-reverse;

flex-wrap

flex-wrap: nowrap;   /* default */
flex-wrap: wrap;     /* items wrap */
flex-wrap: wrap-reverse;

justify-content (main axis)

justify-content: flex-start;
justify-content: flex-end;
justify-content: center;
justify-content: space-between;
justify-content: space-around;
justify-content: space-evenly;

align-items (cross axis)

align-items: stretch;    /* default */
align-items: flex-start;
align-items: flex-end;
align-items: center;
align-items: baseline;

gap

gap: 20px;           /* all gaps */
gap: 10px 20px;      /* row, column */
row-gap: 10px;
column-gap: 20px;

Centering Made Easy!

.center-everything {
    display: flex;
    justify-content: center;
    align-items: center;
}

Flexbox Item Properties

Properties applied to flex children, not the container.

flex-grow

.item {
    flex-grow: 1;  /* Take available space */
}
/* If all items have flex-grow: 1,
   they share space equally */

flex-shrink

.item {
    flex-shrink: 0;  /* Don't shrink */
}
/* Default is 1 (can shrink) */

flex-basis

.item {
    flex-basis: 200px;  /* Starting size */
}
/* Like width, but respects flex */

flex Shorthand

/* flex: grow shrink basis */
flex: 1;           /* flex: 1 1 0% */
flex: 0 0 200px;   /* Fixed 200px */
flex: 1 1 auto;    /* Flexible */

align-self

.special-item {
    align-self: flex-end;
}
/* Override align-items for one item */

order

.first {
    order: -1;  /* Move to start */
}
/* Default order is 0 */

Most Common Pattern:

.item { flex: 1; }  /* Equal width items */

CSS Grid - 2D Layouts

The most powerful CSS layout system! Perfect for two-dimensional layouts (rows AND columns).

Enable Grid

.container {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 100px 200px;
    gap: 20px;
}

Key Concepts

  • Track: A row or column
  • Cell: Intersection of row and column
  • Line: Dividers between tracks
  • Area: One or more cells

Live Demo

1
2
3
4
5
6

grid-template-columns: repeat(3, 1fr)

The fr Unit

fr = fraction of available space

1fr 2fr 1fr = 25% 50% 25%

CSS Grid Properties

Container Properties

/* Define columns */
grid-template-columns: 200px 1fr 1fr;
grid-template-columns: repeat(3, 1fr);
grid-template-columns: repeat(auto-fit,
    minmax(250px, 1fr));

/* Define rows */
grid-template-rows: 100px auto 100px;

/* Gaps */
gap: 20px;
row-gap: 10px;
column-gap: 20px;

Alignment

justify-items: center;  /* horizontal */
align-items: center;    /* vertical */
place-items: center;    /* both! */

Item Properties

/* Span multiple columns */
grid-column: 1 / 3;    /* line 1 to 3 */
grid-column: span 2;   /* span 2 cols */

/* Span multiple rows */
grid-row: 1 / 4;

/* Shorthand */
grid-area: 1 / 1 / 3 / 4;
/* row-start/col-start/row-end/col-end */

Named Grid Areas

grid-template-areas:
    "header header header"
    "sidebar main main"
    "footer footer footer";

.header { grid-area: header; }
.sidebar { grid-area: sidebar; }

Flexbox vs Grid - When to Use Which?

Use Flexbox When:

  • Layout is one-dimensional (row OR column)
  • Content should dictate layout
  • You need items to wrap naturally
  • Building components (nav, cards, buttons)

Examples:

  • Navigation menus
  • Card layouts
  • Centering content
  • Form layouts
  • Icon + text combinations

Use Grid When:

  • Layout is two-dimensional (rows AND columns)
  • Layout should dictate content placement
  • You have a complex, structured layout
  • Building page layouts

Examples:

  • Page layouts (header, sidebar, main, footer)
  • Image galleries
  • Dashboard layouts
  • Magazine-style layouts
  • Complex form layouts

Pro Tip

You can nest them! Use Grid for page layout, Flexbox for components inside grid cells.

CampusKart Milestone: Dashboard Layout

Deliverable: Product grid layout + navigation bar + card components using Flexbox

What to Build

  • Responsive navbar (logo left, links right) with Flexbox
  • Product card component (image, title, price, condition, seller)
  • Product grid (3-4 columns using Flexbox)
  • Category sidebar with filter options
  • Footer with links

Progress Check

Last week your forms were floating in space. Now they're part of a real app layout.

Apply the box model, positioning, and Flexbox concepts from today's class.

Push to GitHub when done.

Video Resources

Frontend Masters

CSS Grid & Flexbox

Comprehensive layout course

Flexbox Froggy

flexboxfroggy.com

Learn Flexbox with a fun game!

Grid Garden

cssgridgarden.com

Learn CSS Grid with a game!

Practice Exercises

Exercise 1: Navigation Bar

Build a responsive nav with Flexbox:

  • Logo on left
  • Links centered or right
  • Even spacing

Exercise 2: Card Layout

Create a responsive card grid:

  • Use CSS Grid
  • Auto-fit columns
  • Minimum card width 250px

Exercise 3: Holy Grail Layout

Classic layout with Grid:

  • Header spanning full width
  • Sidebar + Main content
  • Footer at bottom

Exercise 4: Sticky Header

Fixed navigation that:

  • Stays at top when scrolling
  • Has proper z-index
  • Doesn't overlap content

Examples to Study

View Week 05 Examples →

Week 05 Summary

Key Concepts

  • Display: block, inline, inline-block, flex, grid
  • Position: static, relative, absolute, fixed, sticky
  • Flexbox: 1D layouts, justify-content, align-items
  • Grid: 2D layouts, grid-template, fr units
  • z-index: Stack order for positioned elements

Best Practices

  • Use Flexbox for components
  • Use Grid for page layouts
  • Always set position: relative on parent for absolute children
  • Use gap instead of margins for flex/grid spacing
  • Test layouts at different screen sizes

Coming Next Week

Week 06: CSS Animations & Advanced Features - Transitions, transforms, and more!