Stretch text to fit width of div
This can be done with text-align:justify
and a small hack. See here:
div{
background-color:gold;
text-align:justify;
}
span{
background-color:red;
width:100%;
height:1em;
display:inline-block;
}
<div>
Lorem ipsum sit dolor
<span> </span>
</div>
The trick is to add an element after the text that pretends to be really long word. The fake word is actually a span
element with display:inline-block
and width:100%
.
In my example the fake word is in red and given a height of 1em, but the hack will work even without it.
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 :
$.fn.strech_text = function(){
var elmt = $(this),
cont_width = elmt.width(),
txt = elmt.html(),
one_line = $('<span class="stretch_it">' + txt + '</span>'),
nb_char = elmt.text().length,
spacing = cont_width/nb_char,
txt_width;
elmt.html(one_line);
txt_width = one_line.width();
if (txt_width < cont_width){
var char_width = txt_width/nb_char,
ltr_spacing = spacing - char_width + (spacing - char_width)/nb_char ;
one_line.css({'letter-spacing': ltr_spacing});
} else {
one_line.contents().unwrap();
elmt.addClass('justify');
}
};
$(document).ready(function () {
$('.stretch').each(function(){
$(this).strech_text();
});
});
p { width:300px; padding: 10px 0;background:gold;}
a{text-decoration:none;}
.stretch_it{ white-space: nowrap; }
.justify{ text-align:justify; }
.one{font-size:10px;}
.two{font-size:20px;}
.three{font-size:30px;}
.four{font-size:40px;}
.arial{font-family:arial;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="stretch one">Stretch me</p>
<p class="stretch two">Stretch me</p>
<p class="stretch three">Stretch <a href="#">link</a></p>
<p class="stretch two">I am too slong, an I would look ugly if I was displayed on one line so let me expand to several lines and justify me.</p>
<p class="stretch arial one">Stretch me</p>
<p class="stretch arial three">Stretch me</p>
<p class="stretch arial four">Stretch me</p>
<p class="arial two">Don't stretch me</p>
Js fiddle demo
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:
<div class="wrapper">
<div>H</div>
<div>e</div>
<div>l</div>
<div>l</div>
<div>o</div>
<div> </div>
<div>W</div>
<div>o</div>
<div>r</div>
<div>l</div>
<div>d</div>
CSS:
.wrapper{
width: 100%;
text-align: center;
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
border: 1px solid black;
}
jsfiddle here
Maybe this could help:
text-align:justify;