Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to distribute elements evenly inside a div?

Tags:

html

css

I have to place few elements evenly and fluidly inside another div as mocked below:

enter image description here

enter image description here

enter image description here

I've seen the trick using text-align:justify as given in (https://stackoverflow.com/a/6880421/2159250), But given one or two elements, its getting placed(justified) at right/left side as given below: (which is not what I'm looking for).

enter image description here

Any help would be greatly appreciated.

like image 505
Aymer Avatar asked Oct 04 '14 10:10

Aymer


People also ask

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 make a div align?

You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How do you distribute elements?

To ensure they are all distributed evenly, select all the windows and use Edit > Distribute > Along X. Along Y: The two selected elements at either end (vertically) will remain in place; the rest will be evenly distributed between them.

How do I align elements in the same line?

To get all elements to appear on one line the easiest way is to: Set white-space property to nowrap on a parent element; Have display: inline-block set on all child elements.


2 Answers

Left aligned content looks like this (one or more dots represent a whitespace):

+----------------------------------------------+
|word.word.word.word                           |
+----------------------------------------------+

(1) text-align: justify does not justify the last (or the only) line*. One simple solution is to add a very long word which can only fit on a line of its own:

+----------------------------------------------+
|word..........word..........word..........word|
|longword_longword_longword_longword_longword_l|
+----------------------------------------------+

(2) You want whitespace before the first and after the last word. One simple solution is to add dummy words in order to produce the following result:

+----------------------------------------------+
|dummy....word....word....word....word....dummy|
|longword_longword_longword_longword_longword_l|
+----------------------------------------------+

The desired result can be achieved by adding additional markup. For example:

.row {
    text-align: justify;
}
.row:after {
    display: inline-block;
    content: "";
    width: 100%;
}
.box {
    display: inline-block;
}
.dummy {
    display: inline-block;
}
/**** FOR TESTING ****/
.row {
    margin: 1em 0;
    background: #FC0;
}
.box {
    background: #F0C;
    width: 4em;
    height: 5em;
}
.box:nth-child(even) {
    background: #0CF;
    width: 8em;
}
<div class="row">
    <div class="dummy"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="dummy"></div>
</div>
<div class="row">
    <div class="dummy"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="dummy"></div>
</div>
<div class="row">
    <div class="dummy"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="dummy"></div>
</div>
<div class="row">
    <div class="dummy"></div>
    <div class="box"></div>
    <div class="dummy"></div>
</div>

* text-align-last property could be used in the future

like image 118
Salman A Avatar answered Oct 30 '22 03:10

Salman A


This should be what you are looking for. It requires you to wrap your row elements inside a "dummy" div, but that should be fine.

.row {
  display: table;
  width: 100%;
  table-layout: fixed;
  margin-bottom: 10px;
}
.element-wrapper {
  display: table-cell;
  vertical-align: top;
}
.element {
  width: 80%;
  height: 100px;
  margin: auto;
  background: #ccc;
}
<div class="row">
  <div class="element-wrapper">
    <div class="element">Toto</div>
  </div>
  <div class="element-wrapper">
    <div class="element">Titi</div>
  </div>
  <div class="element-wrapper">
    <div class="element">Tata</div>
  </div>
  <div class="element-wrapper">
    <div class="element">Tete</div>
  </div>
</div>
<div class="row">
  <div class="element-wrapper">
    <div class="element">Toto</div>
  </div>
  <div class="element-wrapper">
    <div class="element">Titi</div>
  </div>
  <div class="element-wrapper">
    <div class="element">Tata</div>
  </div>
</div>
<div class="row">
  <div class="element-wrapper">
    <div class="element">Toto</div>
  </div>
  <div class="element-wrapper">
    <div class="element">Titi</div>
  </div>
</div>
like image 23
Antoine Combes Avatar answered Oct 30 '22 03:10

Antoine Combes