In my html/css app, I have a notion of button, which is, in essence, class .button
with a bunch of appropriate attributes. Here's a simplified example:
.button {
width: 120px;
height: 30px;
line-height: 30px;
background: yellow;
text-align: center;
}
<div class="button">
<div class="button-text">
Button text
</div>
</div>
In a small number of places, the text that needs to be displayed is rather long, yet it still needs to fit fully. As font-stretch
property isn't support, I use transform: scale
, which does what I need - kind of... In addition to the above, I have:
.button {
width: 120px;
height: 30px;
line-height: 30px;
background: yellow;
text-align: center;
}
.button-text {
line-height: 30px;
transform: scale(0.7, 1);
white-space: nowrap;
}
<div class="button">
<div class="button-text">
Very long button text
</div>
</div>
The text now fits, but it's not centred, because the position of centered content within the div is calculated before the transform is applied - see example in this jsfiddle: https://jsfiddle.net/1xz1g634/1/
I can "make" it centred by applying negative left margin to the .button-text
, however that's a hack and the margin would have to be different in different browsers.
How can I universally centre that text? Ideally, without resorting to javascript, but, if everything else fails, I can use javascript/jquery.