We often show a red *
to indicate a required field. This can be done by after
modifier.
Version
TailwindCSS 3.0
after
A red *
after Email
means the required field.
<!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>
<label class="block w-fit ml-2 mt-2">
<span class="after:content-['*'] after:ml-0.5 after:text-red-500 block text-sm font-medium text-slate-700">
Email
</span>
<input type="email" name="email" class="block w-full rounded-md sm:text-sm bg-white border shadow-sm border-slate-300 placeholder:text-slate-400 focus:outline-none focus:border-sky-500 focus:ring-sky-500 focus:ring-1 px-3 py-2 mt-1" placeholder="you@example.com" />
</label>
</body>
Line 10
<label class="block w-fit ml-2 mt-2">
block
: convert<label>
to block elementw-fit
: adjust the width automatically as the content widthml-2
: set left marginmt-2
: set top margin
Line 11
<span class="after:content-['*'] after:ml-0.5 after:text-red-500 block text-sm font-medium text-slate-700">
Email
</span>
after:content-['*']
: assign*
onafter
modifier, which sets the content of the pseudo elementafter:ml-0.5
: applyml-0.5
onafter
modifier, which sets the left margin of the pseudo elementafter:text-red-500
: applytext-red-500
on after modifer, which sets the text color of the pseudo elementblock
: convert<span>
to block elementtext-sm
: set text sizefont-medium
: set font weighttext-slate-700
: set text color
Line 14
<input type="email" name="email" class="block w-full rounded-md sm:text-sm bg-white border shadow-sm border-slate-300 placeholder:text-slate-400 focus:outline-none focus:border-sky-500 focus:ring-sky-500 focus:ring-1 px-3 py-2 mt-1" placeholder="you@example.com" />
block
: convert<input>
to block elementw-full
: set width as100%
of the parent widthrounded-md
: set the border radius of an elementsm:text-sm
: set text size on a small devicebg-white
: set background colorborder
: set border widthshadow-sm
: set outer shadowborder-slate-300
: set border colorplaceholder:text-slate-400
: set placeholder colorfocus:outline-none
: applyoutline-none
onfocus
modifier, which hides the default browser outline on the focused elementfocus:border-sky-500
: applyborder-sky-500
onfocus
modifier, which sets the border colorfocus:ring-sky-500
: applyring-sky-500
onfocus
modifier, whichs sets the color of an outline ringfocus:ring-1
: applyring-1
onfocus
modifier, which applies solid box-shadow of a specific thickness to an elementpx-3
: set the horizontal padding of thepy-2
: set the vertical paddingmt-1
: set the top margin
Conclusion
- Since
after
creates a pseudo element, we can useafter
just on<span>Email</span>
element