Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two divs inline block, why they are not staying next to each other?

Tags:

html

css

I have created two divs and I would like them to stay next to each other, but one is always going down even if they have display: inline-block. What is wrong? I don't understand.

.container {
  width: 200px;
  border: 1px solid black;
}
.a {
  display: inline-block;
  background: red;
}
.b {
  width: 20px;
  display: inline-block;
  background: blue;
}
<div class="container">
  <div class="a">hello hello hello hello hello hello hello</div>
  <div class="b">aaa</div>
</div>
like image 549
MeV Avatar asked Jul 13 '15 17:07

MeV


People also ask

What is the problem with using display inline-block to make div elements sit next to each other?

The display:inline-block makes both elements remain in the same line if there's enough space for it. Otherwise, the line will break.

Can we place two div blocks at same line?

To display multiple div tags in the same line, we can use the float property in CSS styles. The float property takes left, right,none(default value) and inherit as values. The value left indicates the div tag will be made to float on the left and right to float the div tag to the right.

Why is there a gap between my divs?

The gap is the result of the margin of the paragraph escaping from its container. Give me a minute to look at the page and I'll offer more info. EDIT: @RyanReese nailed it. It's just a margin collapse issue.


2 Answers

Problem

Without specifying a width, the width of the inline block is automatically determined by its contents. In your case, the red block contains a long line, which makes it fill (almost) the entire space available. The blue block is wider than the space that is left available by the red block, causing it to wrap to the next line.

NB: Before reading the suggestions I gave in 2015, note that I would probably try to do it using flexbox now, as in this answer by Steve Sanders.

Solution 1: specifying a width for both elements

In the snippet below, both blocks are given a width. You can specify a pixel width, because you know the container size too, but percentages would work as well, provided they add up to a 100%, not more.

Note that I had to modify the HTML a little too to remove the whitespace between them. This is a common pitfall when choosing this solution.

.container {
  width: 200px;
  border: 1px solid black;
}
.a {
  display: inline-block;
  width: 180px;
  background: red;
}
.b {
  display: inline-block;
  width: 20px;
  background: blue;
}
<div class="container">
  <div class="a">hello hello hello hello hello hello hello
  </div><div class="b">aaa</div>
</div>

Solution 2: display the elements as table cells in a row

Alternatively, you can make them behave like a table row. This has the advantage that their height will match too, and that you can easily give one of them a width and the other not. Also, you won't have the whitespace-between-elements issue that you would need to solve when using the first solution.

.container {
  width: 200px;
  border: 1px solid black;
  display: table;
}
.a {
  display: table-cell;
  background: red;
}
.b {
  width: 20px;
  display: table-cell;
  background: blue;
}
<div class="container">
  <div class="a">hello hello hello hello hello hello hello
  </div><div class="b">aaa</div>
</div>
like image 127
GolezTrol Avatar answered Nov 15 '22 21:11

GolezTrol


The display:inline-block makes both elements remain in the same line if there's enough space for it. Otherwise, the line will break.

There are many solutions that would work here, but lets think simpler...

If you have defined a fixed width for both the container and the "b" div, then why not set a fixed width to the "a" div as well, with the 180px remaining?

.container {
  width: 200px;
  border: 1px solid black;
}
.a {
  width: 180px;
  display: inline-block;
  background: red;
}
.b {
  width: 20px;
  display: inline-block;
  background: blue;
}
<div class="container">
  <div class="a">hello hello hello hello hello hello hello</div><div class="b">aaa</div>
</div>
like image 20
LcSalazar Avatar answered Nov 15 '22 20:11

LcSalazar