Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Letter-spacing percent to completely fit the div container

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

like image 605
pumpkinzzz Avatar asked Jul 03 '14 15:07

pumpkinzzz


People also ask

How do I stretch text to fit a div?

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.

What is good letter-spacing in CSS?

default letter-spacing: normal; The spacing between the characters is normal. letter-spacing: 2px; You can use pixel values.

Which CSS property sets the amount of spacing between letters select the right padding to the top of an element for CSS property?

The letter-spacing CSS property sets the horizontal spacing behavior between text characters.

How do I make text fit in a box in CSS?

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.


1 Answers

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>
like image 142
James Donnelly Avatar answered Oct 15 '22 08:10

James Donnelly