i need to fit completely a text in a 100% width div container.
I attempted using letter-spacing
but it looks like only accepts px/em, and not percent values.. but that wont be responsive (e.g. resizing window).
This is what i got: http://jsfiddle.net/3N6Ld/
Should i take another approach? Any ideas? Thank you
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.
default letter-spacing: normal; The spacing between the characters is normal. letter-spacing: 2px; You can use pixel values.
The letter-spacing CSS property sets the horizontal spacing behavior between text characters.
You have to set 'display:inline-block' and 'height:auto' to wrap the content within the border. Show activity on this post. Two ways are there. No need to mention height in this it will be auto by default.
If you know how many letters you have you can sort of achieve this using the vw
(viewport width) unit.
In the below example I've used a value of 14.29vw
, as 100 (100% of the width of the window) divided by 7 (the number of letters in the word "CONTENT") is roughly 14.29.
html, body {
margin: 0;
height: 100%;
width: 100%;
}
.container{
background: tomato;
height: 10%;
width: 100%;
}
.content {
color: white;
letter-spacing: 14.29vw;
overflow: hidden;
}
<div class="container">
<div class="content">
CONTENT
</div>
</div>
If you want to make the "T" closer to the right edge you can increase the letter-spacing
a little. For Stack Overflow's code snippets, setting it to 14.67vw
does the trick:
html, body {
margin: 0;
height: 100%;
width: 100%;
}
.container{
background: tomato;
height: 10%;
width: 100%;
}
.content {
color: white;
letter-spacing: 14.67vw;
overflow: hidden;
}
<div class="container">
<div class="content">
CONTENT
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With