{"id":7405,"date":"2010-09-12T14:12:45","date_gmt":"2010-09-12T21:12:45","guid":{"rendered":"http:\/\/css-tricks.com\/?p=7405"},"modified":"2022-09-22T11:49:56","modified_gmt":"2022-09-22T18:49:56","slug":"adding-stroke-to-web-text","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/adding-stroke-to-web-text\/","title":{"rendered":"Adding Stroke to Web\u00a0Text"},"content":{"rendered":"\n
Fonts on the web are essentially vector-based graphics. That’s why you can display them at 12px or 120px and they remain crisp and relatively sharp-edged. Vector means that their shape is determined by points and mathematics to describe the shape, rather than actual pixel data. Because they are vector, it would make sense if we could do things that other vector programs (e.g. Adobe Illustrator) can do with vector text, like draw a stroke around the individual characters. Well, we can! Example:<\/p>\n\n\n\n
h1 {\n \/* Prefix required. Even Firefox only supports the -webkit- prefix *\/\n -webkit-text-stroke-width: 1px;\n -webkit-text-stroke-color: black;\n}<\/code><\/pre>\n\n\n\n\n\n\n\nOr shorthand:<\/p>\n\n\n\n
h1 {\n -webkit-text-stroke: 1px black;\n}<\/code><\/pre>\n\n\n\nYou might be thinking “Cool, but if only some browsers support this, if I set my text color to white<\/code> and my background is white<\/code>, the stroke makes it look cool in supporting browsers but entirely disappears in non-supporting browsers!”<\/em><\/p>\n\n\n\nOne possibility is this:<\/p>\n\n\n\n
h1 {\n color: black;\n -webkit-text-fill-color: white; \/* Will override color (regardless of order) *\/\n -webkit-text-stroke-width: 1px;\n -webkit-text-stroke-color: black;\n}<\/code><\/pre>\n\n\n\nShown here with @font-face font Anime Ace 2 by Nate Piekos:<\/p>\n\n\n\nProperly stroked!<\/figcaption><\/figure>\n\n\n\nFallback to solid color. Shown here in Firefox<\/figcaption><\/figure>\n\n\n\nAnother possibility is only applying when supported:<\/p>\n\n\n\n
@supports (-webkit-text-stroke: 1px black) {\n h1 {\n -webkit-text-stroke: 1px black;\n -webkit-text-fill-color: white;\n }\n}<\/code><\/pre>\n\n\nSupport<\/h3>\n\nThis browser support data is from Caniuse<\/a>, which has more detail. A number indicates that browser supports the feature at that version and up.<\/p><\/div>Desktop<\/h4>