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;
}
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!
Try this
Fiddle
Just remove the
display: inline-block;
propertie from child class.CSS Divs already have the "abillity" to fill up the whole width.
The use of
flex
was actually a great idea. Here you go:You may play with
justify-content
value and withflex
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.
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 thedisplay
property, namelytable
andtable-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 thedisplay: table;
property to make it behave like<table>
. You'll also want thetable-layout
rule with thefixed
value, to make all of your child elements evenly spaced. Yourdiv.child
elements will then needdisplay: 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 awidth
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:
CSS:
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.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.
jsfiddle