Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centre align text that has extra letter spacing applied

Tags:

css

alignment

What's the best way to centre align some text that has an increased letter-spacing so that it's actually in the centre of its container? The text is in an h5 tag that is centre aligned within the parent div but sits noticeably to the left because letter-spacing applies to the right of each letter. I could manually add some left padding but that wouldn't stay consistently aligned if the browser zoom level was changed.

Is there a way to calculate the spacing between each letter, either using css or JavaScript? Is there a better way to insert spacing between letters so everything is centrally aligned?

The container div already has 20px padding on all sides so I really need to add 0.75em to the left padding. Here's the relevant css for the h5 and container tags:

.container {
    padding: 20px;
    float: left;
}

.container h5 {
    font-size: 33px;
    letter-spacing: 1.5em;
    text-align: center;
}

The container looks like this:

container

like image 704
levelnis Avatar asked Feb 03 '13 22:02

levelnis


People also ask

How do I remove the space between letters in CSS?

In this CSS letter-spacing example, we have removed 1px of space between the characters by setting a -1px value for the letter-spacing property. This removes some of the character spacing that has been added by the selected font.

How do you remove the letter space?

If you want to remove space between letters in word, Select the text, right-click, choose Font, and select the Advanced tab. Change the letter spacing as required.

What is a letter-spacing?

Letterspacing (also known as character spacing or tracking ) is the adjustment of the horizontal white space between the letters in a block of text. Unlike kerning, which affects only designated pairs of letters, letterspacing affects every pair.


1 Answers

Can't you just specify padding-left: 1.5em; for your .container h5?

like image 119
Matthew Strawbridge Avatar answered Nov 15 '22 11:11

Matthew Strawbridge