Using the box-sizing property to give two inline-block elements a width of 50% each

Within my parent div with a padding of 20px, I am trying to make two span tags each occupy 50% of the parent div's width and align on the same line. Despite using box-sizing: border-box, the issue persists.

HTML

<div>
  <span>foo</span>
  <span>bar</span>
</div>

CSS

div {
  border: 1px solid black;
  box-sizing: border-box;
  padding: 20px;
  width: 150px;
}

div span {
  box-sizing: border-box;
  border: 1px solid green;
  display: inline-block;
  width: 50%;
}

UPDATE:

It turns out that in this scenario, using box-sizing was unnecessary and following CBroe's solution resolves the issue effectively.

Answer №1

Your issue lies within the white space among the elements.

When using inline-block elements, they are positioned similarly to regular text. This causes any white space between one element's closing tag and the next element's opening tag to be condensed into a single space character based on standard HTML rules. This results in 50% + one space character + 50% becoming slightly more than 100%, causing the second element to break onto a new "line."

There are various methods to address this problem, such as removing the white space between the tags of the elements or setting the font-size to 0 for all content "outside" those elements. More detailed solutions can be found here: http://css-tricks.com/fighting-the-space-between-inline-block-elements/

Answer №2

There are numerous ways to accomplish this task (perhaps an exaggeration, but I can come up with at least 4).

The simplest method is to convert them into block elements (borders are included for better visual representation):

* { box-sizing: border-box; }
span { display: block; width: 50%; float: left; padding: 12px; text-transform: uppercase; border: 4px solid #000; background: #f00;}
div { border: 2px solid #0f0; overflow:hidden;}
<div>
  <span>first span</span>
  <span>second span</span>
</div>

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

Guide to creating an HTML table with bokeh

As a newcomer to Bokeh, I've been searching through the documentation for examples but haven't come across an in-built method for creating tables. I have some data that I need to display in tabular format and was wondering what the most straightf ...

What is the best way to combine two rows within a single cell using HTML?

Is there a way to create two rows within a single cell? Here is the code snippet that I came up with. I utilized the rowspan attribute to extend the cell width. <tr> <th rowspan="2">Questions</th> <th>sub1</th> <th& ...

"ExceptionThrownByMoveTargetOutOfBounds in Selenium WebDriver for IE9 and Firefox

Having trouble with the FireFox/IE9 driver in Selenium? When using the Actions class and its moveToElement method, I keep encountering a MoveTargetOutOfBoundsException error. Despite trying different solutions like Coordinates, Point, and javascriptexecuto ...

The <img> element is able to fill an entire div while maintaining its original aspect ratio

I am currently facing a challenge with implementing a responsive gallery slider in Bootstrap. In order to achieve responsiveness, I need to use properties like max-height. The issue arises from the fact that the images in the gallery can vary in size and ...

"Encountering a 404 error while attempting to forward to an HTML

I've been encountering an issue while trying to transition from a JSX page to an HTML page. Every time I attempt to do so, I receive a 404 error stating that the page doesn't exist. It's puzzling because it is clearly present in my files and ...

Image link in HTML / CSS not functional on one half of the image

My webpage has an image thumbnail with accompanying text positioned to the right. I have a hyperlink on the image that should open the full-size version when clicked. However, there seems to be an issue where the hyper link only activates on the lower hal ...

Tips for creating a React Table where one element spans the entire cell

Hello, I am trying to make my td element tag occupy the entire cell belonging to that column. The current output I have is shown in this screenshot: https://i.sstatic.net/erPJC.png Essentially, I want the "Independent Study" to take up the first column. H ...

The Json script in a Chrome extension will only execute the first time the URL is inputted

I've been working on developing a Chrome extension that utilizes CSS and HTML elements to block specific sections of the YouTube page. The problem I'm encountering is that the Json script only applies the CSS files correctly the first time I visi ...

Rendering a prototype and setting its display to none

I have created a slideshow with the following code: <div class="row-3"> <div class="slider-wrapper"> <div class="slider"> <ul class="items"> ...

Design the border of the highlighted section within the table using CSS

Is there a way to access the edge (top, left, right, bottom) width css for a selectable table using jQuery UI selectable, or do I need to use JavaScript? Update: By "accessing the edge," I mean creating a border around a selected area in a table (selectin ...

The background image is trapping my text and preventing it from moving forward

As a beginner developer embarking on my first web page project, I've encountered a challenge. The text I inserted into a flexbox refuses to align beneath the image. .title { text-align: center; width: 500px; margin-left: auto; margin-right: a ...

Ways to take out an object from the scene in three.js

As a complete beginner in three.js, I am currently working on a small website and facing an issue. Everything is functioning correctly, but I am struggling to remove an OBJ from the scene. How can I achieve this? Specifically, I would like the OBJ to disa ...

What is the procedure for displaying the complete text for choices on an ionic select button?

On my page, I have included an Ionic select button. The problem is that the options display a long string, and I want them to show the full text without truncating with dot-dot-dot like this: This is how my code looks: <ion-item *ngIf="select == &apos ...

Select a color by inputting the type as "color" while leaving a space between the border and

I'm having an issue with the color input There seems to be a gap between the black border and the actual color, but I want to get rid of it #color-picker { border: 5px solid black; border-radius: 5px; width: 207px; height: 60px; padding: ...

A guide on obtaining meta tag content values and calculating their average

Currently, I am in the process of developing a website that makes use of Schema.org microdata to provide structured content for product listings. One thing I have come across is that Google requires an aggregate rating based on all the reviews. How can I e ...

The appearance of the Bootstrap button is not displaying correctly

My current button looks like this: https://i.sstatic.net/PyP8v.png As seen in the image above, there is something strange at the back of the button. How can I resolve this issue? I am working on a small project using Django and Bootstrap. Thank you in a ...

Store input values for increment and decrement in local storage upon page reload

Greetings all! I'm uncertain if this question has been raised previously on this forum. My current objective is to modify the quantity value of each item in my shopping cart by clicking the plus and minus buttons, and then saving that updated value ...

How can I style my text using CSS with a linear gradient, shadow, and outline effect

Can text be styled with a linear gradient, shadow, and outline all at once? I'm running into an issue where setting "-webkit-text-fill-color: transparent" for the gradient causes the shadow to appear on top of the text. Is there a way to have the te ...

Attempting to extract information from website, successful on majority of sites but encountering issues on this particular one

We are looking to extract dividend data specifically from the Nasdaq exchange similar to this Google Sheets example. While most other URLs work without any issues, the query for this one seems to be failing. <?php $ch = curl_init(); curl_setopt($ch, C ...

Issue with ng-model causing incorrect variable binding

My issue lies with a variable that I have connected to a select input through the use of ng-model. Strangely, the variable seems to not update with the correct data and retains the value assigned to it during initialization. I have a similar setup elsewhe ...