There is a block on my site, where user's work price is written. All users have different price of work, so if text length is more than 5 letters, the text wont fit the parent block.
I'd like to solve this problem with no javascript code if possible
There is a block on my site, where user's work price is written. All users have different price of work, so if text length is more than 5 letters, the text wont fit the parent block.
I'd like to solve this problem with no javascript code if possible
I think your only two options are adjusting the with of the "box" or the font size. I would try to use display: inline; or display: inline-block on your "box" and not setting the width explicitly. With that it should always match the with of the text.
.box {
display: inline;
background: #cccccc;
margin: 5px;
padding: 10px;
}
<div class="box">
100 $
</div>
<div class="box">
1000 $
</div>
<div class="box">
1000 $
</div>
You have four options:
widthfont-sizeOptions 1) and 3) have the advantage of giving your page a uniform appearance.
Options 2) and 4) have the advantage of only altering the appearance of a grey block (and the text it contains) when necessary.
If you declare display: inline-block for a div and do not explicitly set a width for that div, then it will have an implicit width based on its content. See below for an example.
Working Example using dynamic widths:
div {
position: relative;
display: inline-block;
margin: 12px 6px;
padding: 12px;
font-size: 24px;
text-align: center;
color: rgb(0, 127, 0);
background-color: rgb(227, 227, 227);
border-radius: 6px;
}
<div>3000₽</div>
<div>300000₽</div>
<div>300000000₽</div>
Without using JS to query for width, then your only option is to change the size of the box, or change the size of the text.