Introduction to CSS Layout
CSS layout is the system that controls how elements are positioned and displayed on a webpage. Mastering CSS layout is essential for creating responsive, visually appealing websites that work across different devices and screen sizes. This cheatsheet covers everything from traditional positioning methods to modern layout techniques like Flexbox and Grid.
Core CSS Layout Concepts
The Box Model
| Component | Description | CSS Property |
|---|---|---|
| Content | The actual content of the element | width, height |
| Padding | Space between content and border | padding |
| Border | Line around the padding | border |
| Margin | Space outside the border | margin |
Box Sizing Property:
/* Makes width/height include padding and border */
* {
box-sizing: border-box;
}
Display Properties
• display: block – Takes up full width, starts on new line • display: inline – Takes only needed width, doesn’t force new line • display: inline-block – Block-level properties but flows inline • display: none – Removes element from document flow • display: flex – Creates a flex container • display: grid – Creates a grid container
Positioning Methods
• position: static – Default positioning • position: relative – Positioned relative to normal position • position: absolute – Positioned relative to nearest positioned ancestor • position: fixed – Positioned relative to viewport • position: sticky – Positioned based on scroll position
Modern Layout Systems
Flexbox
Flex Container Properties:
.container {
display: flex;
flex-direction: row | row-reverse | column | column-reverse;
flex-wrap: nowrap | wrap | wrap-reverse;
justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly;
align-items: stretch | flex-start | flex-end | center | baseline;
align-content: flex-start | flex-end | center | space-between | space-around | stretch;
gap: 10px; /* row-gap and column-gap shorthand */
}
Flex Item Properties:
.item {
order: 0; /* Default is 0, can be negative */
flex-grow: 0; /* Default is 0, proportion of space to take up */
flex-shrink: 1; /* Default is 1, how much to shrink */
flex-basis: auto; /* Default size before growing/shrinking */
flex: 0 1 auto; /* Shorthand for grow, shrink, basis */
align-self: auto | flex-start | flex-end | center | baseline | stretch;
}
CSS Grid
Grid Container Properties:
.container {
display: grid;
grid-template-columns: 100px 100px 100px | repeat(3, 1fr) | minmax(100px, 1fr);
grid-template-rows: 100px 100px | auto 1fr;
grid-template-areas: "header header" "sidebar content" "footer footer";
grid-column-gap: 10px;
grid-row-gap: 15px;
gap: 15px 10px; /* row-gap column-gap shorthand */
justify-items: start | end | center | stretch;
align-items: start | end | center | stretch;
justify-content: start | end | center | stretch | space-around | space-between | space-evenly;
align-content: start | end | center | stretch | space-around | space-between | space-evenly;
grid-auto-flow: row | column | dense;
}
Grid Item Properties:
.item {
grid-column: 1 / 3 | 1 / span 2 | span 2;
grid-row: 1 / 3 | 1 / span 2 | span 2;
grid-area: header | 1 / 1 / 3 / 3;
justify-self: start | end | center | stretch;
align-self: start | end | center | stretch;
}
Layout Methods Comparison
| Feature | Floats | Flexbox | Grid | Positioning |
|---|---|---|---|---|
| Dimension | 1D | 1D | 2D | N/A |
| Control | Limited | Good | Excellent | Precise |
| Nesting | Required | Optional | Optional | Often required |
| Browser Support | All | Good | Good | All |
| Use case | Simple layouts, text wrapping | Components, rows/columns | Page layouts | Specific positioning |
| Learning curve | Medium | Medium | High | Medium |
Responsive Layout Techniques
Media Queries
/* Small devices (phones) */
@media (max-width: 767px) { ... }
/* Medium devices (tablets) */
@media (min-width: 768px) and (max-width: 991px) { ... }
/* Large devices (desktops) */
@media (min-width: 992px) { ... }
/* Print styles */
@media print { ... }
Responsive Units
• % – Relative to parent element • vw – 1% of viewport width • vh – 1% of viewport height • vmin – 1% of smaller dimension (width or height) • vmax – 1% of larger dimension (width or height) • em – Relative to font-size of parent • rem – Relative to font-size of root element
Responsive Images
img {
max-width: 100%;
height: auto;
}
Common CSS Layout Challenges & Solutions
Centering Elements
Horizontally center a block element:
.center-block {
margin-left: auto;
margin-right: auto;
}
Center text horizontally:
.center-text {
text-align: center;
}
Center both horizontally and vertically (modern way):
/* Using Flexbox */
.center-flex {
display: flex;
justify-content: center;
align-items: center;
}
/* Using Grid */
.center-grid {
display: grid;
place-items: center;
}
Equal Height Columns
/* Using Flexbox */
.equal-height {
display: flex;
}
/* Using Grid */
.equal-height {
display: grid;
grid-auto-flow: column;
grid-auto-columns: 1fr;
}
Sticky Footer
/* Using Flexbox */
body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
main {
flex: 1;
}
/* Using Grid */
body {
display: grid;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
}
Best Practices & Tips
• Use a CSS Reset/Normalize: Ensures consistent rendering across browsers • Mobile-First Approach: Design for mobile screens first, then enhance for larger screens • Avoid Fixed Heights: Use min-height instead to prevent overflow issues • Combine Layout Methods: Use Grid for page layout and Flexbox for components • Use CSS Variables: For consistent spacing and responsive values • Avoid !important: Can lead to specificity issues • Use Feature Queries: Check for feature support before using newer properties
@supports (display: grid) {
.container {
display: grid;
}
}
• Test Across Browsers: Layouts may render differently in various browsers • Minimize DOM Nesting: Flatter DOM structure often means simpler CSS • Avoid Magic Numbers: Use relative units and calculated values
Advanced Layout Techniques
Subgrid (New in CSS Grid Level 2)
.container {
display: grid;
grid-template-columns: repeat(9, 1fr);
}
.item {
grid-column: span 3;
display: grid;
grid-template-columns: subgrid;
}
CSS Shapes and Exclusions
.floated-element {
float: left;
shape-outside: circle(50%);
clip-path: circle(50%);
width: 200px;
height: 200px;
}
CSS Multi-Column Layout
.multi-column {
column-count: 3;
column-gap: 20px;
column-rule: 1px solid #ccc;
}
.spanning-element {
column-span: all;
}
Resources for Further Learning
• Documentation:
• Tools:
- Flexbox Froggy – Learn Flexbox through a game
- Grid Garden – Learn Grid through a game
- CSS Grid Generator
• Books:
- “CSS: The Definitive Guide” by Eric Meyer
- “CSS Secrets” by Lea Verou
• Frameworks/Libraries:
