Is there a straightforward way to horizontally center text that works even when really long words need to stretch out into the surrounding padding?
For example, the following:
HTML
<section>
<p>
Some animals in the wild, like
hummingbirds, have long names.
</p>
</section>
CSS
section p {
width: 75px;
text-align: center;
background: gold;
padding: 20px;
}
Results in “hummingbirds” being off-centered:
If the word “hummingbirds” was properly centered, it would fit nicely within the gold box.
Fiddle with it:
https://jsfiddle.net/m6h37q2z/
ADDED NOTE #1:
This question is NOT about how to remove the padding or reduce the font size or make the box bigger… those are all very easy tweaks (and would break the design). This question is about how to center the word “hummingbirds”.
ADDED NOTE #2:
For those who are saying there is not enough room, here’s a photoshopped screenshot proving that there is enough room:
6
Answers
I ended up using a simplified version of Aziz's solution (which I accepted as the answer).
If you want something really simple (but requires manually wrapping the long overflowing words in
span
tags), here's the solution:HTML
CSS
Works great:
https://jsfiddle.net/m6h37q2z/8
Pure CSS (no Photoshop):
You have limited the width of
<p>
to75px
only. But the space required for the wordhummingbirds,
is more than75px
, that’s why it can’t fit in there. Try increasing the width of the<p>
You have but only so many options when it comes to this. You could change the font-size, or change the width. You could change overflow to either hidden or scroll. I am guessing though, that none of these really appeal to what you are trying to do. You could change the padding as well to compensate.
I don’t think that there is a straightforward to do this but there certainly are ways.
You can achieve this by using flexbox and wrapping some sections of your text with some
<span>
tags like so:Then you can add your styling like so:
As you can see I have changed a few things. Instead of adding
20px
ofpadding
I have made the width of the parent container40px
larger. because the<section>
tag is using thedisplay: flex
property withjustified-content
the child<p>
tag will be centered in the parent<section>
(theflex-direction
is defaultrow
.Furthermore, each
<span>
within<p>
is also center aligned (usingalign-items
which aligns along the cross axis – theflex-direction
iscolumn
.Here is the fiddle: https://jsfiddle.net/o0nq9pgt/
Here is the result:
Well, if the box size can’t be altered, the type size can’t be altered…. to be honest I don’t know how a word sticking out of it’s container, even if it’s centered, stays in line with any design but…..
One possible solution is to break long words…
Fiddle
Grammatically it can be iffy, but visually it’ll be consistent.
A less elegant solution may be to use a background image for the yellow (or a 1 color linear gradient) and then widen the paragraph, but position the 75px wide background centered in the (wider) paragraph.
Solution 3….
wrap long words in a
<span>
and then move the span…..Fiddle update
or…
Fiddle Update Here you’d just need to wrap long words in a span… then the CSS should automatically overcome the padding.
I find this visually horrible and I’m a designer.
To the argument that “there’s enough room”… well look above.. is there? It fits, sure, but by 1 pixel. And that’s on my browser* on my system using my monitor. No clue how it may or may not work out on other displays. Logically it should be the same as what I see, but without testing who knows. That would not be acceptable to me.
The best, logical solution is to RETHINK THE DESIGN and either widen the box, use shorter words, or reduce the font-size.
Here is another approach using negative
letter-spacing
and using jQuery to detect long words (more than 8 chars) and wrapping them in a special span that is centered using negativemargin
anddisplay: block
:https://jsfiddle.net/azizn/bf52mwgr/
Also, If you want ridiculously long words to be broken, you could use this code, which will break words longer than 15 characters and restore the
inline
display:jsFiddle: https://jsfiddle.net/azizn/8w3w2zh1/