What is preventing two inline-block divs from fitting next to each other with a combined width equal to that of the parent container?

This problem is really puzzling me.

Although I can make it work by floating the elements, I am curious to know why this issue is occurring.

Check out the solution

Here is the code snippet:

.container {
  width: 1000px;
  height: 400px;
  background-color: purple;
  position: relative;
}

.item {
  height: 100px;
  display: inline-block;
}

.item.left {
  width: 70%;
  background-color: green;
}

.item.right {
  width: 30%;
  background-color: orange;
}
<div class="container">
  <div class="item left"></div>
  <div class="item right"></div>
</div>

Answer №1

Explanation for why they do not fit side by side is due to the sensitivity of inline elements to white space in the code. To make them line up on the same line, remove the space between the divs:

.container {
  width: 1000px;
  height: 400px;
  background-color: purple;
  position: relative;
}

.item {
  height: 100px;
  display: inline-block;
}

.item.left {
  width: 70%;
  background-color: green;
}

.item.right {
  width: 30%;
  background-color: orange;
}
<div class="container">
  <div class="item left"></div><div class="item right"></div>
</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

The margin-left negative is not functioning when setting the left side at 50%

I'm having trouble getting this specific CSS to work on an element: .element { position: absolute; left: 50%; margin-left: -285px // the element has width: 500px; } This element is contained within another div that also has an absolute positio ...

Step-by-step guide on achieving 100% height for a child element within a parent using Flexbox

Currently facing an issue where trying to make a child element take up 100% of the remaining height of its parent container causes the child's height to go beyond the boundaries of the parent. HTML .container { height: 340px; /* background-i ...

Combine classes

Here's an example of some "LESS" code: .my_class{ color: #000; font: 12px/12px Arial; &_comething_else{ color: #f00; } } After compilation, it turns into this: .my_class{ color: #000; font: 12px/12px Arial; } .my_class_som ...

Use jQuery to swap out two div classes within a table cell (TD) if they are

Although I'm making progress, I must confess that jQuery and CSS are not my strong suits. The objective: To create a dynamic div within a table data cell for a calendar feature. The content of the div varies based on the date range input. A filled d ...

"Error: Unable to locate CSS background image file: net::ERR_FILE_NOT_FOUND

I currently have a background image set for the bottom of the header as shown below: header { background:url(../img/header_bg.png) #ea6225 no-repeat; background-size: 100% auto; background-position:left bottom; } The images are located in a folder ...

Issues with Radio Buttons and Checkboxes in Google Chrome

Recently, there seems to be an issue with radio buttons and checkboxes on my website after a Google Chrome update. I have not made any changes to the CSS in months and everything works fine on other browsers like Firefox, Safari, IE9, and IE10. If you wan ...

What is the solution to preventing Prettier in VS Code from breaking up CSS Selectors onto different lines?

I am trying to achieve this code style: h1,h2,h3,h4,h5 { font-weight: 400 !important; } However, Prettier - Code Formatter is splitting each selector into separate lines like this: h1, h2, h3, h4, h5 { font-weight: 400 !important; } The issue of CSS ...

api for enhancing images in Laravel app through preview, enlarge, and zoom functionalities

As I work on my website, I aim to display images in a compact space, such as within a 300x300 <div>. What I envision is the ability for users to preview or enlarge these images upon clicking, allowing for a closer and more detailed view. For exampl ...

Bootstrap dropdown menu fails to adapt to updated navbar height

I recently made a modification to the navbar size on my website, increasing it from 50px to 63px by tweaking a line in the bootstrap.css file. The exact code snippet I utilized for this adjustment is as follows: .navbar { position: relative; min ...

Mobile media queries are ineffective despite implementing the viewport meta tag

I am facing an issue that even though I have tried various solutions, my media queries are not working on mobile devices after adding the viewport meta tag. Can anyone provide insight into why this might be happening? Update: Upon further investigation, I ...

exclude the outer border from the last row of the table

Is it possible to create a table with only an outer border and exclude the bottom row, so that the bottom border of the second last row gives the appearance of being the final row? I am limited to using CSS as this is on Wordpress and I cannot make changes ...

Adding Materialize CSS to an AngularJS project: A step-by-step guide

Currently, I am utilizing Yeoman to work on a brand new website using angularJS. Even after attempting bower install materialize and bower install angular-material, no changes are reflected in the design and functionality of the site. Additionally, I have ...

Nested divs with inline formatting

On my HTML page, I have the following structure: <div id="frst"> FIRST </div> <div id="top"> <div id="mid"> <div id="bottom"> <ul class="menu"> <li>A</li> ...

Is it possible to utilize CSS alone to change the color of a certain image to white against a black background?

I specialize in crafting dark theme versions of popular websites using CSS. One challenge I often face is dealing with images like the ones below: https://i.sstatic.net/ZLpPT.png https://i.sstatic.net/2NKRP.png Websites typically use background-image t ...

Issue with CSS for Pagination and Content Display

When I print an HTML page, I want to include custom text in the header of all printed pages. @page { margin-top:5mm; margin-bottom: 25mm; margin-left: 30mm; margin-right: 30mm; @top-right{ content: "Page title"; /* Page title ...

Looking for a reliable tool to check your CSS classes and tidy up your code effectively?

It seems like a common issue with a straightforward solution. I have numerous CSS selectors scattered across various files, many of which may be unnecessary and a directory full of HTML files. My goal is to identify all redundant selectors in my CSS within ...

Harnessing the power of the Zurb Foundation 4 grid system to create visually appealing list elements

When it comes to styling the layout of a page, Foundation 4's grid system with its mobile-first approach is a no-brainer, especially if you're already using other foundation elements like textual content, images, and sidebars. On the example pag ...

The self-align class in Bootstrap flexbox is not functioning correctly

I'm currently experimenting with creating a unique blog post layout using Bootstrap. My goal is to have the blog image on the left side, the title on the right side, and the author's name below the title. However, I've encountered an issue w ...

What is the point of the horizontal scroll bar in WP Thesis?

There seems to be a horizontal scroll bar appearing at the bottom of my site, even though there is no content extending beyond the visible area. Can anyone provide guidance on how to fix this issue? My website is built on Wordpress 3.0 and utilizes the Th ...

"Make your slides smooth and responsive with the unslick option in slick

Currently implementing the Slick Slider on a WordPress website. The slider is designed to display 3 columns at a screen size of 1024px and above. When the screen size drops below 1024px, the slider adjusts to show 2 columns, and on mobile devices, it swit ...