What is the reason behind the child div exceeding the boundaries of the parent div? I am seeking a detailed explanation rather than just a quick fix

I've designed a basic example to illustrate a point.

#container {
  height: 60%;
  width: 50%;
  border-style: double;
}

#inset {
  padding: 2%;
  border-style: groove;
  height: 100%;
  width: 100%;
}
<div id="container">
  Parent
  <div id="inset">
    Child
  </div>
</div>

I'm curious why the child div doesn't simply take up the remaining space of its parent div with both height and width set to 100% (Even without any text, the child div overflows).

I've noticed many people asking the same question, but they receive solutions rather than explanations. I'm working on mastering CSS, an area where I feel I lack expertise.

This is how I envisioned it would appear: https://i.sstatic.net/OE4Hv.png

Answer №1

The reason for this behavior is that according to HTML specifications, an element's rendered size is calculated based on its content or specified size in addition to any padding and borders it has. This means that a div with a 50px width, 2px vertical border, and 10px horizontal padding will be displayed as a 74px-wide element.

With the CSS property box-sizing, you can change how the size of an element is calculated. By setting it to border-box, the algorithm includes the paddings and borders as part of the element size. In the example given, the rendered width would remain at 50px.

In regards to your question, your "inset" div is set to be 100% wide, which means it spans the full width of its container but also includes the additional horizontal padding of 2% and vertical borders in its overall width.

You can find more information on this topic - along with a potentially more precise explanation - here: https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing

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

Tips on adjusting the body or HTML to fill the entire page

Currently, I am designing the homepage using the bootstrap Cover page template. The images displayed are just placeholders. While the site functions perfectly on desktop screens, resizing it to smaller sizes causes the grid layout to collapse into a single ...

Looking to leverage iframes in your Javascript code?

Currently, I am implementing a JSP popup window using JavaScript with an iframe to display a table of multiple records. However, when I disable the table of multiple records, it does not prevent the onclick function (hyperlink). The code snippet is provid ...

There seems to be a Javascript TypeError occurring - it looks like the function document.routeEvent

I am currently working on an old webpage that contains JavaScript. However, there is a function that does not seem to work properly with newer browsers such as Mozilla, Chrome, and Safari. Interestingly, the page functions perfectly fine on Internet Explor ...

Is there a way to alter the appearance of an HTML element without an ID using a JavaScript function?

I have a specific goal in mind, but I'm struggling to articulate it concisely. Despite conducting extensive research, none of the suggested solutions seem applicable in my case. Objective: I aim to change the background color of a div based on the da ...

Glitchy/Crazy CSS3 Animations

Currently, I am developing a website at . One of the features I have implemented is CSS3 transitions for route changes, but this feature only works in Chrome. Here's how the animation works: I apply the .preanimate class to rotate the phasing out di ...

Show picture inside a table cell with Jquery jTable

I have implemented the JQuery jTable plugin to showcase my table data. $(document).ready(function () { //Setting up the jTable $('#PeopleTableContainer').jtable({ title: 'List of individuals', p ...

Are there any performance implications when using an excessive number of background images in CSS?

My website seems to be running too slowly based on user reports. I suspect that the background images in CSS may be causing the issue. The site uses a custom build system to concatenate and compress all CSS, create CSS sprites automatically, resulting in ...

How can I deliver assets in React without the PUBLIC_URL showing up in the path?

I have set up a portfolio website that includes my resume. I am trying to make it so that when someone visits the route http://localhost:3000/resume.pdf, they can view my resume directly. The resume.pdf file is located in my public folder. However, instead ...

Restricted footage accessible through RESTful API

Hey there! I'm currently working on implementing authentication protected audio/video streaming in an Angular app using REST API. The main objective is to ensure that the audio/video content is secure and not accessible to users who are not logged in. ...

Ways to display a confirmation message prior to deleting?

When deleting, I would like a confirmation message to appear upon clicking the delete button or image. If the user chooses 'Ok', then the deletion will proceed; however, if 'Cancel' is selected, nothing will happen. I attempted to disp ...

Guide on uploading multiple images using AJAX and jQuery

Here is the code snippet I am currently working on, which allows users to upload one or multiple files and delete them. All the file data is stored in the variable form_data. Unfortunately, I have not been successful in getting the file upload functionali ...

Adding a background behind the currency symbol before the textbox field

I need to customize a text field like the one shown in the image below: https://i.sstatic.net/gu3JA.png After making some quick adjustments, I ended up with the following result. My main concern is now the background of the currency symbol. https://i.ss ...

Unable to create a border that spans from left to right

Struggling to create a border around icons on my website from left to right. Here's what I'm aiming for: However, the CSS code I have been using doesn't seem to be working properly. This is the outcome I am getting. Any assistance in figur ...

Keep moving the box back and forth by pressing the left and right buttons continuously

Looking to continuously move the .popup class left and right by clicking buttons for left and right movement. Here is the js fiddle link for reference. This is the HTML code structure: <button class="right">Right</button> <button class="l ...

Is there a method in AngularJS to automatically set app.comment to null if the point is not equal to 1 using front end logic?

Can we verify on the front end in AngularJS if app.point !=1 and app.comment==null? <td> <input type="number" max="5" min="1" ng-init="app.point=5" data-ng-model="app.point"> </td> < ...

How can I create an HTML select dropdown menu with a maximum height of 100% and a dynamic size?

The dropdown menu I created using an HTML select tag contains a total of 152 options. However, the large number of options causes some of them to be out of view on most monitors when the size is set to 152. I attempted to limit the number of displayed opti ...

Stack Divs Without Using Absolute Positioning

Attempting to overlay divs without using absolute positioning has been quite a challenge. The issue arose when trying to integrate a Paypal cart through Javascript on the website. By default, the cart was positioned closely to the top margin of the webpage ...

The columnFilter plugin in Datatables is failing to initialize

I have a pre-existing table that needs to be customized and initialized properly. <table id="currencies-table" class="table table-striped table-bordered table-hover form-data-table"> <thead> <tr> <th style="width: 10px;" ...

Embedding one embed on top of another using wmode set to direct positioning

I'm struggling to place a flash banner above a gpu accelerated video player. I vaguely recall reading about the importance of wmode direct, but can't remember why it's crucial. Is there a way to position the banner over it without being abl ...

Navigating a Dynamic entity using a button in AFrame

I've inquired about this topic previously, but I feel my question wasn't clear. My goal is to construct a plinko-style aframe world with a ball that resets to its starting position when clicked. While I prefer to use a button to reset the ball, c ...