Skip to main content

How to Make an Element as Wide as Its Container with width: 100% in CSS

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

width: 100% makes an element as wide as its container's content area. On a text box it comes with a catch: the box's own padding and border are added on top, so it sticks out. box-sizing: border-box fixes that.

Syntax​

Syntax
<element> {
width: 100%;
}
PartWhat it does
<element>The element to stretch, such as input.
100%The full width of the container's content area, the part inside its padding.

MDN's width page says a percentage defines the width as a percentage of the containing block's width.

The Example​

The example is a search panel, 300 pixels wide with 8 pixels of padding and a light grey border, holding a line of text and a text box. The text box should fill the panel.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>width: 100%</title>
<style>
.panel {
width: 300px;
border: 1px solid #d1d1d1;
padding: 8px;
}

input {
width: 100%;
}
</style>
</head>
<body>
<div class="panel">
<p>Search the menu:</p>
<input value="Coffee">
</div>
</body>
</html>

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: a panel with a thin grey border holding Search the menu: and a text box with Coffee that runs right up to the panel's right border, with more space on its left than on its right.

The text box stretches across the panel, but it reaches the panel's right border while there are 8 pixels of space on its left. The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

The box is 308 pixels wide, not 300. width: 100% made its content area 300 pixels, the width of the panel's content area, and Chromium added the box's own padding, 2 pixels on each side, and its border, another 2 on each side.

Step 3: Include the Padding and Border in the Width​

Add box-sizing: border-box to the input rule, so the file looks like this:

index.html, the same page with box-sizing border-box added to the input rule, under width 100 percent.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>width: 100%</title>
<style>
.panel {
width: 300px;
border: 1px solid #d1d1d1;
padding: 8px;
}

input {
width: 100%;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="panel">
<p>Search the menu:</p>
<input value="Coffee">
</div>
</body>
</html>

Step 4: Reload the Page​

Save the file and reload the page in your browser:

The page in Chromium: the same panel with the text box now fitting inside it, with the same space on its left and right.

What Happened​

  • The text box is now exactly 300 pixels wide, the width of the panel's content area, and it ends where the panel's padding starts.
  • box-sizing: border-box made the width include the padding and the border, so 100% means the whole box, not only its content.

Common Mistakes​

MistakeWhat happensDo this instead
Using width: 100% on a text box without box-sizing: border-boxIts padding and border are added on top: the box was 308 pixels wide in a 300-pixel areaAdd box-sizing: border-box
Setting width: 100% on an inline element, such as a spanInline elements ignore width: the span stayed 32 pixels wideUse a block element, or give it display: block

Troubleshooting​

SymptomLikely causeFix
The element sticks out of its container on the rightIts padding and border are added to the 100%Add box-sizing: border-box
width: 100% has no effectThe element is inlineMake it a block

FAQ​

100% of what?​

Of the container's content area. The panel was 300 pixels wide inside its padding, so width: 100% meant 300 pixels, not the 318 pixels from border to border.

Do I need width: 100% on a paragraph or a div?​

No. Block elements such as p and div already fill their container's width. width: 100% matters for elements that don't, such as text boxes.

Conclusion​

Use width: 100% to stretch an element across its container, and add box-sizing: border-box when the element has padding or a border, so it fits exactly.

Additional Resources​