Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Parent Width equal to Children Total Width using only CSS?

Tags:

html

css

width

calc

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>
like image 221
Sprout Coder Avatar asked Dec 09 '14 15:12

Sprout Coder


People also ask

How do you make a parent equal width?

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.

Can we give width more than 100% in CSS?

Yes, as per the CSS 2.1 Specification, all non-negative values are valid for width, that includes percentage values above 100%.

How do you make a div full width of a parent?

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%.

Is width inheritable CSS?

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.


6 Answers

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>
like image 170
sooon Avatar answered Oct 01 '22 16:10

sooon


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)

like image 29
Pranav Avatar answered Oct 01 '22 17:10

Pranav


* {
  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>
like image 43
Paulie_D Avatar answered Oct 01 '22 16:10

Paulie_D


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. )

like image 25
Liu CZ Avatar answered Oct 01 '22 18:10

Liu CZ


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.

like image 45
arafatgazi Avatar answered Oct 01 '22 16:10

arafatgazi


* {
      flex-grow: 1;
      flex-basis: 0;
      width: fit-content;
  }

you can try this

like image 32
Nguyễn Dương Avatar answered Oct 01 '22 18:10

Nguyễn Dương