Emoji for Developers: Unicode, HTML, CSS, JavaScript and Storing Emoji in a Database
A practical guide to handling emoji in web pages and apps: code points and emoji sequences, HTML entities, CSS content, JavaScript string length and Intl.Segmenter, regex, MySQL utf8mb4, URL and JSON encoding, fonts and accessibility, with examples.
Emoji look like pictures, but inside a program they're text. That means there are moments when they don't behave like ordinary letters: when you count characters, store them in a database or cut a string short. This guide walks through common problems people building websites and apps run into with emoji, in roughly the order you'll meet them.
Every emoji page on EmojiCanvas has a βDeveloper codesβ section near the bottom with the code points, HTML hex and decimal entities, CSS, JavaScript and URL-encoded values, each with a copy button. It's handy to keep one open while reading.
One emoji can be several code points
Unicode gives every character a number called a code point. π is a simple emoji made of a single code point, U+1F600. But a lot of things that appear on screen as one emoji are actually sequences of several code points.
- Emoji presentation selector (VS16): β€οΈ is
U+2764 U+FE0F. The trailingU+FE0Fasks for the colorful emoji style; without it, some systems show a plain black heart symbol. - Skin tone modifiers: ππ½ is π (
U+1F44D) followed by a skin tone modifier (U+1F3FD). - ZWJ sequences: π¨βπ©βπ§ is man, woman and girl joined by an invisible zero width joiner (
U+200D), five code points in all. Systems that don't support that combination show three separate people. - Keycaps: 1οΈβ£ is the digit 1 (
U+0031),U+FE0Fand the keycap frame (U+20E3).
The guide on how combined emoji work explains this with pictures. The one thing to remember while coding: what a person sees as one character and what your program counts as characters can be different numbers.
Putting emoji in HTML
The simplest approach is to type the emoji straight into your HTML. For that to work, the document needs <meta charset="utf-8"> inside the head, and the file itself has to be saved as UTF-8. If either one is off, emoji turn into garbled characters.
If you can't be sure of the file encoding, use numeric character references. π is 😀 in hex or 😀 in decimal. Sequences need one reference per code point, so β€οΈ becomes ❤️.
Using emoji in CSS
To put an emoji in a pseudo-element, use a backslash and the hex value in the content property: content: "\1F600"; for π. Sequences are chained, so β€οΈ is content: "\2764\FE0F";.
To make sure emoji are drawn with the system's color emoji font, add emoji fonts at the end of your font stack. A common choice is "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji". Without them, some environments pick up a black-and-white symbol from a regular font first.
JavaScript string length
JavaScript strings are stored as UTF-16 code units. Code points above U+FFFF, like π, take two units (a surrogate pair), so "π".length is 2.
- To count code points, spread the string:
[..."π"].lengthis 1. - Even so,
[..."π¨βπ©βπ§"].lengthis 5. To count what a person sees as one character, split the text into graphemes withIntl.Segmenter, for example[...new Intl.Segmenter().segment(text)].length. - Enforcing a character limit with
slice()orsubstring()can cut through a surrogate pair or a sequence and leave a broken character (οΏ½). When trimming a preview, segment first and then cut. - When working with code points as numbers, use
String.fromCodePoint(0x1F600)andcodePointAt(0). The olderfromCharCodemakes you build surrogate pairs by hand.
Finding emoji with regular expressions
Hand-written ranges for βmatch any emojiβ are easy to get wrong. Unicode properties are more reliable.
/\p{Extended_Pictographic}/umatches individual pictographic characters. Characters like1or#, which are the base of keycap emoji, aren't included.- Current browsers and Node.js support the
vflag and/\p{RGI_Emoji}/v, which matches whole sequences like π¨βπ©βπ§ as a single emoji. - Support varies between server-side languages, so test with real data that includes sequences, skin tones and keycaps.
Storing emoji in MySQL: use utf8mb4
MySQL's old utf8 character set (now called utf8mb3) stores at most 3 bytes per character. Emoji take 4 bytes in UTF-8, so saving them there either fails with an βIncorrect string valueβ error or turns them into question marks.
- Set tables and columns to
utf8mb4with a matching collation such asutf8mb4_unicode_ciorutf8mb4_0900_ai_ci. - The connection character set has to be
utf8mb4too. Changing only the tables while leaving the connection settings alone still breaks things on the way in. - When you set a length like
VARCHAR(20), check whether it counts bytes or characters. π¨βπ©βπ§ looks like one character but is 5 code points and 18 bytes in UTF-8.
PostgreSQL stores emoji without extra setup as long as the database encoding is UTF8.
Length means different things in different languages
The same emoji gets counted differently depending on the language.
- Python 3's
len()counts code points:len("π")is 1,len("π¨βπ©βπ§")is 5. - Java and C# strings are UTF-16 like JavaScript, so π has a length of 2.
- If you need bytes, encode to UTF-8 and count. π is 4 bytes.
For any input with a character limit, make sure the front end and the server count the same way. That's where the classic βpassed in the browser, rejected by the server as too longβ bug comes from.
Emoji in URLs and JSON
In a URL, an emoji becomes its UTF-8 bytes in percent-encoding: π is %F0%9F%98%80. In JavaScript, encodeURIComponent() does this for you.
JSON can carry emoji as-is, but some libraries output surrogate pair escapes like \ud83d\ude00. Python's json.dumps() does this by default; pass ensure_ascii=False to keep the emoji as written. Both forms decode to the same character, so compare decoded values rather than raw JSON text.
The device decides what it looks like
The code is the same everywhere, but the picture comes from the emoji font of the OS or app. That's why one emoji looks different across devices, and why newer emoji can show up as empty boxes (β‘) on older systems. The guide on emoji that show as boxes covers the causes.
If every user must see the same artwork, you can render emoji as images instead. Check the license when you do. Google Noto Emoji artwork is Apache 2.0 (the font files are OFL 1.1), Twemoji is CC BY 4.0 (attribution required) and Microsoft Fluent Emoji is MIT. Apple's and Samsung's emoji artwork isn't openly licensed, so it can't be reused. Each emoji page on EmojiCanvas lets you download PNG and SVG files of those three designs.
Accessibility: screen readers read the name
Screen readers announce an emoji by its name. Put π₯π₯π₯ in a heading and listeners hear βfire, fire, fire.β
- Wrap purely decorative emoji in
<span aria-hidden="true">so they're skipped. - When an emoji works as an icon with meaning, give it a label that fits the context, such as
<span role="img" aria-label="Done">β </span>. - A button labeled with a single emoji is hard to understand for screen reader users. Add a text label as well.
Pre-launch checklist
- Are your documents and server responses declared as UTF-8?
- Are tables, columns and the connection character set all utf8mb4?
- Do character limits and string trimming survive π¨βπ©βπ§, ππ½ and 1οΈβ£?
- Do the front end and server count length the same way?
- If you use emoji images, are you meeting the license terms, including attribution?
- Are decorative emoji kept from cluttering what screen readers announce?
Last checked 2026-11-06