A CSS Website Layout defines how different sections of a webpage are
arranged on the screen. It controls the position and size of elements such
as the header, navigation bar, sidebar, main content, and footer. Modern
website layouts are commonly built using Flexbox and
CSS Grid, making webpages responsive and easy to
maintain.
Why Use CSS Website Layout?
A well-designed layout improves readability, navigation, and user
experience. CSS allows developers to create responsive layouts that adapt
automatically to desktops, tablets, and mobile devices without changing
the HTML structure.
Common Website Sections
Section
Purpose
Example
Header
Top Section
Website title or logo.
Navigation
Menu
Links to different pages.
Main Content
Information
Articles, images, videos, and text.
Sidebar
Extra Content
Categories, advertisements, widgets.
Footer
Bottom Section
Copyright and contact information.
Step 1: Create the Basic Layout
Start by creating the basic sections of a webpage using semantic HTML elements.
What this code does: Creates the four basic page sections (header, navigation, main content and footer) with semantic HTML, each drawn as an outlined box so you can see them.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the basic layout demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo only needs one shared rule, because the four sections look the same. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
The commas make this a group selector: one rule is applied to all four classes. This avoids writing the same four declarations four times, and it is how real sites share common styles across sections.
padding: 15px adds space inside each section, between its border and its text. margin-bottom: 8px adds a small gap below each one, so the boxes do not touch.
In this step the sections have no real design yet. The blue border, rounded corners and light blue background simply make each section easy to see. The } closes the shared rule.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content. The four sections are written in the same order that they appear on screen: top to bottom.
<header> is the semantic element for the top of the page, usually holding the logo or site title. It is a block element, so it spans the full width by default.
<nav> marks a group of navigation links. Using it instead of a plain div helps screen readers and search engines to find the menu.
<main> holds the primary content of the page. There should be only one main per page.
<footer> is the bottom section, normally used for copyright and contact information. Because there is no float or flex here, every section simply stacks vertically.
</body> closes the body. At this stage the layout is just four stacked blocks, and the later steps add the sidebar and responsiveness.
</html> is the final closing tag and ends the document.
Step 2: Style the Layout
Add colors, spacing, and alignment to make the layout visually appealing.
What this code does: Gives each section its own background colour, padding and centred text so the page looks like a real website skeleton.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the styled layout demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo uses one shared rule plus one rule per section. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
Properties that every section needs are written once in a group rule: padding: 20px for inner spacing and text-align: center to center the text. Rules after this one add what is different for each section.
A bright blue background with white text makes the header the strongest element on the page. Always pair a dark background with a light color so the text stays readable.
A dark gray bar is a common menu style. It separates the navigation from the blue header above it.
A light gray background keeps the reading area calm. min-height: 150px gives the section a minimum height even with little content. It can still grow taller when more content is added, which a fixed height would not allow.
The darkest color is used for the footer, which visually closes the page. The padding and centered text come from the shared rule above.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: the same four semantic sections as in Step 1.
The header picks up the shared styles and the blue header rule through its class.
The navigation bar uses demo-nav, so it is dark gray with white centered text.
The main area is light gray with at least 150px of height.
The footer is the darkest block and ends the page.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 3: Add a Sidebar Using Flexbox
Flexbox makes it easy to place the sidebar beside the main content.
What this code does: Wraps a sidebar (30%) and main content (70%) in a display: flex container so they sit side by side.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the flexbox sidebar demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo has a container rule and one rule each for the sidebar and the main area. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
display: flex turns .demo-container into a flex container. Its direct children, the aside and the main, become flex items and are placed in a row, side by side, with no floats required. Only direct children are affected.
The sidebar takes 30% of the container width. Percentages let the layout adjust when the window size changes.
A gray background separates the sidebar from the content. padding: 20px keeps its text away from the edges.
box-sizing: border-box counts padding inside the 30% width. Otherwise 30% plus padding would be wider than planned and the row could overflow. The radius values go top-left, top-right, bottom-right, bottom-left, so only the left corners are rounded, and the sidebar joins neatly with the main area.
The main area gets the remaining 70%. 30% plus 70% equals 100%, so the two boxes fill the row exactly.
A light background, inner padding and border-box sizing. The border-box setting is as important here as in the sidebar, because the border added below would otherwise push the total over 100%.
A blue border outlines the main area. The radius 0 6px 6px 0 rounds only the right corners, which is the opposite of the sidebar.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: a flex container holding the sidebar and the main area.
This div is the flex container. Both columns must be direct children of it for the flex layout to work.
<aside> is the semantic element for side content. It appears first in the HTML, so it is shown on the left.
The main area comes second, so it is placed to the right of the sidebar. Changing the HTML order changes the visual order in a flex row.
</div> closes the container. The layout ends here.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 4: Make the Layout Responsive
Media queries allow the layout to adapt to smaller screens by stacking the sidebar and the main content in one column.
What this code does: Uses a media query so that on screens 768px wide or less the sidebar and main content stack vertically at full width.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the responsive layout demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo extends the flex layout with a media query. To see the effect, make the browser window narrower than 768px. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
On wide screens, display: flex keeps the sidebar and main side by side, as in Step 3.
The sidebar uses 30% of the width, with a gray background and padding. box-sizing: border-box keeps that padding inside the 30%.
The main area uses the other 70% with its own background, padding and border. These are the default rules that apply to wide screens.
@media (max-width: 768px) means the rules inside only apply when the screen is 768px wide or narrower, such as a tablet or phone. flex-direction: column changes the main axis from a row to a column, so the items are stacked vertically. Because the sidebar is first in the HTML, it appears above the main content.
In a column layout the 30% and 70% widths would leave each box narrow. Setting width: 100% lets both boxes use the full width. The last } closes the media query. Rules inside it override the earlier rules only while the screen is small.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content. The HTML is identical to Step 3: only the CSS changes how it is arranged.
The same container as Step 3. Its direction changes from row to column when the media query is active.
The sidebar is first in the code, so it is left on wide screens and on top on small screens.
The main area is on the right on wide screens and below the sidebar on small screens.
</div> closes the container.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 5: Create a Complete Website Layout
Combine the header, navigation, sidebar, main content, and footer into a complete responsive webpage.
What this code does: Combines the header, navigation bar, sidebar, main content and footer into one complete responsive webpage.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the complete website layout demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo combines the rules from all previous steps. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
A blue banner with white centered text. This is a full-width block at the top, so it needs no layout rule.
A dark bar with slightly less padding than the header, so the header looks more important.
Only the middle part of the page uses flex. The header, nav and footer stay normal full-width blocks above and below it.
The sidebar takes 25% of the width. With box-sizing: border-box, its padding is included in that 25%.
The main area takes the other 75%, so 25% + 75% fills the row. A very light blue background and a thin border separate it from the page.
A full-width dark block that closes the page, with centered white text.
When the screen is 768px or narrower, flex-direction: column stacks the sidebar above the main content. The header, nav and footer are not affected, because they are outside the flex container.
Both columns become full width so there are no narrow boxes on a phone. The } closes the media query.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content. The elements follow the page structure from top to bottom: header, nav, middle container and footer.
The site name inside a <header>, shown across the top of the page.
The menu text inside <nav>. On a real site these would be links.
This div wraps only the sidebar and the main content, so that flex affects just those two columns.
The sidebar is written first, so it is on the left, or on top on small screens.
The primary content of the page.
</div> ends the flex container, so the footer that follows is outside it and spans the full width.
The footer closes the page with the copyright line.
</body> closes the body. The page now has all five sections of a typical website.
</html> is the final closing tag and ends the document.
Advantages of CSS Website Layout
Advantage
Benefit
Description
Responsive Design
Works Everywhere
Adapts to different screen sizes.
Better Organization
Easy Navigation
Content is arranged logically.
Professional Appearance
Modern Design
Creates clean and attractive webpages.
Easy Maintenance
Reusable Layout
Layout can be reused across multiple pages.
Best Practices
Use semantic HTML elements such as <header>, <nav>, <main>, <aside>, and <footer>.
Use Flexbox or CSS Grid for modern layouts.
Create responsive designs using media queries.
Use percentage widths or flexible units instead of fixed widths.
Keep navigation easy to access.
Maintain consistent spacing throughout the webpage.
Test the layout on desktop, tablet, and mobile devices.
Organize your CSS into logical sections for easier maintenance.