點燈坊

うまく使いさえすれば、時間はいつも十分にある

Using after to Decorate the Element

Sam Xiao's Avatar 2022-01-25

We can use after modifier to add a pseudo element on the fly and apply utility.

Version

TailwindCSS 3.0

after

after000

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 on after modifier, which sets the content of the pseudo element
  • after:text-red-500 : apply text-red-500 utility on after modifier, which sets the text color of the pseudo element
  • after:ml-1 : apply ml-1 utility on after modifier, which sets the left margin of the pseudo element

inline-block

after001

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 on after modifier, which sets the content of the pseudo element
  • after:text-red-500 : apply text-red-500 utility on after modifier, which sets the text color of the pseudo element
  • after:block : apply block utility on after 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

Reference

TailwindCSS, Before and after