With new before
and after
modifiers, we can create lots of fun effects to decorate the element.
Version
TailwindCSS 3.0
before
Let’s try and create a fun background for an image.
<!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='flex items-center justify-center max-w-prose min-h-screen mx-auto'>
<div class='before:block before:absolute before:-inset-1 before:-rotate-6 before:bg-teal-500 relative'>
<img class='relative border-4 border-white' src="https://picsum.photos/300/200/?random=10">
</div>
</div>
</body>
Line 10
<div class='flex items-center justify-center max-w-prose min-h-screen mx-auto'>
flex
: make parent element as Flex box and child element as Flex itemitems-center
: all Flex items are vertical centerjustify-center
: all Flex items are horizontal centermax-w-prose
: set the width of max-widthmin-h-screen
: set height of max-heightmx-auto
: make Flex box horizontal center
Line 11
<div class='relative before:absolute before:-inset-1 before:-rotate-6 before:bg-teal-500'>
before:absolute
: create a pseudo element with absolute positionbefore:-inset-1
: create a pseudo element with top/right/bottom/left positionbefore:-rotate-6
: create a pseudo element with rotatingbefore:g-teal-500
: create a pseudo element with backgroundrelative
: set the element as relative position forabsolute
Line 12
<img class='relative border-4 border-white' src="https://picsum.photos/300/200/?random=10">
relative
: prevent the element from overlaping by the pseudo elementborder-4
: set the border widthborder-white
: set the border color
Conclusion
before
+absolute
+relative
is a common pattern for decorating the element