CSS Website Layout


Website Layout

A website is usually split into different sections: there's a top part called the header, a menu section, the main content area, and a bottom part known as the footer.

Header
Navigation Menu
Content
Main Content
Content

There are many layout designs to pick from, but in this tutorial, we'll examine one of the most popular structures mentioned above.


Header

The top part of a website, usually right under the menu, is where you'll find the header. It often has a logo or the website's name in it.

Example

.header {
  background-color: #F1F1F1;
  text-align: center;
  padding: 20px;
}

Result

Header

Try it Yourself »

Navigation Bar

A navigation bar is like a menu on your website. It has a list of clickable links that help people move around your site.

Example

/* The navbar container */
.topnav {
  overflow: hidden;
  background-color: #333;
}

/* Navbar links */
.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

/* Links - change color on hover */
.topnav a:hover {
  background-color: #ddd;
  color: black;
}

Result

Try it Yourself »

Content

The way things are arranged in this part usually depends on who will be using it. The most usual arrangement is one of the following options, or sometimes a mix of them:

  • 1-column (often used for mobile browsers)
  • 2-column (often used for tablets and laptops)
  • 3-column layout (only used for desktops)

1-column:

 

2-column:

 

3-column:

We'll make a design with three columns, but when the screen is smaller, we'll switch it to just one column.

Example

/* Create three equal columns that float next to each other */
.column {
  float: left;
  width: 33.33%;
}

/* Clear floats after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}

/* Responsive layout - makes the three columns stack on top of each other instead of next to each other on smaller screens (600px wide or less) */
@media screen and (max-width: 600px) {
  .column {
    width: 100%;
  
}
}

Result

Column

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sit amet pretium urna. Vivamus venenatis velit nec neque ultricies, eget elementum magna tristique.

Column

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sit amet pretium urna. Vivamus venenatis velit nec neque ultricies, eget elementum magna tristique.

Column

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sit amet pretium urna. Vivamus venenatis velit nec neque ultricies, eget elementum magna tristique.

Try it Yourself »

Tip: To create a 2-column layout, change the width to 50%. To create a 4-column layout, use 25%, etc.

Tip: A more modern way of creating column layouts, is to use CSS Flexbox.

However, it is not supported in Internet Explorer 10 and earlier versions. If you require IE6-10 support, use floats (as shown above).


Unequal Columns

The primary content is the largest and most crucial section of your website.

This often happens when the columns don't have the same width. In such cases, most of the space is dedicated to the main content. The side content, if there is any, can be used for navigation or to provide additional information related to the main content. You can adjust the column widths as you prefer, but make sure they add up to a total of 100%.

Example

.column {
  float: left;
}

/* Left and right column */
.column.side {
  width: 25%;
}

/* Middle column */
.column.middle {
  width: 50%;
}

/* Responsive layout - makes the three columns stack on top of each other instead of next to each other */
@media screen and (max-width: 600px) {
  .column.side, .column.middle {
    width: 100%;
 
}
}

Result

Side

Lorem ipsum dolor sit amet, consectetur adipiscing elit...

Main Content

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sit amet pretium urna. Vivamus venenatis velit nec neque ultricies, eget elementum magna tristique. Quisque vehicula, risus eget aliquam placerat, purus leo tincidunt eros, eget luctus quam orci in velit. Praesent scelerisque tortor sed accumsan convallis.

Side

Lorem ipsum dolor sit amet, consectetur adipiscing elit...

Try it Yourself »

Footer

The footer sits at the very bottom of your webpage. It usually has important details such as copyright information and contact information.

Example

.footer {
  background-color: #F1F1F1;
  text-align: center;
  padding: 10px;
}

Result

Footer
Try it Yourself »

Responsive Website Layout

With the CSS code provided earlier, we've made a website that adjusts its layout based on the screen size. It can display content in either two columns or full-width columns, depending on how wide the screen is.