Terence Eden. He has a beard and is smiling.
Theme Switcher:

Targetting specific characters with CSS rules

· 9 comments · 450 words · Viewed ~7,652 times


You can't. There is no way to use CSS to apply a style to every letter "E". It simply can't be done.

At least, that's what they want you to think…

What if I told you there was a secret and forbidden way to target specific characters in text and apply some styles to them?

As part of my experiments in creating a "drunk" CSS theme, I thought it would be useful to change the presentation of specific characters. Wouldn't it be fun to have every letter "a" look slightly different to the rest of the text?!

So here's how you can apply limited CSS styles to certain characters while leaving the rest of the text unchanged, and without having to wrap characters in extra markup.

 CSS@font-face {
    font-family: "Different";
    src: url("whatever.woff2") format("woff2");
    /* Lower-Case Vowels */
    unicode-range: U+61, U+65, U+69, U+6F, U+75 ;
}
body {
    font-family: "Different", sans;
}

This creates a new font-family called "Different". It loads a unique font. It is applied to specific Unicode characters - in this case: a, e, i , o, and u.

The body places this font-family first and then defaults to a different family. This means all the lower-case vowels will use one font, and every other character will use something else.

That's… OK. I guess? Having certain characters as Garamond and the others as Times New Roman isn't exactly exciting, is it?

Sadly, there only other thing we can do in CSS to spice things up is to monkey around with size-adjust which lets the text be scaled up or down.

But modern fonts are pretty magic, you know!

The WOFF2 format has a new(ish) COLR table which allows you to create multi-coloured fonts. That means it is possible to target specific characters and have them display in living colour.

For example, using this colourful pixel font by Patrick H. Lauke (CC BY), I can target the Unicode Range of upper-case characters.

The above CSS only changes the appearance of UPPER Case characters!

To wrap things up - yes, you can target specific characters with CSS rules. Sadly, you're pretty much limited to fiddling around with their fonts.


Share this post on…

9 thoughts on “Targetting specific characters with CSS rules”

  1. Very cool! I use this technique to load only the Hebrew and Arabic fonts from some monospaced fonts for editing Wikipedia without forcing me to use those fonts' Latin characters, but I'd never thought to use it for something like the range of capital letters.

    (I also didn't know I could use size-adjust in those rules. I'm gonna have to experiment with that!)

    Reply

  2. @Edent Historically, it was *very* (well, kinda) common to embellish ampersands this way. (Ironically, the "nice" ampersand was often pushed to fonts that had nearly identical ampersand to the one "borrowed" built-in, under some style-set. But it's a different story.) One remark: usually, it is not needed to make an extra font-family including the single face containing the tiny character subset: you can just subset the ranges in the **same** "main" family through a "ranged" face.

    Reply | Reply to original comment on typo.social

  3. Wow! Didn't know this was possible. Curious whether combining this technique with :has() would allow for real-time validation of dynamic or user-generated content.

    Reply

What links here from around this blog?

  1. Screenshot of the homepage all askew.Drunk CSS

What are your reckons?

All comments are moderated and may not be published immediately. Your email address will not be published.

See allowed HTML elements: <a href="" title="">
<abbr title="">
<acronym title="">
<b>
<blockquote cite="">
<br>
<cite>
<code>
<del datetime="">
<em>
<i>
<img src="" alt="" title="" srcset="">
<p>
<pre>
<q cite="">
<s>
<strike>
<strong>

To respond on your own website, write a post which contains a link to this post - then enter the URl of your page here. Learn more about WebMentions.