Hanging punctuation in CSS

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.

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

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.

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).

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

Related posts

Who knows?

Had you heard of these bits of CSS? Me too/neither!

Making the Patterns Day website

The joy of getting hands-on with HTML and CSS.

Supporting logical properties

Using the CSS trinity of feature queries, logical properties, and unset.

Alternative stylesheets

Why do browsers that don’t implement stylesheet switching still download alternative stylesheets?

Declarative design

Defining the inputs instead of trying to control the outputs.

Related links

Retrofitting fluid typography | Clagnut by Richard Rutter

Here’s a taste of what Rich will be delivering at Patterns Day on Thursday—can’t wait!

Tagged with

CSS Nesting and the Cascade | WebKit

As well as a very welcome announcement, Jen has a really good question for you about nesting in CSS.

If you have an opinion on the answer, please chime in.

Tagged with

The perfect link - The A11Y Collective

How do we write, design, and code a link that works for everyone on every device? Let’s dive into the world of creating the perfect link, without making a pig’s breakfast of it.

Tagged with

Some simple ways to make content look good - Set Studio

This is a terrific walkthrough from Andy showing how smart fundamentals in your CSS can give you a beautiful readable document without much work.

Tagged with

The Web Needs a Native .visually-hidden

I agree with the reasoning here—a new display value would be ideal.

Tagged with

Previously on this day

3 years ago I wrote Principles and the English language

Mashing up George Orwell with axioms of web architecture.

6 years ago I wrote Designing Progressive Web Apps by Jason Grigsby

A presentation at An Event Apart Seattle 2018.

6 years ago I wrote Navigating Team Friction by Lara Hogan

A presentation at An Event Apart Seattle 2018.

6 years ago I wrote Announcing Going Offline from A Book Apart

I’ve written a book about service workers for web designers. You can pre-order it now.

6 years ago I wrote Everything You Know About Web Design Just Changed by Jen Simmons

A presentation at An Event Apart Seattle 2018.

9 years ago I wrote 100 words 012

Day twelve.

15 years ago I wrote Let the right tweet in

Horror and cosiness.

16 years ago I wrote FooVid

Voxpop videos from the Social Graph Foo Camp.

22 years ago I wrote Diminished Responsibility

Here we go again: