Glossary
HTML
CSS
JavaScript

Python

CSS

CSS width: The Width Property

The CSS width property specifies the width of an HTML element. By default, width sets the width of the content area of an element, excluding padding and borders.

How to Use CSS width

You can define the width in different units like pixels (px), percentages (%), or viewport width (vw).

.container {
    width: 300px; /* Fixed width in pixels */
    max-width: 100%; /* Maximum width */
}

<div class="container">
    Fixed-width container with a maximum width of 100%.
</div>

When to Use CSS width

You need the width property to adjust the width of HTML elements.

Layout Design

You can define specific widths for containers or elements. Using pixel-based values, you can ensure that the layout remains consistent regardless of the content.

.sidebar {
    width: 250px;
}

Responsive Design

Using percentages for width helps you create responsive designs that adjust to different screen sizes.

.content {
    width: 80%;
}

Grid Systems

In grid layouts, setting explicit widths for columns ensures proper alignment and distribution of space.

.column {
    width: 33.3333%;
}

Examples of width in CSS

Countless websites use the CSS width property to manage the width of elements.

Social Media Applications

A social media application might set a fixed width for its navigation menu. By doing so, the width remains the same size regardless of the viewport.

nav {
    width: 200px;
}

News Websites

Article layouts might set width to a percentage to use most of the viewport while considering margins.

.article {
    width: 90%;
    margin: auto;
}

Photo Galleries

Setting minimum and maximum width values ensures the element adjusts within specific bounds. A photo gallery might use these properties to maintain image proportions without stretching or shrinking them too much.

.column {
		width: 33.3333%;
    min-width: 300px;
    max-width: 1200px;
}

Learn More About width in CSS

Units for width

The CSS width property accepts various units, including pixels, percentages, and viewport width units.

  • Pixels (px): Pixels are absolute units that provide precision and don't change with browser settings or other contextual factors. They are ideal for exact sizing.

    .box {
        width: 300px; /* Fixed width of 300 pixels */
    }
    
  • Percentages (%): Percentages are relative units that define the width as a percentage of the parent element's width. They are great for creating flexible and responsive layouts.

    .container {
        width: 80%; /* Width is 80% of the parent element */
    }
    
  • Viewport Width (vw): Viewport width units are relative to the width of the viewport. For example, 1vw defines 1% of the viewport's width. This is helpful for responsive designs that adjust based on screen size.

    .hero {
        width: 50vw; /* Width is 50% of the viewport's width */
    }
    

CSS Max Width

Using the max-width property, you can allow an element to grow only to a set limit. If the value for width is greater than the value for max-width, then max-width overrides width.

.responsive-container {
    width: 100%;
    max-width: 960px;
    margin: auto;
}

CSS Min Width

Similarly, with the mix-width property, you can allow an element to shrink only to a set limit. If the value for width is less than the value for min-widthmin-width prevails over width.

.responsive-container {
    width: 20%;
    max-width: 250px;
    margin: auto;
}

Including Border and Padding

By default, the width property specifies the width of the content area of an element, excluding padding and borders. To include padding and borders, you can set the box-sizing property to border-box.

For example, by setting width to 300px and box-sizing to border-box, the total width (300 pixels) includes padding and borders.

.box {
    width: 300px;
    padding: 20px;
    border: 5px solid black;
    box-sizing: border-box;
}

Combining Width with Other CSS Properties

Combining width with properties like margin and padding provides greater control over element layout and spacing.

.adjusted-box {
    width: 50%;
    padding: 10px;
    margin: 20px auto;
}

<div class="adjusted-box">
    A box with combined width, padding, and margin properties.
</div>
Learn to Code in Python for Free
Start learning now
To advance beyond this tutorial and learn Python by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2023 Mimo GmbH