Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All widths set to width of widest element

I've read a lot of tutorials about CSS flex boxes and CSS grids, but frankly I just can't figure out how to do this simple(?) thing.

All I want is for the elements in a container to all be as wide as the widest element.

I don't want them to fill up the whole width of the container, just all grow to the maximum width. Can that be done with CSS?

Consider this HTML:

<div class="container">
   <button>a normal button</button>
   <button>tiny</button>
   <button>a really, really, really wide button</button>
</div>

That produces something like this:

enter image description here

What I want is something like this, where they're all the width of that widest button:

enter image description here

I DON'T want them to just stretch out to fill the width of the page:

enter image description here

Can this be done? If so, what would be the CSS for the above HTML?

like image 336
tscheingeld Avatar asked Dec 28 '17 07:12

tscheingeld


People also ask

Which CSS property sets the width of an element?

The width property sets the width of an element. The width of an element does not include padding, borders, or margins! Note: The min-width and max-width properties override the width property.

What is the width of an element?

The width property in CSS specifies the width of the element's content area. This “content” area is the portion inside the padding, border, and margin of an element (the box model). In the example above, elements that have a class name of . wrap will be 80% as wide as their parent element.

How do you find the full width of an element?

Using width, max-width and margin: auto; The element will take up the specified width, and the remaining space will be split equally between the two margins: This <div> element has a width of 500px, and margin set to auto.

What is wide element?

Site-wide elements are the items that appear on every page, and are identical on every page. Site-wide elements are categorized into Header, Navigation and Footer regions.


2 Answers

Yes. This can be done with pure CSS.

Here's a simple solution:

.container {
  display: inline-grid;
  grid-template-columns: 1fr 1fr 1fr;
}
<div class="container">
   <button>a normal button</button>
   <button>tiny</button>
   <button>a really, really, really wide button</button>
</div>

Here's how it works:

Grid Layout provides a unit for establishing flexible lengths in a grid container. This is the fr unit. It is designed to distribute free space in the container (and is somewhat analogous to flex-grow).

However, the Grid spec provides for a particularly unique behavior when the container's width / height is dynamic (e.g., display: inline-grid, in this case).

In such cases, the fr unit will compute the max-content of each grid item. It will then take the maximum value it finds and use that as the 1fr length for all items on the track.

That results in grid items that share the width of the widest item in the row.

Strange, but true.

It's also why a layout with equal height rows is possible with Grid (but not flexbox).

Here's the relevant section in the spec:

7.2.3. Flexible Lengths: the fr unit

...

When the available space is infinite (which happens when the grid container’s width or height is indefinite), flex-sized (fr) grid tracks are sized to their contents while retaining their respective proportions.

The used size of each flex-sized grid track is computed by determining the max-content size of each flex-sized grid track and dividing that size by the respective flex factor to determine a “hypothetical 1fr size”.

The maximum of those is used as the resolved 1fr length (the flex fraction), which is then multiplied by each grid track’s flex factor to determine its final size.

like image 66
Michael Benjamin Avatar answered Nov 15 '22 08:11

Michael Benjamin


Here is a solution using CSS Grid working with any number of buttons:

div.container {
  display: inline-grid;
  grid-auto-columns: 1fr;
}

div.container button {
  grid-row: 1;
}
like image 45
Simon Hi Avatar answered Nov 15 '22 09:11

Simon Hi