@arnoldo.moen
To add custom :before
and :hover
elements in Tailwind CSS, you can use the @layer
directive to define custom utility classes. Here's how you can add custom :before
and :hover
styles:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
@layer utilities { .custom-before::before { content: ''; display: block; position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); } .custom-hover:hover { background-color: #f4f4f4; } } |
1 2 3 4 |
<div class="relative"> <img src="image.jpg" alt="Image" class="w-full h-auto custom-hover" /> <div class="absolute top-0 left-0 z-10 w-full h-full custom-before"></div> </div> |
In this example, we have created two custom utility classes .custom-before::before
and .custom-hover:hover
using the @layer utilities
directive. The custom-before
class adds a semi-transparent overlay on top of an element using the ::before
pseudo-element, and the custom-hover
class changes the background color of an element on hover.
Make sure to compile your Tailwind CSS with PostCSS to process the custom utility classes. You can do this by running the npx tailwindcss-cli@latest build input.css -o output.css
command in your terminal.