Vertical alignment of icones

Asked

Viewed 1,582 times

1

I have the following structure:

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

Class: icons an element <a> 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 using more than 3 icons(2 lines).

Link to the problem: Jsfiddle

Note that the first row of icons is exactly in the center(vertically), but this does not take into account the second row of icons.

Solved!

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 align_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 causing the div align_icons to the size of all icons and aligned to the size of the div .icons.

Note: this was a provisional code because I did not want to change my code, but it can be solved in other ways, if someone has other solutions I ask you to post.

  • Hi Gabriel. Please post your solution as an answer in the area below, and mark it as the accepted answer. So it is more organized. I recommend a look at our [tour], here it works different from the traditional forums. Thank you!

1 answer

2


This is normal behavior, the width occupied by all <a> is bigger than your div’s #icons. Even if you use the property white-space:nowrap (in #icons) the internal content shall exceed the:

img

In this case you have two options: Decrease the size occupied by the child elements or increase the width of the parent element.

Alternative

When you don’t have a fixed amount of internal elements, the best solution is to use the property flexbox.


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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.