HTML Layout Techniques
Master modern web layout methods including Flexbox, CSS Grid, and positioning. Learn when and how to use each technique for creating responsive, professional layouts that work on all devices.
How do you structure a page layout in HTML?
Build the page from semantic landmark elements: <header> for the site banner, <nav> for main navigation, <main> for the unique content, <aside> for related content, and <footer> at the bottom. Then arrange them with CSS Grid or Flexbox; HTML gives the structure and CSS gives the layout.
<body>
<header>…logo, <nav>…</nav></header>
<main>
<article>…</article>
<aside>…</aside>
</main>
<footer>…</footer>
</body>
body { display: grid; grid-template-rows: auto 1fr auto; min-height: 100vh; }Try It: Essential Layout Methods
Explore the four main layout techniques: Flexbox for components, CSS Grid for page layouts, positioning for precise placement, and responsive auto-grids.
Code Editor
<header>Header</header>
<nav>Nav</nav>
<main>Main content</main>
<aside>Aside</aside>
<footer>Footer</footer>Preview
Layout Method Overview
Modern CSS provides multiple powerful layout methods, each with specific strengths. Understanding when to use Flexbox, Grid, positioning, or other techniques is crucial for creating effective web layouts.
One-Dimensional Layouts:
- Flexbox: Component layouts, navigation, forms
- Float: Text wrapping around images (legacy)
- Multi-column: Newspaper-style text columns
Two-Dimensional Layouts:
- CSS Grid: Page layouts, dashboards, complex designs
- Position: Overlays, tooltips, precise placement
- Table: Only for actual tabular data
Layout Selection Guide:
Use Flexbox When:
- • Creating navigation bars
- • Aligning form controls
- • Centering content
- • Distributing space evenly
- • Building components
Use CSS Grid When:
- • Creating page layouts
- • Building dashboards
- • Making card grids
- • Complex 2D arrangements
- • Magazine-style layouts
Flexbox: One-Dimensional Layouts
Flexbox is perfect for one-dimensional layouts. It excels at distributing space and aligning items within a container, making it ideal for navigation bars, form layouts, and component arrangements.
Key Flexbox Properties:
display: flexCreates flex container
justify-contentControls main axis alignment
align-itemsControls cross axis alignment
flex-directionSets main axis direction
Flexbox Use Cases:
- Navigation bars: Horizontal menu layouts
- Form controls: Aligning inputs and labels
- Card layouts: Equal height components
- Centering: Both horizontal and vertical
Common Flexbox Patterns:
Perfect Centering:
.center {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}Space Distribution:
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem;
}CSS Grid: Two-Dimensional Layouts
CSS Grid is the most powerful layout system available. It's designed for two-dimensional layouts and can handle complex arrangements with precise control over rows and columns.
Essential Grid Properties:
display: gridCreates grid container
grid-template-columnsDefines column sizes
grid-template-areasNames grid regions
gapSpace between grid items
Grid Use Cases:
- Page layouts: Header, sidebar, main, footer
- Card grids: Responsive image galleries
- Dashboards: Complex data layouts
- Magazine layouts: Asymmetric designs
Advanced Grid Techniques:
Template Areas:
.grid-layout {
display: grid;
grid-template-areas:
"header header header"
"nav main sidebar"
"footer footer footer";
}Auto-Responsive Grid:
.auto-grid {
display: grid;
grid-template-columns:
repeat(auto-fit, minmax(250px, 1fr));
gap: 1rem;
}Positioning & Modern Layouts
CSS positioning allows precise control over element placement. Combined with modern layout techniques, it enables complex, interactive designs.
Position Types:
staticDefault normal flow
relativeRelative to normal position
absoluteRelative to positioned ancestor
fixedRelative to viewport
stickySwitches between relative/fixed
Common Position Uses:
- Modals & overlays: Fixed positioning
- Tooltips: Absolute positioning
- Sticky headers: Sticky positioning
- Floating buttons: Fixed positioning
Modern Layout Combinations:
Grid + Flexbox:
Use Grid for page layout, Flexbox for components within grid areas
Position + Grid:
Overlay positioned elements on grid layouts for interactive features
Container Queries:
Future CSS feature for component-based responsive design
Responsive Layouts & Performance
Modern layouts must work seamlessly across all devices while maintaining optimal performance. Learn key strategies for responsive, fast-loading layouts.
Responsive Strategies:
- Mobile-first approach: Start with mobile designs
- Intrinsic web design: Let content determine layout
- Auto-responsive grids: No media queries needed
- Flexible units: %, rem, em, vw, vh
Performance Tips:
- Avoid layout thrashing: Minimize DOM changes
- Use transform: For animations over layout properties
- Optimize images: Responsive images with srcset
- Critical CSS: Inline above-the-fold styles
Layout Testing Checklist:
Device Testing:
- • Test on real devices
- • Check different orientations
- • Verify touch interactions
- • Test with slow networks
Accessibility Testing:
- • Keyboard navigation
- • Screen reader compatibility
- • Focus management
- • Color contrast ratios
Performance Testing:
- • Measure layout shifts
- • Check paint times
- • Monitor memory usage
- • Test rendering performance
Layout Best Practices
Best Practices:
- Choose the right tool
Use Flexbox for components, Grid for layouts, Position for overlays
- Mobile-first approach
Design for mobile devices first, then enhance for larger screens
- Semantic HTML first
Use proper HTML structure before applying layout styles
Common Mistakes:
- ✗Using tables for layout
Tables should only be used for tabular data
- ✗Overusing absolute positioning
Can break responsive design and accessibility
- ✗Fixed pixel dimensions
Use relative units for better responsiveness
HTML Layout — step by step
Use semantic layout elements
Build layout structure with header, nav, main, article, aside, section, and footer. These communicate purpose and create accessibility landmarks automatically.
🎯 Quick Check
Which approach is recommended when writing responsive CSS: mobile-first or desktop-first?
HTML Page Layout: summary
Key points
- Use only one
<main>per page; screen readers can jump straight to it. - Landmarks replace generic wrappers like
<div id="header">and give structure to assistive technology. - Don't use tables or floats for page layout; use Grid and Flexbox.
Frequently asked questions
›What is the difference between section and div for layout?
<section> groups related content with a heading. <div> is a meaningless wrapper for styling. Use landmarks first and <div> only for purely visual grouping.
›How do I make the footer stay at the bottom?
Make the body a grid with grid-template-rows: auto 1fr auto; min-height: 100vh; so <main> takes the leftover space.