css - Alter text size to fit button width in html -
this question has answer here:
- how set font size based on container size? 10 answers
is way in html or css change text size in order fit in button of width changes according container. have 3 buttons of same width, 3rd text little small , 2 letters go out of button if button shrinks.
svg can here. consider following:
<button style="position: relative; width: 400px; height: 200px; "> <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="100%" viewbox="0 0 100 50"> <text y="50%" x="50%" text-anchor="middle" font-size="20">foobar</text> </svg> </button>
the 20
in font-size="20"
refers coordinates specified viewbox
, font size 1/5 (20/100) of size of button. of course, not precisely solve problem of ensuring text fits in button, if have general idea of number of characters, can choose font size such that number of characters fit.
Comments
Post a Comment