Logo LIBR
Halo

Portfolio

Back to Blog
9 min read
LayoutMay 22, 2025

CSS Layout: Positioning and Modern Techniques

CSS, Layout, Flexbox, Grid
Syifa Fauziyah Arizal

Master modern CSS layout techniques including positioning, Flexbox, Grid systems, and responsive design to create professional web layouts.

Now that the design looks good, it's time to arrange the layout. In this lesson you'll learn how to position elements properly, build layouts using Flexbox and Grid, and create responsive experiences for different screen sizes.

Positioning: Keeping Elements in Place

CSS has the position property to control how elements "stick" to the page.

Types of Position

TypeBrief Description
staticDefault (follows HTML flow)
relativeOffset relative to initial position
absoluteSticks to nearest positioned parent
fixedSticks to viewport (doesn't scroll)
stickyHybrid of relative + fixed (sticks when scrolling)

Example

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

z-index: Who's On Top?

z-index controls the stacking order of elements (z-axis). Higher value = more forward.

CSS
.card {
  position: absolute;
  z-index: 10;
}

Useful so popups/modals don't get covered by other content. Think of it like Photoshop layers.

Flexbox: Stress-Free Layouts & Centering

Important Container Properties

CSS
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: row;
}

Important Item Properties

CSS
.item {
  flex-grow: 1;
}

Simple Flexbox Example

CSS
.flexbox {
  display: flex;
  gap: 20px;
  justify-content: space-between;
}
HTML
<div class="flexbox">
  <div class="box">1</div>
  <div class="box">2</div>
  <div class="box">3</div>
</div>
PropertyFunction
justify-contentHorizontal position (start, center, end, space-between)
align-itemsVertical position (start, center, stretch, end)
flex-directionLayout order: row / column

CSS Grid: For More Complex Layouts

Grid is like Flexbox's "matrix" version — perfect for 2D layouts (rows & columns).

Basic Example

CSS
.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

Important Properties

PropertyFunction
grid-template-columnsNumber & width of columns
grid-template-rowsNumber & height of rows
gapSpace between elements
grid-column / rowFor merging columns/rows (span)

CSS Grid Example

HTML
<div class="grid-container">
  <div class="grid-item">A</div>
  <div class="grid-item">B</div>
  <div class="grid-item">C</div>
</div>
CSS
.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

1fr = 1 fraction of available space. Think of it as: 1 slot out of 3 equally divided slots.

Responsive Layout

Use @media query to create responsive layouts based on screen size.

Basic Media Query Example

CSS
@media (max-width: 768px) {
  .container {
    flex-direction: column;
  }
}

Meaning: when screen ≤ 768px (tablet/mobile), change flex to vertical.

Key Takeaways

  • Know all position types and how to use them
  • Can control layers with z-index
  • Flexbox = easy layouts, centered without stress
  • CSS Grid = create complex magazine-like layouts
  • Media query = responsive layouts automatically

More to read