I have to place few elements evenly and fluidly inside another div as mocked below:
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).
Any help would be greatly appreciated.
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.
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.
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.
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.
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
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>
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