Published on

How to Use Before and After in Tailwind CSS?

Authors
  • Name
    Ripal & Zalak
    Twitter

How to Use Before and After in Tailwind CSS?

The ::before and ::after pseudo-elements are powerful tools for adding decorative elements or additional content to your components without modifying your HTML structure. Tailwind CSS provides utilities to handle these pseudo-elements effectively.

In this guide, we'll explain how to replicate the functionality of ::before and ::after from standard CSS in Tailwind CSS. We'll also provide a detailed example to help you get started.

Standard CSS Example

Below is a basic example of a CSS class using ::before and ::after:

.card {
  position: relative;
  background-color: grey;
  display: block;
  width: 300px;
  min-height: 90px;
  cursor: pointer;
  padding: 15px;
  margin: calc(50vh - 30px) auto 0 auto;
}

.card::before {
  content: '';
  position: absolute;
  left: -5px;
  top: -5px;
  width: calc(100% - 25px);
  height: calc(100% - 10px);
  border-top: 5px solid brown;
  border-left: 5px solid brown;
}

.card::after {
  content: '';
  position: absolute;
  top: -5px;
  right: 10px;
  height: 5px;
  width: 25px;
  background-color: orange;
}

Replicating the Same in Tailwind CSS

To achieve the same design using Tailwind CSS, you'll need to:

  1. Use relative positioning on the parent element (card).
  2. Add before: and after: utility prefixes for the pseudo-elements.
  3. Utilize the content-[''] utility to define empty content.

Here's the Tailwind equivalent:

<div
  class="before:border-brown-500 relative mx-auto mt-[50vh] min-h-[90px] w-72 cursor-pointer bg-gray-500
p-4 before:absolute before:left-[-5px] before:top-[-5px] before:h-[calc(100%-10px)] before:w-[calc(100%-25px)] before:border-l-[5px] before:border-t-[5px] before:content-['']
after:absolute after:right-[10px] after:top-[-5px] after:h-[5px] after:w-[25px] after:bg-orange-500 after:content-['']"
></div>

Explanation of Key Tailwind Utilities Used

  • relative: Ensures the child pseudo-elements are positioned relative to the card element.
  • before:content-[''] and after:content-['']: Creates the pseudo-elements and sets their content to an empty string.
  • before:absolute and after:absolute: Positions the pseudo-elements absolutely within the card container.
  • Custom widths and heights: Using utilities like before:w-[calc(100%-25px)] and before:h-[calc(100%-10px)] replicates the dynamic dimensions defined in plain CSS.
  • Borders and Backgrounds: Utilities like before:border-t-[5px] and after:bg-orange-500 define visual styles.

Why Use Tailwind CSS for Pseudo-Elements?

Tailwind CSS makes it easier to maintain consistency and quickly iterate on your designs. With utilities for pseudo-elements, you can avoid writing repetitive CSS while achieving the same flexibility.


Frequently Asked Questions (FAQ)

1. What is the purpose of ::before and ::after?

These pseudo-elements allow you to insert additional content or decorative elements before or after the actual content of an HTML element, without modifying the DOM structure.

2. Can I use content with Tailwind CSS?

Yes, Tailwind provides the content-[''] utility to define content for ::before and ::after. You can also use it to add text or symbols.

3. How do I enable the before: and after: utilities in Tailwind?

The before: and after: utilities are enabled by default in Tailwind CSS. If they are not working, ensure your Tailwind configuration file is set up correctly.

4. What are the advantages of using Tailwind over plain CSS?

Tailwind reduces the need to write custom CSS, promotes consistency, and speeds up development by offering pre-configured utility classes.