DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk2 min

Why Use box-sizing: border-box?

With box-sizing: border-box, declared dimensions include content, padding, and border—helping elements fit their layout without growing beyond the width you set.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use box-sizing: border-box when you want an element’s declared width and height to include its content, padding, and border. Padding and borders then fit inside the dimensions you set, while margin remains outside.

What changes between content-box and border-box?

CSS uses content-box as the initial value. With that model, a declared width measures the content area; padding and borders are added outside it. With border-box, the declared width measures the combined content, padding, and border. Margin is outside the measured box in both models.

Model What declared width measures Effect of padding and border
content-box Content area They increase the rendered width beyond the declared width.
border-box Content, padding, and border together They use space within the declared width, leaving less room for content.

For example, MDN’s box-sizing example sets an element’s width to 350px and gives it a 10px border. With content-box, the rendered width is 370px before any margin is added. With border-box, the total width stays 350px; the content area becomes narrower to make room for the border and any padding.

Why does adding padding make an element wider?

If the element uses content-box, its declared width applies only to its content. Padding and border are added outside that width, so the element’s rendered size grows. Switching to border-box keeps those parts within the declared dimension, which is useful when an element must fit a fixed column or component width.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

For instance, four elements each set to width: 25% can exceed the row’s available width under content-box when they also have padding or borders. With border-box, those additions are included in each element’s 25% width.

How to apply border-box broadly

MDN documents this inheritance pattern for setting the value on the root element and letting elements and pseudo-elements inherit it:

html {
  box-sizing: border-box;
}

*,
*::before,
*::after {
  box-sizing: inherit;
}

To check why an element’s size differs from what you expect, inspect it in your browser’s developer tools and look at the box model view. It shows the content, padding, border, and margin areas separately, as described in MDN’s box model guide.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When border-box may not be the right choice

border-box does not remove padding or borders. They consume part of the declared dimensions, so the content area shrinks. The content box cannot become negative; MDN notes that it is floored at zero.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Choose content-box when you want width and height to describe the content independently of padding and borders. MDN notes this can be useful for some relatively or absolutely positioned elements when positioning values should remain independent of changes to padding and border.

The distinction matters for elements whose sizing is controlled by width or height. Ordinary inline boxes do not respect those dimensions in the same way as block boxes, because box-model behavior depends on display type.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.