Skip to main content

CSS Website Layout

Websites are commonly split into sections: a header, a navigation menu, a main content area, and a footer.

That arrangement of header, navigation, content, and footer ranks among the most common, although plenty of other layout designs exist.


CSS Header

Placed at the top of a site in most cases, a header tends to hold a logo or the website name.

Example

Prompt
Create a page with a header element styled with a light gray background (#f1f1f1), centered text, and 10px padding. The header should contain an h1 with the text "My Header".
Output: complete HTML page

CSS Navigation Bar

Visitors find their way around your website using a navigation bar, which holds a list of links.

Example

Prompt
Create a page with a horizontal navigation bar using an unordered list with class "topnav". Use flexbox to display the items horizontally. The nav bar should have a dark background (#333333) with light-colored links (#f1f1f1), 14px 16px padding, no underlines, and a hover effect that changes the background to light gray (#dddddd) and text to black. Include four links: Link1, Link2, Link3, Link4.
Output: complete HTML page

CSS Layout Content

The device in use often determines how a website's content is displayed. These layouts are the most common:

  • 1-column layout - often seen on mobile browsers
  • 2-column layout - often seen on tablets and laptops
  • 3-column layout - for desktops only

In the example below, three columns collapse into one when the screen is narrower than 600px.

Tip: The CSS Media Queries chapter explains the CSS @media rule in more detail.

Tip: The CSS Flexbox chapter explains CSS Flexbox in more detail.

Example

Prompt
Create a page with a 3-column layout using flexbox. Each column should have a heading "Column" and a paragraph of lorem ipsum text. Use a media query so that when the screen width is less than 600px, the columns stack vertically. Each column should have 10px margin.
Output: complete HTML page

CSS Basic and Fixed Footer

At the bottom of a webpage sits the footer, commonly carrying details such as copyright and contact information.

Basic footer

A simple footer style looks like this.

Prompt
Create a page with some content and a footer element at the bottom. The footer should have a light gray background (#f1f1f1), centered text, 8px padding, and display the text "Footer".
Output: complete HTML page

Fixed footer

However far you scroll, a fixed footer remains visible at the bottom of the page.

Prompt
Create a page with enough content to scroll and a fixed footer that stays visible at the bottom of the viewport. The footer should use position fixed, bottom 0, left 0, width 100%, a light gray background (#f1f1f1), 8px padding, centered text, and z-index 1000. The footer text should say "Fixed Footer".
Output: complete HTML page

CSS Responsive Website

Combining media queries with flexbox, this example builds a responsive website whose navigation bar and content are both flexible.

Example

Prompt
Create a responsive website layout with a header, a horizontal navigation bar that becomes vertical on small screens, a flex-based content area with a side navigation and main article section, and a footer. Use media queries so the layout adapts for screens under 600px by stacking elements vertically.
Output: complete HTML page