We can use before
modifier to add a pseudo element on the fly and apply utility.
Version
TailwindCSS 3.0
before
TailwindCSS
is a real element on HTML, whereas Awesome
is a pseudo element created by before
modifier.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.tailwindcss.com"></script>
<title>TailwindCSS</title>
</head>
<body>
<div class="before:content-['Awesome'] before:text-red-500 before:mr-1">
TailwindCSS
</div>
</body>
Line 10
<div class="before:content-['Awesome'] before:text-red-500 before:mr-1">
TailwindCSS
</div>
before:content-['Awesome']
: assign String onbefore
modifier, which sets the content of the pseudo elementbefore:text-red-500
: applytext-red-500
utility onbefore
modifier, which sets the text color of the pseudo elementbefore:mr-1
: applymr-1
utility onbefore
modifier, which sets the right margin of the pseudo element
inline-block
The pseudo element created by before
modifier is inline-block
. We have to set it to block
to occupy a new line.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.tailwindcss.com"></script>
<title>TailwindCSS</title>
</head>
<body>
<div class="before:content-['Awesome'] before:text-red-500 before:block">
TailwindCSS
</div>
</body>
Line 10
<div class="before:content-['Awesome'] before:text-red-500 before:block">
TailwindCSS
</div>
before:content-['Awesome']
: assign String onbefore
modifier, which sets the content of the pseudo elementbefore:text-red-500
: applytext-red-500
utility onbefore
modifier, which sets the text color of the pseudo elementbefore:block
: applyblock
utility onbefore
modifier, which sets the element as a block-level element
Conclusion
- By using
before
modifier, we can decorate the element by creating a pseudo element using utilities