How come the width factor encompasses the border when the box-sizing is set to content-box?

My HTML file contains the following code:

<html lang="en" class=""><head>

  <meta charset="UTF-8">
  <title>CodePen Demo</title>

  <meta name="robots" content="noindex">

  <link rel="shortcut icon" type="image/x-icon" href="https://static.codepen.io/assets/favicon/favicon-aec34940fbc1a6e787974dcd360f2c6b63348d4b1f4e06c77743096d55480f33.ico">
  <link rel="mask-icon" type="" href="https://static.codepen.io/assets/favicon/logo-pin-8f3771b1072e3c38bd662872f6b673a722f4b3ca2421637d5596661b4e2132cc.svg" color="#111">
  <link rel="canonical" href="https://codepen.io/hardkoded/pen/VwjvWBm">
  
  ... (skipping some code for brevity) ...
  
  
  ... (skipping some code for brevity) ...

  
  </script>


</body></html>

This is actually a modified Tic Tac Toe game from this CodePen link: https://codepen.io/sophiebits/pen/qNOZOP. I made some changes to it.


When inspecting one of the squares in Chrome, I noticed that the computed CSS box model showed a content width of 32px, even though I set the square's width to be 34px in the CSS. Shouldn't the computed width match what was specified in the CSS?


I'm aware that the default value of box-sizing is content-box, meaning the width property should exclude any border or padding. So why is there discrepancy between the specified width and the computed width of the square?

https://i.sstatic.net/WxcO6.png

Answer №1

Buttons (as well as some other elements) are designed with a box-sizing property of "border-box" rather than "content-box," which is why they behave differently.

If you need more information, check out the documentation here: https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing

To modify this in your CSS code, use:

.square{
   box-sizing: content-box; /* border-box */
}

Answer №2

Take a look at this straightforward JSFiddle example: https://jsfiddle.net/3wr8n9u2/

<div class="container"></div>
.container {
  background: green;
  border: 4px solid purple;
  width: 15px;
  height: 15px;
}

In my opinion, the height is just right!

https://i.sstatic.net/LmPtS.png

To make it easier for us to determine the issue, consider providing a more concise example in your query.

Nevertheless, as mentioned in another response, the challenge may lie in certain elements having a different default value for box-sizing.

Answer №3

Essentially, the concept of box-sizing is what determines whether an element's width should include its border or not.

For instance, if you have a div with a width of 300px and you apply box-sizing: border-box like this:

div {
  width: 300px;
  height: 100px;
  border: 10px solid blue;
  box-sizing: border-box;
}

The width of the div will always remain at 300px - accounting for 20px of border and 280px of content. Any adjustments to padding or border size will impact the content area.

On the other hand, using box-sizing: content-box like so:

div {
  width: 300px;
  height: 100px;  

  border: 10px solid red;
  box-sizing: content-box;
}

This would result in the div having a width of 320px - including 300px of content and 20px of border width.

To maintain a specific width or height for your element, such as 50px, utilizing box-sizing: border-box would be the way to go.

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

What is the best way to extract the content within a nested <p> element from an external HTML document using the HTML Agility Pack?

I'm currently working on retrieving text from an external website that is nested within a paragraph tag. The enclosing div element has been assigned a class value. Take a look at the HTML snippet: <div class="discription"><p>this is the ...

Revamping CSS for a Responsive Wordpress Tesseract Theme

I'm currently in the process of developing a website using the Tesseract theme at thevandreasproject.com. However, I have encountered an issue with responsiveness when integrating the SiteOrigins PageBuilder plugin. The compatibility between PageBuild ...

Tips for properly aligning blockquote opening and closing quotation marks

We are currently implementing custom styles for beginning and end quotes in a string using the <blockquote> tag. However, we are having issues with the alignment, as the opening quote appears before the text and the closing quote is at the end of the ...

Adjusting the spacing between dropdown menu items in Bootstrap sass with Rails

After adding the bootstrap-sass gem to my app, I have implemented two dropdown menus on the right side of the screen. Although they are horizontally aligned, I find that they are too close to each other. I would like to create some space between them and ...

