Reputation: 2970
Consider this simple example
<div class="outer">
<div class="left">
left left left left left left left left left left left left left left left
</div>
<div class="right">
right
</div>
</div>
I want to float
the children div
s to left and right, in the same line. Also, when the outer div
size is shrinked, I want it to scroll, rather than inserting a line break between the 2 divs.
This is my CSS:
.outer {
width:fit-content;
border: 1px solid black;
white-space:nowrap;
overflow-x:auto;
}
.left, .right{
display:inline-block;
}
.left {
background-color: #dcc2c2;
}
.right {
clear:none;
float:right;
background-color: #d0dee8;
}
The problem is that when outer div
is shrinked, a line break is inserted:
Here is a demo fiddle. Any help is appreciated.
Upvotes: 3
Views: 518
Reputation: 288260
On browsers that support text-align-last
, you can display them as inline-blocks and justify them:
.outer {
white-space: nowrap;
overflow-x: auto;
-moz-text-align-last: justify;
text-align-last: justify;
border: 1px solid black;
}
.left, .right {
display: inline-block;
}
.left {
background-color: #dcc2c2;
}
.right {
background-color: #d0dee8;
}
<div class="outer">
<div class="left">left left left left left left left left left left left left left left left</div>
<div class="right">right</div>
</div>
Upvotes: 0
Reputation: 1032
You need to float your .left class, you aren't doing that.
.left {
background-color: #dcc2c2;
float: left;
}
This will make .left and .right float in the same line, until there isn't enough room on that line, then they fill below.
You should also set the height of the .outer div.
Upvotes: 0
Reputation: 288260
You can use flexible boxes:
.outer {
display: flex; /* Magic begins */
justify-content: space-between; /* Flush to the edges */
overflow-x: auto; /* Add scrollbar if necessary */
border: 1px solid black;
}
.left, .right {
flex-shrink: 0; /* Don't shrink them */
white-space: nowrap; /* Don't wrap lines inside them */
}
.left {
background-color: #dcc2c2;
}
.right {
background-color: #d0dee8;
}
<div class="outer">
<div class="left">left left left left left left left left left left left left left left left</div>
<div class="right">right</div>
</div>
Upvotes: 2
Reputation: 3925
Use absolute
position of both left
and right
divs and relative
position of outer
.outer {
position: relative;
height: 100px;
...
}
.left, .right{
display:inline-block;
}
.left {
position: absolute;
left: 0;
}
.right {
position: absolute;
right: 0;
}
But outer
's height should be set implicitly.
Upvotes: 0
Reputation: 12045
.outer {
display: table;
width: 100%;
border: 1px solid black;
white-space: nowrap;
}
.left,
.right {
display: table-cell;
}
.left {
background-color: #dcc2c2;
}
.right {
background-color: #d0dee8;
}
<div class="outer">
<div class="left">left left left left left left left left left left left left left left left</div>
<div class="right">right</div>
</div>
Upvotes: 4