Frontend learning notes
Build layouts with intention.
A practical guide to the CSS concepts that help websites feel organized, responsive, and easier to use.
Introduction
CSS layout is how we control the placement, spacing, and behavior of elements on a webpage. Good layouts are not only visually appealing; they also help people read and navigate content more easily.
This guide focuses on core CSS tools that are useful for modern web development: the box model, Flexbox, Grid, responsive design, and positioning.
The Box Model
Every HTML element is treated like a rectangular box. The box model has four layers: content, padding, border, and margin.
* {
box-sizing: border-box;
}
Using box-sizing: border-box makes width calculations
easier because padding and borders are included inside an element’s set
width.
- Content: the text, image, or item inside the box
- Padding: space inside the box
- Border: the line around the padding and content
- Margin: space outside the box
Flexbox Basics
Flexbox is useful when arranging items in one direction: either a row or a column. It is commonly used for navigation bars, button groups, card content, and small layouts.
.card-row {
display: flex;
gap: 1rem;
align-items: stretch;
}
The gap property creates consistent space between sibling
elements. It is usually cleaner than adding margins to each item.
When should I use flex: 1?
Use it when an item should grow and take available space. For example,
a card’s content area can use flex: 1 so buttons line up
at the bottom of different cards.
CSS Grid Basics
CSS Grid is useful for two-dimensional layouts. It lets you control both columns and rows, making it ideal for galleries, dashboard layouts, and project-card sections.
.project-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1.5rem;
}
The repeat(3, 1fr) value creates three equal columns.
The fr unit means “a fraction of the available space.”
What does minmax() do?
It sets a minimum and maximum size for a grid track. It is helpful when you want columns to resize naturally without becoming too small.
.project-grid {
display: grid;
grid-template-columns: repeat(
auto-fit,
minmax(220px, 1fr)
);
gap: 1.5rem;
}
Responsive Design
Responsive design means that a webpage adapts to different screen sizes. A desktop layout may use multiple columns, while a mobile layout may stack the same content vertically.
@media (max-width: 700px) {
.project-grid {
grid-template-columns: 1fr;
}
}
You can also use clamp() for fluid text sizes that grow
with the screen but stay within a minimum and maximum size.
h1 {
font-size: clamp(2.5rem, 6vw, 5rem);
}
Why use max-inline-size for paragraphs?
It controls line length so paragraphs remain readable. Very wide text lines are harder to follow, especially on large screens.
Positioning Elements
Positioning lets you place elements in a more specific location. Normal document flow should be your default, but positioning is useful for small details like image captions, badges, and sticky navigation.
#navbar {
position: sticky;
top: 0;
height: 100vh;
}
A sticky sidebar stays visible while the reader scrolls through the documentation. This makes it easier to move between sections.
.image-caption {
position: absolute;
right: 1rem;
bottom: 1rem;
}
An absolutely positioned element is positioned relative to its nearest
ancestor with position: relative.
Useful Layout Patterns
Most interfaces are built by combining a few reliable layout patterns. Learning these patterns makes future projects faster to plan and build.
- Centered content using
width: min(1100px, 90%) - Responsive card grids using
auto-fitandminmax() - Equal-height cards using Flexbox and
flex: 1 - Sticky sidebars for documentation layouts
- Mobile-first layouts that stack content when space becomes limited
Continue practicing by looking at a design first, identifying its layout patterns, then rebuilding it with semantic HTML and CSS.