Add text shadow with Tailwind CSS
<p class="drop-shadow">Lorem ipsum</p>
/* or */
<p class="[text-shadow:1px_1px_2px_var(--tw-shadow-color)] shadow-red-500">Lorem ipsum</p>
TailwindCSS does not "yet" have a dedicated text-shadow
utility class for creating text shadows. The current version at the time of writing this is 3.3.2.
Fortunately, there are several solutions to the problem.
Using drop-shadow
The simplest solution is to use the drop-shadow
class. You can use it with the usual size parameters, like sm, md, lg, xl
.
Problems with drop-shadow
Shadow location
In the background, this solution uses the drop-shadow() filter. This is interesting because the drop-shadow()
CSS function applies a drop shadow effect to the input image. By default, the image is the text itself, but as soon as you set a background color, the image becomes the full container and the shadow is not on the text, but on the square container that contains it.
Drop shadow without background color:
<p class="drop-shadow">Lorem ipsum</p>
Drop shadow with background color:
<p class="drop-shadow bg-red-100">Lorem ipsum</p>
Appropriate text shadow with drop-shadow class, using a wrapper:
<div class="bg-red-100">
<p class="drop-shadow">
Lorem ipsum
</p>
</div>
Shadow color
Another problem is changing the color of the shadow. Unfortunately, the shadow-[color]
solution used by box-shadow
does not work here by default.
Fortunately, by using arbitrary values we can solve this problem too. The inconvenience is that, in addition to the color, the offset-x and offset-y values must be specified before the color code. However, it is now possible to set the color using shadow-[color]
.
<div class="bg-red-100">
<p class="drop-shadow-[2px_2px_var(--tw-shadow-color)] shadow-blue-500">
Lorem ipsum
</p>
</div>
And the result is:
Using Tailwind arbitrary properties
If there is no CSS property in Tailwind, like text-shadow
in this case, you can also use square bracket notation to write completely arbitrary CSS:
<p class="[text-shadow:1px_1px_2px_var(--tw-shadow-color)] shadow-blue-500 bg-red-100">Lorem ipsum</p>
Creating custom plugin
Don't be scared by the title. TailwindCSS can be easily extended with your own plugins. All you need to do is extend the tailwind.config.js
file with a few lines of code.
Here are the steps you need to take:
- Import the plugin library into your config file
const plugin = require('tailwindcss/plugin')
- Extend the
theme
object with atextShadow
property with various size defaults:
textShadow: { sm: '1px 1px 2px var(--tw-shadow-color)', DEFAULT: '2px 2px 4px var(--tw-shadow-color)', lg: '4px 4px 8px var(--tw-shadow-color)', xl: '4px 4px 16px var(--tw-shadow-color)', },
- Finally add a new plugin to the
plugins
object:
plugin(function ({ matchUtilities, theme }) { matchUtilities( { 'text-shadow': (value) => ({ textShadow: value, }), }, { values: theme('textShadow') } ) } )
A complete tailwind.config.js
looks like this:
/** @type {import('tailwindcss').Config} */
const plugin = require('tailwindcss/plugin')
module.exports = {
content: ['**/*.html'],
theme: {
extend: {},
textShadow: {
sm: '1px 1px 2px var(--tw-shadow-color)',
DEFAULT: '2px 2px 4px var(--tw-shadow-color)',
lg: '4px 4px 8px var(--tw-shadow-color)',
xl: '4px 4px 16px var(--tw-shadow-color)',
}
},
plugins: [plugin(function ({ matchUtilities, theme }) {
matchUtilities(
{
'text-shadow': (value) => ({
textShadow: value,
}),
},
{ values: theme('textShadow') }
)
})
],
}
From now you can use text-shadow
utility class in your template files:
<p class="text-shadow-lg shadow-green-600 bg-red-100">Lorem ipsum</p>
- Create blinking background with TailwindCSS
- Create a neon effect with TailwindCSS
- Rotate element with TailwindCSS
- Change SVG icon color with TailwindCSS
- Using :not in TailwindCSS
- Specify exact width value in TailwindCSS
- Set element width over 100% using Tailwind CSS
- Stretch children to fill horizontally
- Create a fixed navbar with TailwindCSS
- Use font from local files in TailwindCSS
- Make an element invisible in mobile size in Tailwind CSS
- Create a 20/80 grid with Tailwind CSS
- Select the nth child in TailwindCSS
- Add class to child when hovering on parent in TailwindCSS
- Fill the full height of the screen in TailwindCSS
- Create transition with TailwindCSS
- Create horizontal rule with text using TailwindCSS
- Set background image with TailwindCSS
- How to use CSS variables in TailwindCSS
- Center an absolute element in TailwindCSS
- Remove arrows from number input in TailwindCSS
- How to use !important in TailwindCSS
- Access child elements in TailwindCSS
- Select first child in TailwindCSS
- Create semi transparent background in TailwindCSS
- How to use calc() in TailwindCSS
- Add text shadow with Tailwind CSS