Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make groups of divs the same width using only CSS?

I've got a group of buttons (divs) which I would like to be the same length for all buttons within a logical group.

I could simply do a min-length for the CSS style of the button, however some button groups are short, while others are very long. This makes a uniform min-length either insufficient for long groups or wasteful/stupid looking for short ones. Additionally, multiple groups (short and long) can appear on the same screen.

Example of Button Layouts

(These divs are all styled with display: inline-block, so that's why they don't fill the width of the container)

I've thought of a few nasty solutions to this, but none are preferable:

  • Set a specific min-length for each group
  • Use JavaScript to resize the buttons

I was wondering if there was a generic, pure CSS solution to the above problem instead of using either of these methods. Thanks for any help you can provide.


Edit: Here is the markup and CSS I've got so far. Pretty simple:

HTML:

<div class="wrapper">
    <div class="button">Lorem ipsum</div>
    <div class="button">Lorem ipsum dolar</div>
    <div class="button">Lorem</div>
</div>

CSS:

div.button { display: inline-block; min-width: 50px; }
like image 625
DOOManiac Avatar asked Dec 02 '14 17:12

DOOManiac


People also ask

How do you make equal width in CSS?

Using CSS width property: By using this property, we can define each cell's width value. If we set the value in the equal percentage unit then the width of the each cell will be equal, every time window size doesn't matter.

How do I make all divs the same size?

You can use Jquery's Equal Heights Plugin to accomplish, this plugins makes all the div of exact same height as other.

How do you evenly distribute items in CSS?

The "space-evenly" value for the justify-content property distributes the space between items evenly. It is similar to space-around but provides equal instead of half-sized space on the edges. Can be used in both CSS flexbox & grid.

How do I keep two side by side div elements the same width?

The two or more different div of same height can be put side-by-side using CSS. Use CSS property to set the height and width of div and use display property to place div in side-by-side format.


2 Answers

Wrap similar buttons in a container div with a desired width and set the inners divs to:

box-sizing: border-box;
width: 100%;

This will allow you to edit the widths of a logical grouping in one place.

like image 116
Mike Kauffman Avatar answered Oct 17 '22 07:10

Mike Kauffman


wrap them both in a container.

<div class="group">
    <div class="button">Hi</div>
    <div class="button">Wassup</div>
    <div class="button">Yo</div>
</div>

then apply css like this:

    display:inline-block;
    min-width: 100%;

and then set the div "group" that groups those divs in the width you wanted to

like image 24
marto Avatar answered Oct 17 '22 06:10

marto