Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

W3.CSS Containers

The Container Class

The w3-container class is one of the most useful classes in W3.CSS.

It adds 16px left and right padding to an HTML element.

This gives content consistent spacing and alignment across a web page.

London

London is the capital of England.

Example

<div class="w3-container w3-blue">
  <h2>London</h2>
  <p>London is the capital of England.</p>
</div>
Try it Yourself »

Containers Create Consistent Spacing

Without a container, content can run all the way to the edge of its parent element.

With w3-container, the content gets consistent horizontal spacing.

No container


With w3-container

Example

<div>
  <p>No container</p>
</div>

<div class="w3-container">
  <p>With w3-container</p>
</div>
Try it Yourself »

Containers Work with most HTML Elements

The w3-container class can be used with many HTML elements.

For example:

  • <div>
  • <header>
  • <footer>
  • <section>
  • <article>
  • <form>

W3.CSS styles classes, not HTML elements.

This means w3-container works the same way on a <div>, <section>, or <article>.

It also works on other elements like headers <h1> and paragraphs <p>.


Headers and Footers

Containers are useful for page headers and footers.

My Website

Welcome

This is the main content of the page.

Copyright 2026

Example

<header class="w3-container w3-teal">
  <h1>My Website</h1>
</header>

<div class="w3-container">
  <h2>Welcome</h2>
  <p>This is the main content of the page.</p>
</div>

<footer class="w3-container w3-teal">
  <p>Copyright 2026</p>
</footer>
Try it Yourself »


Sections and Articles

Containers are also useful for grouping sections of content.

Travel Guide

Discover some of Europe's most popular destinations.


Paris

Paris is known for art, architecture, food, and history.

Example

<section class="w3-container w3-light-grey">
  <h2>Travel Guide</h2>
  <p>Discover some of Europe's most popular destinations.</p>
</section>

<article class="w3-container">
  <h3>Paris</h3>
  <p>Paris is known for art, architecture, food, and history.</p>
</article>
Try it Yourself »

Containers and Colors

Combine w3-container with a W3.CSS color class to create colored sections.

News

Latest stories and updates.

Example

<div class="w3-container w3-indigo">
  <h2>News</h2>
</div>

<div class="w3-container w3-pale-blue">
  <p>Latest stories and updates.</p>
</div>
Try it Yourself »

Container Padding

The w3-container class adds 16px left and right padding.

It does not add top or bottom padding.

I have no top or bottom padding

Example

<div class="w3-container w3-blue">
I have no top or bottom padding.
</div>
Try It Yourself »

Normally you will not have to change the default padding of a container because HTML headings and paragraphs provide their own top and bottom margins:

A Heading

A paragraph inside a container.

Examples

<div class="w3-container w3-blue">
  <h2>A Heading</h2>
  <p>A paragraph inside a container.</p>
</div>
Try it Yourself »
<div class="w3-container w3-blue">
  <h1>I am a Heading</h1>
  <p>I am a paragraph.</p>
</div>
Try It Yourself »

When you need extra vertical space, combine the container with a padding class:

More Space

This container has extra top and bottom padding.

Example

<div class="w3-container w3-blue w3-padding-32">
  <h2>More Space</h2>
  <p>This container has extra top and bottom padding.</p>
</div>
Try it Yourself »

Build a Page with Containers

Containers can form the basic sections of a complete web page.

City Guide

Explore Copenhagen

Discover architecture, food, canals, and Scandinavian design.

City Guide

Example

<header class="w3-container w3-black">
  <h1>City Guide</h1>
</header>

<section class="w3-container w3-padding-32">
  <h2>Explore Copenhagen</h2>
  <p>Discover architecture, food, canals, and Scandinavian design.</p>
  <button class="w3-button w3-teal">Explore</button>
</section>

<footer class="w3-container w3-light-grey">
  <p>City Guide</p>
</footer>
Try it Yourself »

Note

Many web pages use <div> elements instead of <header> and <footer> elements.

There is no difference in the way W3.CSS treats these elements.


What You Have Learned

The w3-container class gives sections of a page consistent horizontal spacing.

  • It adds 16px left and right padding
  • It works with <div>, <header>, <footer>, <section>, <article>, and other elements
  • It can be combined with colors, padding, cards, and other W3.CSS classes
  • It is useful for building the main sections of a web page

More Examples

Explore more examples to see how w3-container can be used.

Example

<div class="w3-container">
  <p>The w3-container class is an important w3.CSS class.</p>
</div>
Try It Yourself »

To add a color, just add a w3-color class:

Example

<div class="w3-container w3-red">
  <p>London is the capital city of England.</p>
</div>
Try It Yourself »

The w3-container class can be used to style headers:

Examples

<div class="w3-container w3-teal">
  <h1>Header</h1>
</div>
Try It Yourself »
<header class="w3-container w3-teal">
  <h1>Header</h1>
</header>
Try It Yourself »

There is no difference in the way W3.CSS treats <div> and <header> elements.

The w3-container class can be used to style footers:

Examples

<div class="w3-container w3-teal">
  <h5>Footer</h5>
  <p>Footer information goes here</p>
</div>
Try It Yourself »
<footer class="w3-container w3-teal">
  <h5>Footer</h5>
  <p>Footer information goes here</p>
</footer>
Try It Yourself »

There is no difference in the way W3.CSS treats <div> and <footer> elements.


Articles and Sections

The w3-container class can be used to style <article> and <section> elements:

Example

<div class="w3-container">
  <h2>London</h2>
  <p>London is the most populous city in the United Kingdom,
  with a metropolitan area of over 9 million inhabitants.</p>
</div>

<article class="w3-container">
  <h2>Paris</h2>
  <p>The Paris area is one of the largest population centers in Europe,
  with more than 2 million inhabitants.</p>
</article>

<section class="w3-container">
  <h2>Tokyo</h2>
  <p>Tokyo is the center of the Greater Tokyo Area,
  and the most populous metropolitan area in the world.</p>
</section>
Try It Yourself »

Note

There is no difference in the way W3.CSS treats <div>, <article> and <section> elements.

Containers can be rounded:

Example

<div class="w3-container w3-blue w3-round-xlarge">
  <p>London is the most populous city in the United Kingdom,
  with a metropolitan area of over 9 million inhabitants.</p>
</div>
Try It Yourself »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->