This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis
Today I learned there's a CSS property to align the last line of a rendered paragraph – text-align-last
. 😲
Initially, I couldn't see the value of this property because the last paragraph line always follows the overall text-align
property. If your text is left-aligned, the last line is left-aligned, too. The same applies to center
and right
.
So, what's the deal?
After researching the topic, I discovered that the property can be helpful with text-align: justify
. As discussed in a Tailwind issue, you can't style paragraphs with justified text and control the alignment of the last line without an additional CSS property.
With a combination of text-align
and text-align-last
this layout becomes possible.
p {
text-align: justify;
}
p.left {
text-align-last: left;
}
p.right {
text-align-last: right;
}
Justifying text might look good to you and your designer, but be aware that irregular text spacing fails the WCAG success criterion 1.4.8.
It's cool that the property supports logical values like start
and end
. These values react to the reading direction. Suppose you build a multilingual site that includes right-to-left and left-to-right languages. Using start
and end
makes the last line alignment react to the text direction. Sweet!
[Interactive component: visit the article to see it...]
If you wonder why the .
is flipping around in the demo when you change the reading direction. The Unicode character has the character Bidirectional Class “Other Neutral” defined. These characters can be moved around by the Bidirectional Algorithm.
So, how's the text-align-last
browser support? 👇
And even though a WebKit ticket claims there's a prefixed property -webkit-text-align-last
, it doesn't seem to work in Safari 15.4
.
If the only use case of this property is in combination with text-align: justify
, I'm very skeptical about it. Web performance and accessibility are close to my heart, so I haven't implemented justified text for ages.
Nevertheless, it's good to know that text-align-last
exists. I guess... 🤷♂️
Reply to Stefan
This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis
Stefan Judis | Sciencx (2022-03-19T23:00:00+00:00) How to align the text of the last paragraph line (#tilPost). Retrieved from https://www.scien.cx/2022/03/19/how-to-align-the-text-of-the-last-paragraph-line-tilpost/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.