Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Div fill remaining horizontal space

Tags:

html

css

I have this following code:

<div class="parent">
    <ul class="menu">
        <li>this</li>
        <li>width</li>
        <li>is</li>
        <li>dynamic.</li>
    </ul>

    <div class="something">
        <span>so is this</span>
        <table>because of this table.</table>
    </div>

    <div class="fill">
        <span>and so is this. but this guy needs to fill the remaining width.</span>
    </div>
</div>

Image

These 3 items - ul and 2 divs - are aligned side by side, and as you can see, they have dynamic widths. I have to make these 3 items fit inside div.parent, which width is fixed at 1200px.

Currently, I'm using 'float: left;' to align these 3 items side-by-side, and I can use 'display: inline-block;' if necessary [works perfectly]. But I've tried to use some tricks with 'display: table;' for the parent and 'display: table-cell;' for these 3 items, without success.

I need to fill this remaining space on the black div, which is the 'div.fill'. Any ideas?

EDIT2: http://jsfiddle.net/cAs9t/

like image 767
Paulo Mu Avatar asked Jan 21 '14 19:01

Paulo Mu


People also ask

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.

How do you make an element occupy full width?

The . header-square is missing width attribute. Therefore you should expand to 100% by doing width: calc(100% / 7); to achieve your desired result.

How do you flex occupy remaining space?

Use the flex-grow property to make a flex item consume free space on the main axis. This property will expand the item as much as possible, adjusting the length to dynamic environments, such as screen re-sizing or the addition / removal of other items.

How do I make a div fill the whole page?

position:absolute You can also use position absolute as well as set all the viewport sides (top, right, bottom, left) to 0px will make the div take the full screen.


1 Answers

Demo

Just add

div.fill { float: none; overflow: hidden; }

Where float: none removes the floating (you can also avoid applying float: left to .fill), and overflow: hidden (or anything different than visible) prevent floating elements from overlapping .fill.

Other ways:

  • You could use display: table-cell and display: table, but you couldn't specify which element should grow to fill all remaining space.

  • But if you want full control and want to distribute remaining spaces in a more complex way, you should use Flexboxes.

like image 162
Oriol Avatar answered Sep 22 '22 04:09

Oriol