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

Handling SVG fill and stroke

3m:24s

Tailwind has fill-* and stroke-* utilities that let you set the fill and stroke properties of an SVG, but they are not useful.

Why Tailwind’s fill and stroke utilities are not useful

They are supposed to help you colour your SVGs easily, according to Tailwind’s docs:

Tailwind docs showing a blue bell icon styled with the fill-blue-500 utility

But this only works for SVGs without children that have fill or stroke properties.

<!-- This works because <rect> doesn't have fill -->
<svg viewBox="0 0 100 50" class="fill-blue-700">
<rect width="100" height="50" />
</svg>
<!-- This doesn't work -->
<svg viewBox="0 0 100 50" class="fill-blue-700">
<rect fill="currentcolor" width="100" height="50" />
</svg>
Rect w/o fill
Rect with fill

To make fill-* work with such SVGs, you have to reach into the SVG and use fill-* directly on the affected child element… but it’s not worth the effort.

<!-- fill-blue-700 overwrites fill="currentcolor" -->
<svg viewBox="0 0 100 50">
<rect class="fill-blue-700" fill="currentcolor" width="100" height="50" />
</svg>

So you’re only going to find fill and stroke useful if you’re working with specialized icon sets without any fill or stroke.

Just change the text colour

I typically use SVGs through my Icon svelte component. It draws icons from Iconify, so I can switch between icon sets easily.

Here’s an example of how I’d use two different icons from Phosphor and Tabler.

<Icon icon="ph:user" client:idle />
<Icon icon="tabler:user" client:idle />

Phosphor user icon

Tabler user icon

They both use <path> to create the icon, but they do it differently:

  • Phosphor uses fill for the icon’s colour.
  • Tabler uses stroke for the icon’s colour.
HTML for Phosphor’s user icon
HTML for Tabler’s user icon

We can see from here that they both use currentColor to denote the icon’s colour — so adjusting the icon’s colour is as simple as changing the color of the element (or containing element).

<Icon class="text-red-500" icon="ph:user" client:idle />
<Icon class="text-blue-500" icon="tabler:user" client:idle />

Phosphor user icon

Tabler user icon

This is a simple way to make things work.

Sizing and aligning SVG icons

Icons need to be sized correctly or they may overpower the text they’re used with. Usually, a number between 0.8em and 1em is the right size.

<div class="horizontal items-center">
<svg class="text-[1em]">...</svg>
<div>Your text here</div>
</div>

0.9em

1em

Centering icons

If the icon and text are in a single line, you can center the items vertically with items-center. (This is built into horizontal by default).

If icons are paired with multiple lines of text, they should be aligned with margins instead. A simple way to do this is to set both the icon’s height and margin-block in lh.

<div class="horizontal">
<svg class="w-[1em] h-[0.8lh] my-[0.1lh]">...</svg>
<div>Your text here</div>
</div>

Lorem ipsum dolor sit amet consectetur adipisicing elit. Quam cum asperiores quod deleniti saepe illum molestias cumque facere sit repellat minima, magnam id, possimus maxime beatae? Dolore sit eum possimus?