Unicode CSS Classes
CSS class name structure and consistency is really important; some developers camelcase classnames, others use dashes, and others use underscores. One thing I've learned when toying around by HTML and CSS class names is that you can actually use unicode symbols and icons as classnames. Check this out!
The HTML and CSS
There's only one way to add a classname with HTML so you'll do it that way, of course:
<!-- place this within the document head -->
<meta charset="UTF-8" />
<!-- error message -->
<div class="ಠ_ಠ">You do not have access to this page.</div>
<!-- success message -->
<div class="❤">Your changes have been saved successfully!</div>
...and there's only one way to declare styles for a given class:
.ಠ_ಠ {
border: 1px solid #f00;
background: pink;
}
.❤ {
background: lightgreen;
border: 1px solid green;
}
Wild that you can use unicode classnames for elements, right? Of course I don't recommend doing so, but you can if you'd like to!
As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us. Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos. Another technology available...
You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...
I often incorporate tools into my customers' websites that allow them to have some control over the content on their website. When doing so, I offer some tips to my clients to help them keep their website in good shape. One of the tips...
The HTML5 revolution has provided us some awesome JavaScript and HTML APIs. Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers. Regardless of API strength or purpose, anything to help us better do our job is a...
David, this is awesome :). Could you please highlight the cons of using this and why you wouldn’t recommend it?
I think the main issue would be that they would be more difficult to maintain due to not everyone knowing the shortcuts to type ❤ for example. Plus support may be patchy in earlier browsers.
To my big suprise while heart-shaped-symbol class is styled properly (and displayed properly as heart-shaped-symbol in the console), heart-shaped-symbol is not rendered properly in div content on Chrome 26, encoding utf-8.
It’s especially weird when I’m looking at this page, as I see as heart-shaped-symbol, and class definition .❤ is rendered as empty square.
Any idea what this is happening?
Works for me on Mac / current Chrome.
Actually I knew about this possibility when I had to deal with a in a class name (yes, the empty space). This is totally awesome but, unfortunately, not professional looking at all :)
I may change my mind on the last part, though…
Mathias Bynens has written about this some time ago, check it out: http://mathiasbynens.be/notes/html5-id-class He also wrote about how to escape these characters in CSS: http://mathiasbynens.be/notes/css-escapes
FYI your HTML comments in the samples are malformed. “!” is missing.
I’ve done a lot of testing of Unicode support across browsers, and there’s a massive amount of variation.
The results are all available at unicode.johnholtripley.co.uk
How to type these unicode characters?
FYI this has been added to the css-modules project (https://github.com/css-modules/css-modules/issues/119). While it’s not recommended to use unicode characters for development, it seems like a useful way to minify and obfuscate class names for production builds if you’re looking to save a few bytes.
On a tangent, I usually don’t like to put special characters in my HTML but instead use HTML entities. I feel like text editors choke sometimes and/or encoding gets messed up sometimes when viewing. Is this just my paranoid dev side trying to be tidy or is this a real problem?