Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Unorthodox Tailwind
Lesson Plan
  1. Introduction
    1. Introduction
    2. What Tailwind brings to the table
    3. Tailwind: The Good and The Bad
    4. The First and Most Important Step
    5. Simple Unorthodox System
    6. The System I Use
  2. Tailwind Fundamentals
    1. Introduction
    2. Installing Tailwind
    3. Using Tailwind utilities
    4. Understanding Tailwind’s utility system
    5. Understanding Tailwind values
    6. Understanding Tailwind variants
  3. Unorthodox Fundamentals
    1. Introduction
    2. Making Simple Tailwind Utilities
    3. Components vs Utilities
    4. Specificity, Cascade, and CSS Layers
    5. Debugging Tailwind Styles
    6. Composable Utility System
    7. Create lots of components
  4. Layouts System
    1. Tailwind is great for layouts
    2. Four Types of Layouts
    3. Building Shell Layouts
    4. Building Macro Layouts
    5. Better Responsive Variants
    6. A collection of maximum widths
    7. A note on grid wrappers
    8. Working with sections
    9. Building Micro Layouts
    10. Positioning Layouts
  5. Typography System
    1. Introduction
    2. A system for using multiple typefaces
    3. Tailwind's font-size system
    4. Finding your font-size system
    5. My font-size system
    6. A simple responsive typography system
    7. The main body
    8. Vertical rhythm
  6. Theming System
    1. Introduction
    2. Tailwind's colour system
    3. The pigment system
    4. Tailwind's gradient system
    5. A friendlier gradient system
    6. Handling SVG fill and stroke
    7. Improving Tailwind's colour palette
    8. A simple way to theme
  7. Useful Tools
    1. Introduction
    2. Making functional utilities
    3. Custom variants
    4. Scaffolding tools
    5. Extending Tailwind
  8. Effects System
    1. Introduction
    2. Vertical text
    3. Strikethrough text
    4. Text stroke
    5. Highlighting text
    6. Nicer shadows
    7. Transitioning shadows
    8. Drop shadows
    9. Dark mode tricks
    10. Gradient borders
    11. More effects to explore
  9. Putting Everything Together
    1. The Unorthodox Tailwind process
    2. The 4 systems combined
    3. Create components liberally
    4. Advanced tips
    5. My CSS Structure
    6. A Cheatsheet
    7. Consolidating your thoughts
    8. Parting words

Text stroke

5m:22s

Let’s do one more text utility before we go into something more complex. In this case, we are going to make one that creates a text stroke.

Text Stroke

Don’t belittle this simple text-stroke. It can be used to create impressive text effects like the one you see below!

Creating a text stroke

Unlike what you might imagine, I am not a CSS expert that can conjure all these kinds of strange techniques up by myself. These techniques have already existed on the interwebs, created by others; I just happened to find and repurpose them.

For this text stroke effect, a quick search on Google suggests that Chris Coyier had already dug this up back in 2010!

You’ll want to read that article to get a sense of how to use it, but to keep things short, you need the following CSS to create a text stroke.

.text-stroke {
color: black; /* The fallback color */
-webkit-text-fill-color: orange; /* The fill color (overrides color) */
-webkit-text-stroke-width: 1px; /* Stroke width */
-webkit-text-stroke-color: black; /* Stroke color */
}

Text stroke

This is a great start — but there’s something wonky about the overlapping strokes, so it didn’t quite work out. After a bit more research, I found that you also need paint-order to get the strokes to render right.

.text-stroke {
/* ... */
paint-order: stroke fill;
}

Essentially, this is saying:

  1. Paint the stroke first
  2. Then the fill.

So fill should cover stroke.

Text stroke

But this created another problem — the stroke seems to disappear!

Upon closer inspection (and rereading Chris’ article), I realized the stroke seems to disappear because we only have 0.5px worth of stroke on the outside — and that’s because half of the stroke is rendered outside the text.

We need to bump it up a few notches if we want the stroke to be visible.

.text-stroke {
/* This has 2px of stroke outside */
-webkit-text-stroke-width: 4px;
/* ... */
}

Text stroke

Nice.

Creating the utility

At this point, we know we need the following CSS to create a text stroke.

.text-stroke {
color: black; /* The fallback color */
-webkit-text-fill-color: orange; /* The fill color (overrides color) */
-webkit-text-stroke-width: 1px; /* Stroke width. Should be min 2px */
-webkit-text-stroke-color: black; /* Stroke color */
paint-order: stroke, fill;
}

We can create a few CSS variables to house each of these values to make text-stroke easy to use. We can also put in a few guardrails when we are doing that.

  • We can ensure that the stroke-width is a minimum of 2px, so at least 1px of stroke width is on the outside.
  • And we can have a fallback color (that is the same as stroke-color) if this is not supported.
@utility text-stroke {
color: var(--stroke-color, currentcolor);
-webkit-text-fill-color: currentcolor;
-webkit-text-stroke-width: max(var(--stroke-width, 0.1em), 2px);
-webkit-text-stroke-color: var(--stroke-color);
paint-order: stroke fill;
}

Now we can use text-stroke like this:

<div
class="text-stroke text-orange-500"
style="--stroke-color: black; --stroke-width: 4px"
>
...
</div>

Text stroke

Nice.

Using text-stroke with other text

I got inspired to build text stroke when I created the first version of SplendidLab’s landing page.

Here, I want some of the header text to be an outline, while the others are standard coloured text. This helps to create good visual contrast and makes things interesting.

What I discovered at this point is that words that use text-stroke seem larger than the words that don’t use it.

This makes sense, because some stroke-width is created outside of the text. So words that use text-stroke would automatically seem bigger. You can see that in this demo below — I deliberately enlarged the stroke to make this visible.

With Stroke and Without Stroke

One way to fix this is to reduce the font-size by half of the stroke-width. Then the sizes can become even again.

@utility text-stroke {
font-size: calc(1em - var(--stroke-width, 0.1em) / 2);
/* ... */
}

With Stroke and Without Stroke

Functional Utilities for text-stroke

Building this utility should look very familiar to you at this point because we have been doing the same things over the last two lessons.

Except, we have a problem: Tailwind already uses stroke-* to output values for SVG stroke and stroke-width properties so we can’t use the same namespace.

I thought about it for a while, but the best I came up with is spl-stroke since I’m putting all these effects under Splendid Labz anyway. So I went ahead with it.

@utility spl-stroke-* {
/* For stroke-width */
--stroke-width: --spacing(--value(number));
--stroke-width: --value([length]);
/* For stroke-color */
--stroke-color: --value(--color-*, [color]);
--stroke-color: oklch(
from --value(--color-*, [color]) l c h / --modifier(number)%
);
}

Creating the text effect

Now we can take a stab at creating the text effect I showed you above! This can be achieved by using text-stroke and drop-shadow.

<div
class="text-purple-900 text-stroke spl-stroke-orange-500 spl-stroke-0.5"
style="filter: drop-shadow(0 0 0.25em var(--color-orange-500));"
></div>

Hey there

Now a further question might have arisen in your mind: How do you manipulate drop-shadow easily without writing the entire filter function? Well, we’ll get to that in a bit, because that will take us on a foray into shadows.

But first, let’s look at a final text highlight effect.