I want to horizontally center a few inline blocks, but at the same time have them align to the left on their last row (see below).
The problem is that I achieved something like this (http://jsfiddle.net/5JSAG/):
| _____ _____ |
| | | | | |
| | 1 | | 2 | |
| |_____| |_____| |
| _____ |
| | | |
| | 3 | |
| |_____| |
While I want something like this:
| _____ _____ |
| | | | | |
| | 1 | | 2 | |
| |_____| |_____| |
| _____ |
| | | |
| | 3 | |
| |_____| |
You can see some sample HTML at http://jsfiddle.net/5JSAG/.
I have tried using column-count
and column-width
but it doesn't work as I want it to, because the order of the blocks changes:
| _____ _____ |
| | | | | |
| | 1 | | 3 | |
| |_____| |_____| |
| _____ |
| | | |
| | 2 | |
| |_____| |
Inline block divs can be centered by applying text-align:center to their parent.
To align things in the inline direction, use the properties which begin with justify- . Use justify-content to distribute space between grid tracks, and justify-items or justify-self to align items inside their grid area in the inline direction.
Block level elements We can center a block-level element by giving it margin-left and margin-right of auto (which has a known specified width):
Center Align Elements To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.
Found a quite simple solution to this problem: just add some filler divs at the end, which are of the same width with the blocks that are aligned.
http://jsfiddle.net/5JSAG/34/
HTML:
<div style="text-align:center">
<div class="entry">1</div>
<div class="entry">2</div>
<div class="entry">3</div>
<div class="entry">4</div>
<div class="entry">5</div>
<span class="fill"></span>
<span class="fill"></span>
<span class="fill"></span>
<span class="fill"></span>
</div
CSS:
.fill
{
display:inline-block;
width:100px;
height:0px;
line-height:0px;
font-size:0px;
}
.entry
{
display:inline-block;
margin-top:10px;
width:100px;
height:60px;
padding-top:40px;
border:1px solid red;
}
Floating them seems the best option here. You could put left/right margins on the container if you need space on the left and right, or you could give the container a width and auto left and right margins.
Looks like it might be worth margin this up as an unordered list, too.
Here's an example:
http://codepen.io/anon/pen/Ehgdp
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