Box without a border wrapping around it

I can't seem to figure out why the border I applied to a child div is not completely enclosing the element. I created two boxes using div tags and added a border to one of them.

    #box {
      height: 500px;
      width: 500px;
      background-color: orange;
    }
    
    #upper-half {
      height: 50%;
      width: 100%;
      padding: 0px;
      border: 5px solid black;
    }
       <html>
      <head>
        <meta charset="utf-8">
        <title>Height and width percentages</title>
        <link rel="stylesheet" href="practice.css"/>
      </head>
      <body>
        <div id="box">
          <div id="upper-half">
          </div>
        </div>
      </body>
    </html>

The border doesn't fully enclose the "upper half" box.

To investigate this issue, I used the browser's page inspector and found that the dimensions of the box are 509.2 x 259.2.

I have a few questions regarding this matter:

  1. Why is the box size 509.2 x 259.2 even with zero padding?

  2. Why isn't the border completely enclosing the box?

  3. Why does the border touch the left side, top, and bottom of the box as expected, but not the right side?

Answer №1

Consider adding box-sizing: border-box;

The default setting for box-sizing is content-box, which adds the width of borders to the content width. By changing it to border-box, it will include the total width with borders.

    #box {
      height: 500px;
      width: 500px;
      background-color: orange;
    }
    
    #upper-half {
      box-sizing: border-box;
      height: 50%;
      width: 100%;
      padding: 0px;
      border: 5px solid black;
    }
       <html>
      <head>
        <meta charset="utf-8">
        <title>Height and width percentages</title>
        <link rel="stylesheet" href="practice.css"/>
      </head>
      <body>
        <div id="box">
          <div id="upper-half">
          </div>
        </div>
      </body>
    </html>

To learn more, visit this helpful tutorial

Answer №2

Remove the width:100% property from the inner box. Take a look at the example provided below.

#box {
  height: 500px;
  width: 500px;
  background-color: orange;
}
    
#upper-half {
  height: 50%;
  

  border: 5px solid black;
}
<div id="box">
  <div id="upper-half">
  </div>
</div>

Answer №3

Your inquiries may be a bit perplexing as you have solely defined one element: #box. However, I will make an attempt to address them:

  1. Based on your CSS specifications, your #box has dimensions of 500px x 500px.
  2. The reason why the #upper-half does not fully enclose #box is because it has been set to 50% x 100% of the containing div, which is #box, resulting in dimensions of 250px x 500px.
  3. It's crucial to note that the height and width of the border are not factored into the total calculations, hence why it extends beyond the anticipated 50% x 100%. The border aligns with the top and left sides of #box by default since objects in the DOM are typically aligned in this manner. The lack of alignment with the bottom or right edges is due to the absence of specific instructions indicating such alignment.

To address this issue, as suggested by @Damian Peralta, applying box-sizing: border-box; to #upper-half can effectively confine it within #box.

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

Space between elements in a horizontal arrangement

While working with Bootstrap 5 and using paddings in columns, I encountered issues when some of my columns had backgrounds or when adding an embed element. How can I create margins in such cases? https://i.sstatic.net/EhF7y.png To address this issue, I i ...

Automatically filling out a Pug form using an Express/Node.js web application

My Node.js web app is created with Express and I am attempting to populate a Jade template after querying MongoDB. The goal is to fill the Jade template with the queried values. One thing that puzzles me is that even when I remove everything within the co ...

Align boxes in the center within a DIV container

Here is the box design that I have created: https://i.sstatic.net/3rtcn.jpg The green color boxes are generated dynamically inside a "col-md-10" div. If there are less than 3 boxes in the second row, I would like to center align them. For example, in thi ...

Adjusting HTML/CSS for various screen sizes and devices

I have designed a website, but when it is viewed on devices like iPad or iPhone, the layout does not adjust to fit the screen properly. The text box containing my name and social media buttons appears on the left side, while the background image is positio ...

Issues with HTML keycodes not functioning properly in Firefox

