Я знаю много методов центрирования внутреннего содержимого, но ни один из них не работает с текстом нестандартного размера с выравниванием по левому краю.
Мне нужен результат http://joxi.ru/krDDq9duE6zL0r.
Перепробовал все известные мне способы:
// first attempt
.inner {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
// second attempt
.wrapper {
display: flex;
align-items: center;
justify-content: center;
}
// third attempt
.wrapper {
display: table-cell;
text-align: center;
vertical-align: middle;
}
// forth attempt
.wrapper:after {
content:'';
display: inline-block;
vertical-align: middle;
height: 74px;
}
Вот код:
.wrapper,
.wrapper2,
.wrapper3,
.wrapper4 {
position: relative;
box-sizing: border-box;
width: 225px;
height: 74px;
padding: 0 20px;
margin: 5px 0;
background: yellow;
}
.try1 {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.try2 {
position: relative;
float: left;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.wrapper2 {
display: flex;
align-items: center;
justify-content: center;
}
.wrapper3 {
display: table-cell;
text-align: center;
vertical-align: middle;
}
.try4 {
display: inline-block;
text-align: left;
}
.wrapper4 {
position: relative;
white-space: nowrap;
}
.wrapper4:after {
content: '';
display: inline-block;
vertical-align: middle;
height: 74px;
}
.try5 {
display: inline-block;
vertical-align: middle;
white-space: normal;
}
<p>Tries:</p>
<div class="wrapper">
<div class="try1">DUE DILIGENCE EXPIRATION</div>
</div>
<div class="wrapper">
<div class="try2">DUE DILIGENCE EXPIRATION</div>
</div>
<div class="wrapper2">
<div class="try3">DUE DILIGENCE EXPIRATION</div>
</div>
<div class="wrapper3">
<div class="try4">DUE DILIGENCE EXPIRATION</div>
</div>
<div class="wrapper4">
<div class="try5">DUE DILIGENCE EXPIRATION</div>
</div>
<p>I want:</p>
<div class="wrapper">
<div class="try1">DUE DILIGENCE EXPIRATION</div>
</div>
UPD Обратите внимание, что текст может быть любым от одной буквы до нескольких строк. Таким образом, внутренняя ширина содержимого является гибкой.