Skip to content

Commit 2d10ade

Browse files
committed
Add Apply Tailwind Classes To Existing CSS Class as a Tailwind TIL
1 parent d1f83ed commit 2d10ade

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1393 TILs and counting..._
13+
_1394 TILs and counting..._
1414

1515
---
1616

@@ -1235,6 +1235,7 @@ _1393 TILs and counting..._
12351235

12361236
### Tailwind CSS
12371237

1238+
- [Apply Tailwind Classes To Existing CSS Class](tailwind/apply-tailwind-classes-to-existing-css-class.md)
12381239
- [Base Styles For Text Link](tailwind/base-styles-for-text-link.md)
12391240
- [Specify Paths For Purging Unused CSS](tailwind/specify-paths-for-purging-unused-css.md)
12401241
- [Use Tailwind Typography Prose In Dark Mode](tailwind/use-tailwind-typography-prose-in-dark-mode.md)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Apply Tailwind Classes To Existing CSS Class
2+
3+
Let's say I have some HTML in my app that I don't totally control -- maybe it's
4+
a 3rd-party component or it was rendered by a markdown transformer. Regardless,
5+
I am ending up with some HTML where tags have class names that I'd like to
6+
style.
7+
8+
```html
9+
<div class="code-block">
10+
<!-- ... -->
11+
</div>
12+
```
13+
14+
If it was HTML (or JSX) that I was writing, I could stick whatever tailwind
15+
class names I wanted on the various tags to get the styling just right. But
16+
since I don't control it directly, I have to find another way to _apply_ those
17+
tailwind classes.
18+
19+
Enter [tailwind's `@apply`
20+
directive](https://tailwindcss.com/docs/functions-and-directives#apply). With
21+
this, I can target an existing class selector with any tailwind utility classes.
22+
Add lines like the following to your root `tailwind.css` file.
23+
24+
```css
25+
@tailwind base;
26+
@tailwind components;
27+
@tailwind utilities;
28+
29+
/* additional styling here 👇 */
30+
.code-block {
31+
@apply bg-gray-200 rounded-md p-4;
32+
}
33+
```

0 commit comments

Comments
 (0)