Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple divs spread to fill width of container?

Tags:

html

css

My setup right now is something like this

<div class="container">
    <div class="child">AAAAA</div>
    <div class="child">BB</div>
    <div class="child">CCCCCCCCC</div>
    <div class="child">D</div>
</div>

.container {
    width: 100%;
    border: 1px solid black;
    display: inline-block;
}
.child {
    border: 1px solid grey;
    display: inline-block;
}

http://jsfiddle.net/T2478/

I want all divs with class "child" to spread themselves up to fill the entire width of the div with class "container"?

Probably a pretty easy solution, I'm fairly new to html and css

How can I do this so that no matter what width "container" is the children will spread out evenly?

Thanks!

like image 477
user2981824 Avatar asked Nov 12 '13 05:11

user2981824


People also ask

How do I make a div take up the whole width?

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. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.

How do I fit a div into a container?

Method 2: We can make the display attribute of the child container to table-row and display attribute of parent container to table, that will take all the height available from the parent div element. To cover all the width, we can make the width of parent div to 100%.

How do you overlap a container in HTML?

You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).

Can you get two divs at once?

With CSS properties, you can easily put two <div> next to each other in HTML. Use the CSS property float to achieve this. With that, add height:100px and set margin.


3 Answers

The use of flex was actually a great idea. Here you go:

.container {
  width: 100%;
  display: flex;
  justify-content: space-between;
  
  border: 1px solid black;
}

.child {
    flex: 1 1 auto;
    
    border: 1px solid grey;
}
<div class="container">
    <div class="child">AAAAA</div>
    <div class="child">BB</div>
    <div class="child">CCCCCCCCC</div>
    <div class="child">D</div>
</div>
    

You may play with justify-content value and with flex value for extra customization.
And no need to mess around with tables where you shouldn't.

P.S. You can dive into the flex easily with this well known article.

like image 115
Mesqalito Avatar answered Oct 24 '22 17:10

Mesqalito


You're going to need to emulate table layout behavior, as in this answer.

This does NOT involve using actual <table> and children elements! This involves using certain values of the display property, namely table and table-cell, to emulate the layout behavior of tables. You don't want to use actual table markup for pure layout, as that is semantically incorrect and frowned upon. If your content is actual tabular data that belongs in a table, then it makes perfect sense to use an actual <table>. I'll present the non-table way here since that's likely what you want.

Your div.container element will need the display: table; property to make it behave like <table>. You'll also want the table-layout rule with the fixed value, to make all of your child elements evenly spaced. Your div.child elements will then need display: table-cell; to make them act like table cells. The browser will now automagically compute evenly-spaced layouts for your child elements. Note that you do not need/want a width rule on your child elements anymore, since the browser computes that. The container element will need a width of some kind, otherwise the table will simply collapse to be only as large as its content. Note that content can also overflow out of the child elements if the content width is more than the computed, evenly-spaced dimensions.

The final code is below, and here's a fiddle.

HTML:

<div class="container">
    <div class="child">AAAAA</div>
    <div class="child">BB</div>
    <div class="child">CCCCCCCCC</div>
    <div class="child">D</div>
    <div class="child">E</div>
    <div class="child">E</div>
    <div class="child">E</div>
</div>

CSS:

.container {
    width: 100%;
    border: 1px solid black;
    display: table;
    table-layout: fixed;
}
.child {
    border: 1px solid grey;
    display: table-cell;
}

Also, an unrelated note about display: inline-block;. It will render any and all whitespace between inline-block elements (you can see this as spacing between your child elements in your original fiddle). This is normal and following the spec, but is sometimes undesired. You can read about ways to get around it here.

like image 37
ajp15243 Avatar answered Oct 24 '22 16:10

ajp15243


I know this answer is quite old, but I can across a problem like this just now and used CSS' calc function. Take a look at the updated code I made for you.

<div class="container">
    <div class="child">AAAAA</div>
    <div class="child">BB</div>
    <div class="child">CCCCCCCCC</div>
    <div class="child">D</div>
</div>

.container {
    width: 100%;
    border: 1px solid black;
    display: flex;
    display: -webkit-flex;
    display: -ms-flexbox;
}
.child {
    border: 1px solid grey;
    width: calc( 100% / 4); /* 4 is how many .child divs you have */
}

jsfiddle

like image 33
Arin_nirA Avatar answered Oct 24 '22 15:10

Arin_nirA