Vertical alignment of icons

1

I have the following structure:

a div with width: 228px and height: 228px .

Class: icons a <a> element with image size (icon).

The problem is that I have some cases that have 3 icons (1 line) and others with more than 3 icons, forming more than one line. That is, my alignment is not exactly in the center when I use more than 3 icons (2 lines).

Link to problem: JSFiddle

Note that the first line of icons is exactly in the center (vertically), but is not taking into account the second line of icons.

Resolved!

I removed the code from the #icon selector:

top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);

I created a new div called alinha_icons:

<div id="icons">
   <div id="alinha_icons">
       <a class="icon" target="_blank" href="#"><img src="img.png"></a>
       <a class="icon" target="_blank" href="#"><img src="img.png"></a>
       <a class="icon" target="_blank" href="#"><img src="img.png"></a>
       <a class="icon" target="_blank" href="#"><img src="img.png"></a>
    </div>
</div>

#alinha_icons{
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
position: relative;
display: block;}

Thus making div alinha_icons the size of all the icons and aligned in relation to the size of div .icons.

    
asked by anonymous 20.01.2015 / 13:00

1 answer

2

This is normal behavior, the width occupied by all <a> is greater than that of its #icons div. Even if you use the property white-space:nowrap (in #icons ) the internal content will exceed the limit:

Inthiscaseyouhavetwooptions:Decreasethesizeoccupiedbychildelementsorincreasethewidthoftheparentelement.

Alternative

Whenyoudonothaveafixedamountofinternalelements,thebestsolutionmightbetousetheflexboxproperty.


Example:

* {-webkit-box-sizing:border-box;box-sizing: border-box;}

/* Só isso...
   Não precisei definir nada nos elementos filhos */
.documents {
    display: -webkit-flex;
     display: -ms-flexbox;  
        display: -moz-box;
            display: flex;
    -webkit-flex-flow: row wrap;
    justify-content: center;
}

/* Só para melhorar a visualização, as regras abaixo não tem relevância */
.documents {background: #27ae60;height:60px;width: 300px}
.documents span {color: #fff;margin: 12.5px 1px 0 1px;font-size: 2em}
<link rel='stylesheet' href='http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css'/>

<p>Com um único elemento</p>
<div class='documents'>
    <span class='fa fa fa-file-word-o'></span>
</div>

<p>Com mais de um...</p>
<div class='documents'>
    <span class='fa fa fa-file-word-o'></span>
    <span class='fa fa fa-file-word-o'></span>
    <span class='fa fa fa-file-word-o'></span>
    <span class='fa fa fa-file-word-o'></span>
</div>

<p>Com mais ainda...</p>
<div class='documents'>
    <span class='fa fa fa-file-word-o'></span>
    <span class='fa fa fa-file-word-o'></span>
    <span class='fa fa fa-file-word-o'></span>
    <span class='fa fa fa-file-word-o'></span>
    <span class='fa fa fa-file-word-o'></span>
    <span class='fa fa fa-file-word-o'></span>
    <span class='fa fa fa-file-word-o'></span>
    <span class='fa fa fa-file-word-o'></span>
    <span class='fa fa fa-file-word-o'></span>
</div>
    
20.01.2015 / 13:56