2016-09-12 3 views
2

У меня есть значки, которые я хочу центрировать как по горизонтали, так и по вертикали.Как центрировать символ, который больше его контейнера

См codepen и фрагмент кода ниже:

div { 
 
    border: 1px solid black; 
 
} 
 
.icon-placeholder { 
 
    height: 34px; 
 
    overflow: hidden; 
 
    width: 34px; 
 
} 
 
.icon { 
 
    color: hotpink; 
 
    font-size: 400%; 
 
} 
 
.icon::before { 
 
    content: '+'; 
 
}
<div class="icon-placeholder"> 
 
    <span class="icon"></span> 
 
</div>

Как я могу сделать это, независимо от размера шрифта .icon «s.

Я пробовал transform, position: absolute, display: table не повезло. Я не могу использовать flex.

+0

[Как центр элементов по вертикали и горизонтали в Flexbox] (http://stackoverflow.com/a/33049198/3597276) –

ответ

0

На что нужно отметить, хотя, + не обязательно в центре, в первую очередь в зависимости от шрифта.

div { 
 
    border: 1px solid black; 
 
} 
 

 
.icon-placeholder { 
 
    height: 34px; 
 
    overflow: hidden; 
 
    position: relative; 
 
    width: 34px; 
 
} 
 

 
.icon { 
 
    color: hotpink; 
 
    font-size: 400%; 
 
} 
 

 
.icon::before { 
 
    content: '0'; 
 
    display: inline-block; 
 
    position: absolute; 
 
    right: 50%; 
 
    bottom: 50%; 
 
    transform: translate(50%, 50%); 
 
}
<div class="icon-placeholder"> 
 
    <span class="icon"></span> 
 
</div>

0

Вы можете достичь его с помощью transform и absolute позиционирование

div { 
 
    border: 1px solid black; 
 
    margin: 10px; 
 
} 
 

 
.icon-placeholder { 
 
    height: 34px; 
 
    overflow: hidden; 
 
    width: 34px; 
 
    position: relative; 
 
} 
 

 
.icon { 
 
    color: hotpink; 
 
    font-size: 400%; 
 
    margin-top: -10%; 
 
    position: absolute; 
 
    top: 50%; 
 
    transform: translate(-50%, -50%); 
 
    left: 50%; 
 
} 
 
.icon::before { 
 
    content: '+'; 
 
} 
 

 
.plus-symbol{ 
 
    font-size: 400%; 
 
    outline: dotted 1px red; 
 
    color: hotpink; 
 
} 
 

 
.left, .right{ 
 
    width: 45%; 
 
    padding: 20px; 
 
    box-sizing: border-box; 
 
} 
 

 
.left{ 
 
    float: left; 
 
} 
 
.right{ 
 
    float: right; 
 
} 
 

 
.custom-plus-icon, .custom-plus-icon:before{ 
 
    position: absolute; 
 
    width: 10%; 
 
    border-radius: 1px; 
 
    background-color: hotpink; 
 
    height: 80%; 
 
    margin: auto; 
 
    left: 0; 
 
    right: 0; 
 
    top: 0; 
 
    bottom: 0; 
 
} 
 
.custom-plus-icon:before{ 
 
    content: ''; 
 
    width: 100%; 
 
    height: 100%; 
 
    transform: rotate(90deg); 
 
}
<div class="left"> 
 
    <h3>Plus symbol using font</h3> 
 
    <span class="plus-symbol">+</span> 
 

 
    <br/> 
 
    <br/> 
 

 
    <label>Font-size : 400%</label> 
 
    <div class="icon-placeholder"> 
 
    <span class="icon"></span> 
 
    </div> 
 
    <label>Font-size : 300%</label> 
 
    <div class="icon-placeholder"> 
 
    <span class="icon" style="font-size: 300%;"></span> 
 
    </div> 
 
    <label>Font-size : 200%</label> 
 
    <div class="icon-placeholder"> 
 
    <span class="icon" style="font-size: 200%;"></span> 
 
    </div> 
 
    <label>Font-size : 100%</label> 
 
    <div class="icon-placeholder"> 
 
    <span class="icon" style="font-size: 100%;"></span> 
 
    </div> 
 
</div> 
 
<div class="left"> 
 
    <h3>Plus symbol using pseudo element</h3> 
 
    <div class="icon-placeholder"> 
 
    <span class="custom-plus-icon"></span> 
 
    </div> 
 
</div>

+1

Где сделайте '12%' взялось? –

+0

'margin-top: -12%' был добавлен в центр символа плюс внутри поля. У шрифта, вероятно, есть пробел сверху, что составляет 12% от размера шрифта. – z0mBi3

+0

Это не разумное решение. Это пробелы не всегда равны 12%. –

Смежные вопросы