I have 3 div
blocks inside another div
.
What I wanted to do is to put them inline, but the first 2 div
blocks should take a width according to their content and the last div
take the remaining space.
<div class="container">
<div class="red">Red</div>
<div class="green">Green</div>
<div class="blue">Blue</div>
</div>
I try to avoid the use of fixed widths because I need to use this in a responsive design.
How can I make the blue div
in this fiddle take the rest available space of its parent and act responsive if the screen is resized?
If you have a border or padding set for your divs, then you've created additional pixels that will prevent your divs from adding up to a 100% width. To fix this, make sure you've added box-sizing: border-box to the div's styles.
What you could do is set your div to be position: absolute so your div is independent of the rest of the layout. Then say width: 100% to have it fill the screen width. Now just use margin-left: 30px (or whatever px you need) and you should be done.
The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent.
An inline element does not start on a new line. An inline element only takes up as much width as necessary. This is a <span> element inside a paragraph.
float: left
the red and green and the blue get width: clac(100% - 100px)
.blue {
width: calc(100% - 100px);
}
http://jsfiddle.net/6kLVn/190/
I believe if you don't want to specify any pixel or percentage widths at all and make the red and green containers only as wide as their content, you will need to wrap them inside their own container, named .left
below:
<div class="container">
<div class="left">
<div class="red">Red</div>
<div class="green">green</div>
</div>
<div class="blue">blue</div>
</div>
If you now float .left
to the left, and also float .left div
to the left, you now no longer need to specify any inline-block elements. The blue container will simply take up as much space as it has available until the end of the .container
.
.left {
float: left;
}
.left div {
float: left;
}
Fiddle
Edit
Silly me, the .left
container is obviously not needed as long as you just add float: left
to your red and green blocks, just like @Ennui said above in the comments :)
Updated fiddle
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