bogdansrc bogdansrc - 4 months ago 8
HTML Question

Center multiple inline blocks with CSS and align the last row to the left

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 | |
| |_____| |

Answer

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;
}