Within my html/css application, I have defined a button as a class named .button
with specific attributes. Here is 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 certain instances, the text to be displayed on the button may be lengthy and needs to fit entirely within the specified dimensions. As the font-stretch
property is not universally supported, I utilize transform: scale
, which serves the purpose partially. In addition to the above code, I include:
.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 transformed text now fits properly within the button, but it is no longer centered due to the order in which CSS transformations are applied. To view an example of this issue, refer to the following jsfiddle link: https://jsfiddle.net/1xz1g634/1/
To resolve this alignment problem, I could manually center the text by adding a negative left margin to the .button-text
class, but this solution feels like a workaround and may vary across different browsers.
Is there a method to consistently center the text within the button without relying on javascript? If necessary, I am open to using javascript or jquery as a last resort.