3

Hanging punctuation in CSS

 1 month ago
source link: https://adactio.com/journal/21027
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Hanging punctuation in CSS

April 3rd, 2024

There’s a lovely CSS property called hanging-punctuation. You can use it to do exactly what the name suggests and exdent punctuation marks such as opening quotes.

Here’s one way to apply it:

html {
  hanging-punctuation: first last;
}

Any punctuation marks at the beginning or end of a line will now hang over the edge, leaving you with nice clean blocks of text; no ragged edges.

Right now it’s only supported in Safari but there’s no reason not to use it. It’s a perfect example of progressive enhancement. One line of CSS to tidy things up for the browsers that support it and leave things exactly as they are for the browsers that don’t.

But when I used this over on The Session I noticed an unintended side-effect. Because I’m applying the property globally, it’s also acting on form fields. If the text inside a form field starts with a quotation mark or some other piece of punctuation, it’s shunted off to the side and hidden.

Here’s the fix I used:

input, textarea {
  hanging-punctuation: none;
}

It’s a small little gotcha but I figured I’d share it in case it helps someone else out.

8:19am

Tagged with css hanging punctuation typography typesetting frontend development property safari progressive enhancement forms inputs text

Older »

Have you published a response to this? Let me know the URL:

Responses

Paul Watson

Great tip! It’s a far less common use-case, but I’d also advise adding the output element to the list of elements where hanging-punctuation is re-set to none.

# Posted by Paul Watson on Wednesday, April 3rd, 2024 at 11:18am

David Bushell

@adactio I may or may not be quietly updating every website I’ve made in the last year

# Posted by David Bushell on Wednesday, April 3rd, 2024 at 11:32am

Lee Reamsnyder

@adactio I also have had to remove hanging-punctuation from pre blocks. I’d get unwanted horizontal scroll bars if a line of code started with a quotation mark or parentheses.

# Posted by Lee Reamsnyder on Wednesday, April 3rd, 2024 at 1:07pm

kolya

@adactio adactio.com is down.EDIT: It’s back.

# Posted by kolya on Wednesday, April 3rd, 2024 at 1:51pm

CM Harrington

@adactio FWIW, it seems like it may make more sense to consider hanging punctuation as an exception — so instead of applying it globally, apply it specifically to paragraphs, headlines, quotes, and block quotes. Because you probably have more rando places you have text where you don’t want it (ex: table cells, forms as you mention, etc).

# Posted by CM Harrington on Wednesday, April 3rd, 2024 at 2:35pm

Kevin Marks

@adactio do you know why it doesn’t create hanging hyphens?

# Posted by Kevin Marks on Thursday, April 4th, 2024 at 3:50pm

Luke Dorny

@adactio oh brilliant. Adding!

# Posted by Luke Dorny on Thursday, April 4th, 2024 at 8:20pm

4 Shares

# Shared by David Bushell on Wednesday, April 3rd, 2024 at 12:04pm

# Shared by Eric A. Meyer on Wednesday, April 3rd, 2024 at 2:06pm

# Shared by Tyler Sticka on Wednesday, April 3rd, 2024 at 2:32pm

# Shared by adrien on Thursday, April 4th, 2024 at 10:12am

16 Likes

# Liked by tristy on Wednesday, April 3rd, 2024 at 8:27am

# Liked by Evil Jim O’Donnell on Wednesday, April 3rd, 2024 at 9:02am

# Liked by Johannes Odland on Wednesday, April 3rd, 2024 at 9:02am

# Liked by Steve Randy Waldman on Wednesday, April 3rd, 2024 at 9:02am

# Liked by Andy Linton ✅ on Wednesday, April 3rd, 2024 at 10:01am

# Liked by Richard Rutter on Wednesday, April 3rd, 2024 at 10:01am

# Liked by Ethan Marcotte on Wednesday, April 3rd, 2024 at 10:32am

# Liked by Elly Loel ✨🌱 on Wednesday, April 3rd, 2024 at 11:03am

# Liked by Adam Perfect on Wednesday, April 3rd, 2024 at 11:28am

# Liked by strongest on Wednesday, April 3rd, 2024 at 11:28am

# Liked by Eric A. Meyer on Wednesday, April 3rd, 2024 at 2:05pm

# Liked by Tyler Sticka on Wednesday, April 3rd, 2024 at 2:32pm

# Liked by Lori Olson on Wednesday, April 3rd, 2024 at 3:31pm

# Liked by Troels Thomsen on Wednesday, April 3rd, 2024 at 8:47pm

# Liked by adrien on Thursday, April 4th, 2024 at 10:12am

# Liked by Luke Dorny on Thursday, April 4th, 2024 at 8:30pm


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK