I have a div with a fixed width, but the text inside the div can change.
Is there a way of setting, with css or other, the spacing between the letters so the text always fills the div perfectly?
original title: "html - Stretch text to fit width of div"
I have a div with a fixed width, but the text inside the div can change.
Is there a way of setting, with css or other, the spacing between the letters so the text always fills the div perfectly?
Minulla on kiinteäleveinen div-osa, mutta div-yksikön sisällä oleva teksti voi muuttua. Onko olemassa tapa asettaa css tai muu kirjainten välinen etäisyys, jotta teksti täyttyy aina ...
Tämä on yhteenveto käännöksen jälkeen. Jos haluat tarkastella koko käännöstä, napsauta käännä-kuvaketta
As Mark said,
text-align:justify;
is the simplest solution. However, for short text, it won't have any effect. The following jQuery code stretches the text to the width of the container.It calculates the space for each character and sets
letter-spacing
accordingly so the text streches to the width of the container.If the text is too long to fit in the container, it lets it expand to the next lines and sets
text-align:justify;
to the text.Here is a demo :
This can be done with
text-align:justify
and a small hack. See here:Maybe this could help:
Even easier HTML/CSS method would be to use flexbox. It's also immediately responsive. But worth noting SEO won't pick it up if you were to use it as a h1 or something.
HTML:
CSS:
jsfiddle here
I found a better solution for text shorter than one line, without any extra js or tag, only one class.
A little late to the party, but for a simple, pure CSS implementation consider using
flexbox
:MARCH 2018 If you are landing on this question in a more current time...
I was attempting do do what the OP asked about but with a single word and found success with the following:
1. Use a
<span>
and set css:span { letter-spacing: 0px; display:block}
(this makes the element only as wide as the content)2. On load capture the width of the span
let width = $('span').width();
3. Capture the length of the span
let length = $('span').length;
4. Reset the width of the span to the container
$('span').css({'width','100%'});
5. Capture the NEW width of the span (or just use the container width)
let con = $('span').width();
6. Calculate and set the letter spacing to fill the container
$('span').css({'letter-spacing':(cont-width)/length})
Obviously this can be converted to use vanilla js and it is useful even with most font styles including non mono-space fonts.
Some of the above answers work well but the text has to wrap with many child div
and it's a lot of extra codes.
The text inside the div should be clean, so I made it with a help of JS.
I think what you're actually looking for is scaling.
Render your font with some nice-looking resolution that makes sense and then use JS to stretch it to the container by using css transforms:
The benefits of this is that your text will always wit, but you run into the problem of it becoming too small to be readable.
Quick note is that you'll need to make the element you're trying to shrink/expand have absolute position:
With the lineclamp module I wrote you can accomplish what you want. You set a max number of lines or height you want text to take up and it finds the max font size that will fit in that number of lines, and/or trims text to make it fit.
Working example: https://codepen.io/tvanc/pen/BaNmVXm
In the question, OP says letter spacing. In a comment, OP says letter spacing or font size.
So, still nibbling away at the edges of this problem, if you combine wener's answer that stretches short lines of text with this suggestion to use a real element from another thread, you can do the whole thing without needing an
:after
pseudo class.You're substituting in an actual element and placing it after the one you want to justify. And this will trigger the justify alignment in the same way:
If someone is interested on how to justify the content and center only the last line you can use
see the example:
jsfiddle link
For a purely HTML/CSS solution for when you are working with even shorter text (short enough that
wouldn't work)
I followed this great post as an inspiration, and placed each letter within a div
HTML:
CSS:
Samanlaisia ongelmia
Viimeaikaiset kysymykset
Licensed under cc by-sa 3.0 with attribution required.