We can use after
modifier to add a pseudo element on the fly and apply utility.
Version
TailwindCSS 3.0
after
TailwindCSS
is a real element on HTML, whereas Rocks
is a pseudo element created by after
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="after:content-['Rocks'] after:text-red-500 after:ml-1">
TailwindCSS
</div>
</body>
Line 10
<div class="after:content-['Rocks'] after:text-red-500 after:ml-1">
TailwindCSS
</div>
after:content-['Rocks']
: assign String onafter
modifier, which sets the content of the pseudo elementafter:text-red-500
: applytext-red-500
utility onafter
modifier, which sets the text color of the pseudo elementafter:ml-1
: applyml-1
utility onafter
modifier, which sets the left margin of the pseudo element
inline-block
The pseudo element created by after
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="after:content-['Rocks'] after:text-red-500 after:block">
TailwindCSS
</div>
</body>
Line 10
<div class="after:content-['Rocks'] after:text-red-500 after:block">
TailwindCSS
</div>
after:content-['Rocks']
: assign String onafter
modifier, which sets the content of the pseudo elementafter:text-red-500
: applytext-red-500
utility onafter
modifier, which sets the text color of the pseudo elementafter:block
: applyblock
utility onafter
modifier, which sets the element as a block-level element
Conclusion
- By using
after
modifier, we can decorate the element by creating a pseudo element using utilities