Here is the code I am using: function restrictInput(e) { var charCode = (e.which) ? e.which : ((e.charCode) ? e.charCode : ((e.keyCode) ? e.keyCode : 0)); if((charCode < 48 || charCode > 57) && ( ...

Having trouble with SCSS styles not being applied after refactoring to SCSS modules?

Currently, I am in the process of restructuring an application to ensure that component styles are separated from global styles using CSS modules. However, I have come across an issue where the styles are not being applied correctly. The original code sni ...

Issue with font-size changes using css variables in Angular not updating in browser for specific fields

Utilizing CSS variables, I have implemented a feature that allows users to adjust the font size to small, medium, or large. While this functionality works correctly for most fields, there are certain instances where the value is applied but not displayed. ...

Using my function to iterate through all 'li class' elements on an eBay Scraper page

Just getting started with Python and BeautifulSoup. I'm facing an issue with implementing my function on all the list items (li class) on a specific eBay page that shows sold items. Each sold listing appears as an li class. Here is the URL for refer ...

Removing the gaps within table cell contents

Is there a way to eliminate the border space between cells in a table? Despite using cellspacing, I still see a thin white border. Any suggestions on how to fix this? <body> <center><table style="border-collapse: collapse; width:400px; bo ...

Unable to display scrollbar when generating dynamic content with jquery ajax

I have a jQuery report where I am generating dynamic HTML content (nested divs, span, label) using JSON. The report utilizes jQuery, mCustomScrollbar, commons, and jQueryUI. When I have a <div>...//some static code </div>, everything works per ...

Unable to retrieve input values from the text fields using JavaScript

In my project, I have created two PHP pages - "doc.php" and "chkval.php". The issue I am facing is that the textfield values are not being captured in the "chkval.php" page using $POST method. An error that I encountered is: Use of undefined constant re ...

Move the scroll bar away from the scrollable div and reposition it on a different part of the page using CSS

Is it possible to use CSS to position the scrollbar of a div that takes up the left half of my page with overflow-y: scroll to be where the page scrollbar would normally be (far right side of the page)? I've experimented with the ::-webkit-scrollbar ...

I'm still unclear on the necessity of using clear:both

Presenting a simplified example of my previous query, I am still seeking clarification regarding the necessity of using clear:both to enable padding-top. Can someone provide a straightforward illustration to elucidate the usage of float and clear? Hence, ...

Preventing Context Menu from Appearing on Long Click in HTML5 Games

I'm attempting to utilize a similar technique as demonstrated in this stackoverflow post: How to disable the 'save image as' popup for smartphones for <input> However, I am encountering an issue where my button is not displaying at ...

How to Set a Button as Inline Text in a React Native Component

Below is the code snippet I am working with utilizing the Text and Button components provided by react-native-paper: <Text>See also </Text> <Button mode="text" compact onPress={this.nav( name )}>Compass</Button> <Text&g ...

relocate HTML components

I've been trying to reposition HTML elements within the body of a webpage, but I'm struggling with getting a form tag to appear exactly in the center of the browser - and only that form tag. Can anyone offer guidance or suggest relevant topics fo ...

Tips for effectively sizing a logo within a Bootstrap navbar

<!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Bootstrap CSS --> <link ...

Issue with SVG on tainted canvas causes IE security error when using toDataURL

In my Angular JS directive, I have implemented a feature to export SVGs to PNG. While this functionality works seamlessly in most browsers, it encounters a security error in IE. Despite my numerous attempts to troubleshoot the issue, I have been unable to ...

Accordion button refuses to compress

After following all the steps in my accordion lesson, I encountered an issue where my button refuses to collapse when clicked. Can anyone provide guidance on how to solve this problem? Adding a class of "show" allows the content to display, but it still d ...

Is it possible to set a unique identifier in Vue.js that persists even after the page is reloaded?

Currently, I'm working on a small project diving into Vue js - a Habittracker. Unfortunately, there is a bug in my code. When the page is reloaded and new habits are added, the function that should change the background doesn't work as expected. ...