I have an element that contains only a text sentence, for example:
<p>Click here to see the Product Widget Name in action</p>
The text in the element will change according to the product page it is shown on.
The height of this element is 45px high, but the width variable. What I'm trying to do is to get the width to be as small as possible without the text overflowing out of the element. So I want the text to wrap as much as possible within the 45px height of the element, so the width is as small as possible.
However I can't seem to get this to work in any way in CSS. Below are some things I've tried, can add more elements to make this work if it is required.
p {
height: 45px;
font-size: 8pt;
line-height: 1.15;
border:1px solid black;
width:1px;
max-width:fit-content;
min-width:min-content;
}
<p>Click here to see the Product Widget Name in action</p>
p {
height: 45px;
font-size: 8pt;
line-height: 1.15;
border:1px solid black;
width:min-content;
max-width:fit-content;
min-width:min-content;
}
<p>Click here to see the Product Widget Name in action</p>
p {
height: 45px;
font-size: 8pt;
line-height: 1.15;
border:1px solid black;
width:auto;
max-width:min-content;
min-width:fit-content;
}
<p>Click here to see the Product Widget Name in action</p>
I know I can do this in JS, but would rather avoid it if I can. If JS is the only way, would I have to use a trial/error way by looping through multiple widths until I hit the right balance?
Please let me know if there's a solution to this!