I need to stack two div
elements horizontally side-by-side, where the right one should always adjust its size automatically to its content (up to a max given width) and the left one should just use the space that's left.
Something like this:
So far no problem. I've managed to do this by floating the right div
to the right and setting the overflow of the left one to hidden:
HTML:
<div class="frame">
<div class="right">
I adjust my with to my content but still need to know my boundaries if I'm floated right (max-width)
</div>
<div class="left">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</div>
CSS:
.frame {
background-color: lightgreen;
width: 80%;
padding: 12px;
overflow: hidden;
}
.left {
overflow: hidden;
background-color: #709670;
border: 1px solid black;
}
.right {
float: right;
max-width: 200px;
background-color: #127212;
color: white;
border: 1px solid black;
}
The challenge is that I want the two div
s to be stacked vertically when the page is displayed on a small screen (or small browser window, ...) like this:
Basically I thought this could be done by a simple media query:
@media screen and (max-width: 700px) {
.right {
float: none;
max-width: none;
}
}
The only problem with this is that the right div
(the one that resizes according to its content) needs to be created BEFORE the left one in the markup to make the floating thing work. As a result, it appears above the left one in the "small" layout:
Here's a working example (just use the middle resizer to switch between the "small" and "large" layout): Example on JSFiddle
I've already tried to accomplish the layout with the usage of display: table
and table-cell
instead of floating, but this way I fail to make the right column as large as its content and still have a working max-width set.