I don't know how many .child
elements .parent
will contain but I know their individual width.
I want to set the width of .parent
to be equal to (width of each .child
) * (total number of .child
)
I don't want to use floats
and width: auto
.
Can I do something with calc()
without using Javascript?
.parent {
height: 100%;
width: calc({number of children} * {width of each child = 100px});
}
.child {
height: 100px;
width: 100px;
}
<div class="parent">
<div class="child">a</div>
<div class="child">b</div>
<div class="child">c</div>
</div>
Set the padding to 0 in the parent. So in the parent css class add padding: 0px; this should fix it. You may also need to set margin: 0px; in the child css class.
Yes, as per the CSS 2.1 Specification, all non-negative values are valid for width, that includes percentage values above 100%.
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%.
Inheritance in CSS occurs when an inheritable property is not set on an element. It goes up in its parent chain to set the property value to its parent value. CSS properties such as height , width , border , margin , padding , etc. are not inherited.
I know this is a bit late, but Hope this will help somebody who is looking for similar solution:
<div class="parent" style="display: inline-flex">
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
</div>
the trick is to use inline-flex
for the parent
and inline-table
for the child
. Everything is dynamic. I make the table scrollable horizontally by adding another grandparent
with overflow-x:scroll;
:
<div class="grandparent" style="width: 300px; overflow-x: scroll; background: gray">
<div class="parent" style="display: inline-flex">
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
</div>
</div>
If you're on Chrome, you can use max-width: fit-content
It's not yet supported by other major browsers though.
UPDATE:
As Kasper pointed out in the comments, most browsers now do support max-width: fit-content
(albeit with a vendor prefix in the case of Firefox)
* {
margin:0;
padding:0;
box-sizing:border-box;
}
.parent {
height: 100%;
display:inline-block;
background:#bada55;
font-size:0; /* clear whitespace*/
}
.child {
height: 100px;
width: 100px;
border:1px solid red;
display:inline-block;
}
<div class="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
use
{ width: max-content }
on parent node should works fine. It's also an experimental feature. (I don't know why others do not post this approach. )
You can simply set display: inline-block;
to the .parent
element for its width to be equal to the total width taken by its .child
elements.
.parent {
display: inline-block;
}
Whichever elements in HTML come with display: block;
by default, will occupy the full width of their parent element. In your case, the .parent
element, because of having display: block;
is taking up the full width of its parent. So, by setting display: inline-block;
for the .parent
element will make its width become equal to its contents which in this case is the total width taken by its .child
elements.
* {
flex-grow: 1;
flex-basis: 0;
width: fit-content;
}
you can try this
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