Incorporating a custom object into two dropdown menus using JavaScript

As a novice in the world of JavaScript, I am attempting to dynamically add values from a text box to two select boxes. Upon entering a value (node name), my goal is to include that name in both select boxes srcNodes and targetNodes. However, I'm enco ...

Swap out original source files in HTML with minified versions

I have successfully utilized a maven plugin to create a minified and compressed version of my CSS and JavaScript files. Now, I am looking to update the section in my main HTML page that currently loads all those individual files. Here is what it looks lik ...

AngularJs fails to render CSS styles

I'm encountering a small issue with my angularJS code. I am attempting to showcase JSON data that includes both CSS and HTML code. However, on my website, all that is displayed is a hard-coded form of HTML and CSS (resembling the code below). I have t ...

In ReactJS, ensure only a single div is active at any given moment

I'm working on a layout with three divs in each row, and there are multiple rows. Only one div can be selected at a time within a row, and selecting a new div will automatically unselect the previously selected one. Here is a simplified version of my ...

Trigger the onClick event of an element only if it was not clicked on specific child elements

<div onClick={()=>do_stuff()}> <div classname="div-1"></div> <div classname="div-2"></div> <div classname="div-3"></div> <div classname="div-4"></div> ...

Trigger the opening of a bootstrap modal from an external source on the current page

One common question is how to load a modal from another page onto the current page, or how to open a modal on the current page when it loads. My idea is a little different: I want to click on an image hotspot (like a person in a team photo) on the /home p ...

When I update URLs in Django, my CSS breaks down

Recently, I encountered an issue on my website and found a solution by creating two separate HTML pages. However, when I modified the urls.py to differentiate the URLs for these pages, the CSS stopped working. Below is my code snippet and a detailed explan ...

Tips on quickly validating a form using JavaScript

I've set up a form with 3 text inputs and a validation function that triggers on submit. Here's a snippet of the code: <form action="" method="post" onSubmit="return checkForm(this.form)"> and checkForm(): var pattern_name = /^([a-zA-Z ...

Unique 15-character code following email address

I've noticed a strange 15-character code that keeps attaching itself to the end of my outgoing emails. https://i.sstatic.net/tZdQo.png This mysterious code seems to change with each batch of emails I send out. Even though multiple emails sent in a l ...

Implementing Sass mixin transition to specifically add transition-delay - a comprehensive guide

As I continue to enhance my front-end development skills and practice Sass to optimize my CSS code, I encountered a roadblock. After exploring resources and tutorials online, I created a global mixin in Sass named 'transition'. Here is the code: ...

When using a CSS background image in ReactJS, a white space may appear at the bottom of the window

Currently, I am working on creating a background for a React website and implementing it in the div above component. My purpose is to have this background only appear on specific components and not throughout the entire website. However, I am encountering ...

PHP - display the modified page with the updates applied

I currently have an HTML page where users can input information and submit a form to change database information. Typically, the submit button calls a PHP file on the server to process the data. However, I am looking for a way for this PHP file to return t ...

The issue of innerHTML malfunctioning when multiple eventListeners are used in a JavaScript code for the "Type Ahead" feature

I am currently working on enhancing the "Type Ahead" code from the Wes Box Javascript30 course by adding my own modifications. In the original code, a single data source is used to update search suggestions based on user input. I have adapted the code to i ...

Issues arise when using jQuery to manipulate HTML content within an Asp.Net environment

Recently, I found myself in a puzzling situation with a div that has the attribute runat="server". <div id="results" runat="server" class="results"></div> Using jQuery, I added HTML content to this div. $('.results').html('Add ...

The cdkDropList in Angular's drag and drop feature does not support the application of element styles

Just wanted to share my experience in case it helps someone else out there! I've been working on an Angular project where I needed to create a Trello-like application. To enable dragging elements from one list to another, I installed the Angular cdk ...

After applying CSS, I am unable to click on the radio buttons

Even after selecting the other two, it still defaults to the first one. Here is my code: input[type=radio] { display:none; } input[type=radio] + label:before { content: ""; display: inline-block; ...