What is the reason behind padding increasing the apparent width of an element, despite having box-sizing set to border-box?

While working with Semantic-ui, I encountered an issue with toggle boxes placed next to each other in a flexbox container. When the window size is reduced, the boxes wrap around so that one is positioned on top of the other.

To create some spacing between them, I added both right and bottom padding of approximately 5px. However, I noticed a peculiar behavior. The padding caused the boxes to separate horizontally but not vertically when stacked, despite having bottom padding on each box.

Upon further investigation, I discovered that the check boxes had their box-sizing property set to border-box. It turns out that the border-box box model calculates width and height to include padding and border.

The checkboxes were assigned a height of 1.5rem.

My question arises from this situation. My understanding was that padding shouldn't alter the size of an element when using border-box. However, this only seems to hold true if definite dimensions are defined as shown in the provided jsfiddle. Since the height is specified, the bottom padding isn't considered an additional dimension. Yet, the visible width of the divs changes when applying right padding even without a specific width set.

Why does this occur? Shouldn't padding have no impact on the element's size (unless set excessively large) regardless of whether a definite width is determined or left to be calculated?

JSFiddle: http://jsfiddle.net/Astridax/8cd48emn/

I encourage you to experiment with toggling paddings using dev tools to observe the phenomenon firsthand.

Answer №1

From my understanding, when using the border-box property, padding should not affect the size of the element.

It seems there is some confusion here. The specification clarifies this issue: http://www.w3.org/TR/css3-ui/#box-sizing0

border-box

When using the 'border-box' value, the specified width and height (including min/max properties) define the border box of the element. This means that any padding or border applied to the element will be contained within these specified dimensions. The actual content width and height are calculated by subtracting the total padding and border widths from the specified values for 'width' and 'height'. To prevent negative dimensions, this calculation will not result in a negative number according to CSS specifications ([CSS21], section 10.2).

The key effect of setting the box-sizing property to border-box is that widths will include both the border and padding within the specified values. However, unspecified widths will behave as normal, adjusting to accommodate content, padding, and borders.

Edit:

Your suggestion sounds reasonable in theory but is actually impractical due to a fundamental limitation. Consider a situation where a div contains content with an intrinsic width of exactly 500px, followed by a 20px padding around it.

#myDiv {
    padding: 20px;
    width: auto;
}

Initially, the div would have a width of 540px assuming the default 'content-box' behavior.

Now, let's switch the box-sizing to border-box.

#myDiv {
    box-sizing: border-box;
    padding: 20px;
    width: auto;
}

If we simply ignore the padding in the calculations, the size of the div would constantly fluctuate between considering and excluding the padding area. This inconsistency highlights why accommodating your proposed adjustment leads to endless contradictions.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to display two menus on opposite sides of the screen while using Google Maps?

Currently, I am working on developing a Progressive Web App using Vue.js and Vuetify.js. My goal is to have two buttons overlaid on Google Maps - one on the left side of the screen to open a navigation drawer, and another on the right side to display user ...

Allow users to modify multiple rows in a customizable table with Flask

I am currently working on a web application that utilizes Flask/Python as the back-end with SQLite, and HTML/CSS/JS (Bootstrap) for the front-end. In one specific section of the application, users should be able to edit multiple fields simultaneously in a ...

Utilizing React to highlight buttons that share the same index value upon hover

I have some data in a JavaScript object from a JSON file, where certain entries have a spanid (number) while others do not. I've written React code to highlight buttons with a spanid on hover, but I'm looking for a way to highlight or change the ...

Create a sleek and uniform design with Bootstrap by setting up three columns that maintain the same height

I am attempting to design a 3 column div that maintains equal height for all responsive rows, each containing an ICON and information. <div class="container"> <div class="row row-eq-height"> <div class="col c ...

dynamic text overlay on a bootstrap carousel

Having limited knowledge in html and css, I am using a bootstrap carousel slider with text. I am trying to change the color of the box randomly. https://i.sstatic.net/TozUP.jpg Here is the code that I am currently using: <ol class="carousel-indicato ...

