Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make an inline-block div take 100% of the remaining width

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?

like image 528
Diego Avatar asked Jul 30 '13 19:07

Diego


People also ask

How do inline block elements add up to 100% width?

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.

How do you make a div 100 width?

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.

How do you make a div fill a remaining horizontal space?

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.

Do inline elements take up the entire width of their container?

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.


2 Answers

float: left the red and green and the blue get width: clac(100% - 100px)

.blue {
    width: calc(100% - 100px);
}

http://jsfiddle.net/6kLVn/190/

like image 161
URL87 Avatar answered Oct 19 '22 12:10

URL87


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

like image 8
Elise Avatar answered Oct 19 '22 12:10

Elise