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>
The display:inline-block makes both elements remain in the same line if there's enough space for it. Otherwise, the line will break.
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.
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.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With