distinguish different designs for individual components in Angular 5

My project is divided into two main parts: one for public web pages and the other for an admin control panel. Each part has its own set of CSS and javascript files for custom templates. If I include all CSS and js files in index.html, they all load at the ...

How can the parent div's height be determined by the child div when using position: absolute?

I am struggling with setting the height of a parent div that contains a nested child div with absolute positioning. The child div has a fixed height of 652px, but I cannot seem to get the parent div to match this height. I have tried adjusting the clear an ...

Pattern to locate a CSS class identifier

I've been working on creating a regex to match valid CSS class name structures. Currently, my progress looks like this: $pattern = "([A-Za-z]*\.[A-Za-z]+\s*{)"; $regex = preg_match_all($pattern, $html, $matches); However, there are certai ...

Prevent click event listener from activating if the click results in a new tab or window being opened

Occasionally, when using my web app, I need to prevent click event listeners from activating if the click is meant to open a new window or tab. For instance, in my single page application, there are links to other content. While they function well, there i ...

Can anyone provide a solution for the issue with bootstrap utilities?

Currently, I am using Vue3 with Vite and attempting to integrate Bootstrap into my project. While I have managed to implement most of it successfully, I encountered a significant error when trying to override Bootstrap-related variables. Can anyone offer a ...

What strategies can I employ to prevent this bar from breaking?

How can I prevent my navigation bar from breaking when zoomed out? Below is the code snippet in question: <nav> <ul id="nav"> <li><a href="#">Home</a></li> <li><a href="#">What We Do</a></li> &l ...

Dealing with Sideways Overflow Using slideDown() and slideUp()

Attempting to use slideUp() and slideDown() for an animated reveal of page elements, I encountered difficulty when dealing with a relatively positioned icon placed outside the element. During these animations, overflow is set to hidden, resulting in my ico ...

Embed Google Maps inside a Twitter-Bootstrap container

I am currently working on integrating Google Maps into my web server. Since I am not proficient in Front-End development, I am facing challenges with bootstrap elements. However, I have managed to code the necessary components for Google Maps to function p ...

The Bootstrap navbar does not collapse as expected

Upon opening my HTML file, I am unable to locate the collapse button on the navbar. The collapsible button seems to be missing. Here is a snippet of my HTML code: <!DOCTYPE html> <html> <head> <title>BoostUp - Boost your ...

The <input type="number"/> element is failing to display a numeric keypad on iOS devices

Based on the information from Apple's official documentation, setting up an input element with a type of number should result in a number keypad being displayed. <input type="number"/> number: This creates a text field specifically for en ...

Can the underline height be adjusted when a developer uses `text-decoration: underline;` in CSS to generate an underline effect?

Can the height of the underline created using text-decoration: underline; in CSS be adjusted? I used CSS to apply an underline to the word "about" with an 'id' of '#title4' but I want to increase the space between the word and the unde ...

Having trouble making my if / else statement show the_header_image_tag on WordPress

Having some trouble getting my if / else statement to function correctly. I am using a WordPress function called '' and I want to apply different CSS styles based on whether it is used or not. My theme allows for the display of a banner, but onl ...

Designing templates for websites and applications using node.js

Simplified Question: As I delve into improving my skills with node.js, I'm exploring the concept of templating. How would using node to serve one HTML file and loading page-specific information through AJAX/sockets impact performance and design princ ...

The applet failed to load on the HTML webpage

I'm having trouble getting this applet to load on an HTML page. I've already added full permissions in the java.policy file and I'm using the default HTML file from NetBeans Applet's output. /* Hearts Cards Game with AI*/ import java.a ...

What is the method to display just the final 3 characters within a paragraph?

Is there a way to display only the last three characters of a string using either a method or a string method? I consistently have a 13-digit number, but I specifically require showing only the final three digits. Appreciate any assistance provided. Than ...