Skip to main content

CSS Box Model

CSS Width and Height

Written by Updated CSS Width and Height exercises

Set physical and logical dimensions while using min/max constraints, percentages, intrinsic content, and box sizing to prevent overflow.

width and height set the size of a box. Width is the one you will use all the time. Height is the one that causes trouble: fix it, and the moment the text gets longer, it spills out of the box.

Both work together with Box Sizing, which decides whether padding and border count towards the size, and with Intrinsic Sizing, where the content decides the size.

Syntax

css

selector {
    width: value;
    height: value;
}

Example

css

div {
    width: 200px;
    height: 100px;
}

That makes the div 200px wide and 100px tall.

Properties

PropertyWhat it setsExample
widthThe widthwidth: 300px;
heightThe heightheight: 150px;
max-widthThe largest it may getmax-width: 100%;
min-widthThe smallest it may getmin-width: 200px;
min-heightThe smallest height; it can still growmin-height: 300px;
max-heightThe largest heightmax-height: 400px;

Prefer limits to fixed sizes

A fixed height breaks as soon as the text wraps onto more lines, the font loads bigger, or someone zooms in. Let the height be auto (the default) and use min-height if the box needs a minimum size. For width, max-width beats width:

Shrinks on small screens, stops growing on big ones

css

.article {
  max-width: 65ch;
  margin: 0 auto;
}

img {
  max-width: 100%;
  height: auto;
}

The article can be narrower on a phone but never wider than a comfortable reading line. The image never overflows its container, and height: auto keeps its proportions.

Why height: 50% does nothing

A percentage height needs the parent to have a fixed height. If the parent is auto, the browser cannot work out 50% of it, and the percentage is ignored. Width percentages work because the parent's width is usually known. For full-screen sections, use min-height: 100dvh instead.

Example

Complete CSS Width and Height Example

html

<!DOCTYPE html>
<html>
<head>
    <title>CSS Width and Height</title>
    <style>
        div {
            width: 300px;
            height: 150px;
            background-color: lightblue;
            padding: 10px;
            border: 2px solid black;
        }

        img {
            width: 200px;
            height: auto;
        }
    </style>
</head>
<body>

    <div>This box has fixed width and height.</div>

    <img src="https://picsum.photos/seed/sample/600/400" alt="Sample Image">

</body>
</html>

The box is 300px wide and 150px tall, but with its padding and border it takes up a bit more space on the page. The image is 200px wide, and its height follows automatically.

Browser Support

Feature
Chrome
Edge
Firefox
Safari
width and height11211

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8.

CSS Width and Height exercises

A width

Make the box 250px wide.

The HTML your code works on
<div class="box">Box</div>
<span class="tag">Tag</span>

Result

Check your answer to see it here.