Tailwind CSS Pseudo-elements

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Thanks for sharing, I didn't know this feature was now implemented in TailwindCSS. I'll make use of it now
Tailwind can probably do about 99% of the things you'll ever need.
Really blows my mind at this point 🤯
Yeah, me too Chris Bongers
Especially with the new way of adding styles using [...]
I can't remember what they called it, I'll need to check on that later.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

I only learned that Tailwind recently added the option to style pseudo-elements. Ever since the introduction of Tailwind JIT it turns out we can now also leverage pseudo-elements in Tailwind!
Let's look at how it works and what we can do with them.
If you are not aware of pseudo-elements, they are similar to pseudo-classes like :hover, :first, etc.
The difference is that pseudo-classes are existing elements that get styled differently.
As to where pseudo-elements are new elements.
They can give us the superpower to add new styled elements to the DOM.
Another way to identify pseudo-elements is to always start with two :: where the classes only use one :.
Let's look at each of the pseudo-elements and how we can use them in Tailwind CSS.
This pseudo-element can manipulate the first line of a specific sentence.
Let's say we want to make the first line of an article blue, so it pops a bit more. While we are at it, we could also transform the first line to uppercase.
<p class="first-line:uppercase first-line:tracking-widest first-line:text-blue-500">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.
</p>
This will result in the following:
Like the first-line selector, we can also target the first letter.
You often see this in those old-school books giving a nice effect.
I personally really love this effect, and this is how you use it in Tailwind CSS.
<p
class="first-letter:text-7xl first-letter:font-bold first-letter:mr-3 first-letter:float-left first-letter:text-teal-500"
>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.
</p>
The result of the first-letter will look like this:
The before pseudo-element is perfect for adding that extra new element to the dom, which you can use to add nice effects to certain elements.
Let's try and create a fun background for an image. We want the image to show, but there should be a different colored div with an angle on the background.
<div
class="relative before:block before:absolute before:-inset-1 before:-rotate-6 before:bg-teal-500"
>
<img class="relative border-4 border-white" src="img.jpg" />
</div>
Which will result in the following:
The after element can be used the same way as the before element. Let's try something else for this one.
We often have forms with required fields. Let's add a red * for the required fields.
<label class="block">
<span
class="after:content-['*'] after:ml-0.5 after:text-red-500 block text-sm font-medium text-gray-700"
>
Email
</span>
<input
type="email"
name="email"
class="mt-1 px-3 py-2 bg-white border shadow-sm border-gray-300 placeholder-gray-400 focus:outline-none focus:border-sky-500 focus:ring-sky-500 block w-full rounded-md sm:text-sm focus:ring-1"
placeholder="[email protected]"
/>
</label>
Resulting in this amazing piece:
I'm sure you have seen this before, you select a piece of text, and the color is different.
That is done by using the selection pseudo-element.
It looks like this:
<p class="selection:bg-teal-500 selection:text-white">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.
</p>
Try it out by selecting some text:
Now that we can use these selectors in Tailwind, there is almost no need for any custom CSS while using Tailwind.
I'm thrilled these are now so well supported, and I'm sure it will be a game-changer.
If you want to read up more, the official docs of Tailwind are always a gem of information.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter