How to Make an Element as Wide as Its Container with width: 100% in CSS
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
<element> {
width: 100%;
}
| Part | What 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:
<!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 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:

<!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:

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-boxmade the width include the padding and the border, so100%means the whole box, not only its content.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using width: 100% on a text box without box-sizing: border-box | Its padding and border are added on top: the box was 308 pixels wide in a 300-pixel area | Add box-sizing: border-box |
Setting width: 100% on an inline element, such as a span | Inline elements ignore width: the span stayed 32 pixels wide | Use a block element, or give it display: block |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The element sticks out of its container on the right | Its padding and border are added to the 100% | Add box-sizing: border-box |
width: 100% has no effect | The element is inline | Make 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
- width, on MDN.
- box-sizing, on MDN.
