Belajar Tailwind CSS #12 Hover, fokus, dan status lainnya part 5
Ahmad Dika Zulfahmi
07 April 2025
Child Selectors – Styling direct children
Kadang kita pengen ngasih style ke anak langsung dari sebuah elemen (bukan semua keturunannya), tapi kita nggak bisa akses langsung elemen tersebut (misalnya karena itu dari komponen luar). Di sinilah varian *:
(dibaca: child selector) dipakai.
<!DOCTYPE html>
<html lang="en" data-theme="midnight">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tailwind Child Selectors</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="p-10 space-y-10 bg-gray-100 text-gray-800">
<div>
<h2>Categories</h2>
<ul
class="*:rounded-full *:border *:border-sky-100 *:bg-sky-50 *:px-2 *:py-0.5 dark:text-sky-300 dark:*:border-sky-500/15 dark:*:bg-sky-500/10">
<li>Sales</li>
<li>Marketing</li>
<li>SEO</li>
<li>Analytics</li>
<li>Design</li>
<li>Strategy</li>
<li>Security</li>
<li>Growth</li>
<li>Mobile</li>
<li>UX/UI</li>
</ul>
</div>
</body>
</html>
Semua li
(anak langsung dari ul
) akan dapat style yang disebutkan di atas.
berikut hasilnya:
Styling all descendants (menggunakan **:
)
Kalau kamu mau style semua keturunan (bukan cuma anak langsung), kamu bisa pakai **:
.
Contoh:
<!DOCTYPE html>
<html lang="en" data-theme="midnight">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tailwind Child Selectors</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="p-10 space-y-10 bg-gray-100 text-gray-800">
<ul class="**:data-[avatar]:size-12 **:data-[avatar]:rounded-full">
<li>
<img src="https://png.pngtree.com/png-vector/20220628/ourmid/pngtree-male-profile-avatar-user-portrait-png-image_5548548.png" data-avatar />
<p>Leslie Abbott</p>
</li>
<li>
<img src="https://png.pngtree.com/png-vector/20220824/ourmid/pngtree-human-businessman-manager-male-vector-png-image_33421134.png" data-avatar />
<p>Hector Adams</p>
</li>
<li>
<img src="https://png.pngtree.com/png-vector/20220816/ourmid/pngtree-gentleman-hipster-worker-avatar-vector-art-illustration-png-image_5873304.png" data-avatar />
<p>Blake Alexander</p>
</li>
</ul>
</body>
</html>
Semua elemen di dalam ul
yang punya data-avatar
akan dapat size dan border radius yang disebutkan
berikut hasilnya:
Custom Variants – Arbitrary variants
Kadang kamu pengen nambahin styling cuma pas elemen punya class tertentu. Kamu bisa bikin varian custom pakai format [&.class-name]:utility
.
Contoh:
<!DOCTYPE html>
<html lang="en" data-theme="midnight">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tailwind Child Selectors</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="p-10 space-y-10 bg-gray-100 text-gray-800">
<ul role="list">
<li class="[&.is-dragging]:cursor-grabbing">Item 1</li>
<li class="[&.is-dragging]:cursor-grabbing">Item 2</li>
</ul>
</body>
</html>
berikut hasilnya:
Selector dengan spasi (pakai underscore)
Kalau kamu mau apply styling ke elemen tertentu di dalam struktur (misalnya semua <p>
), kamu bisa pakai _
untuk spasi.
Contoh:
<!DOCTYPE html>
<html lang="en" data-theme="midnight">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tailwind Child Selectors</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 text-gray-800">
<div class="[&_p]:mt-4">
<p>Ini paragraf pertama.</p>
<ul>
<li>
<p>Ini paragraf dalam list.</p>
</li>
</ul>
</div>
</body>
</html>
Semua <p>
di dalam div
akan punya margin-top mt-4
.
berikut hasilnya:
Menggunakan @media atau @supports di arbitrary variants
Kamu bisa pakai at-rule seperti @supports
untuk styling berdasarkan kondisi CSS tertentu.
Contoh:
<!DOCTYPE html>
<html lang="en" data-theme="midnight">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tailwind Child Selectors</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 text-gray-800">
<div class="flex [@supports(display:grid)]:grid">
<div>Item 1</div>
<div>Item 2</div>
</div>
</body>
</html>
berikut hasilnya:
Custom variant dengan @custom-variant
Kalau kamu sering pakai arbitrary variant yang sama, kamu bisa bikin custom variant.
Contoh definisi:
<html data-theme="midnight">
<button class="theme-midnight:bg-black theme-midnight:text-white px-4 py-2 rounded">Click me</button>
</html>
berikut hasilnya:
Artikel Lainnya Dengan Kategori Terkait :
1. Belajar Tailwind CSS #01 Instalasi Tailwind CSS
2. Belajar Tailwind CSS #02 Editor Setup di Tailwind CSS
3. Belajar Tailwind CSS #03 Compatibility di Tailwind CSS
4. Belajar Tailwind CSS #04 Upgrade dari Tailwind CSS v3 ke v4
5. Belajar Tailwind CSS #05 Perubahan di Tailwind CSS v4 dengan v3
6. Belajar Tailwind CSS #06 Styling dengan Utility Classes part 1
7. Belajar Tailwind CSS #07 Styling dengan Utility Classes part 2
8. Belajar Tailwind CSS #08 Hover, fokus dan status lainnya part 1
9. Belajar Tailwind CSS #09 Hover, fokus, dan status lainnya part 2
10. Belajar Tailwind CSS #10 Hover, fokus, dan status lainnya part 3
11. Belajar Tailwind CSS #11 Hover, fokus, dan status lainnya part 4
12. Belajar Tailwind CSS #12 Hover, fokus, dan status lainnya part 5
13. Belajar Tailwind CSS #13 Responsive design part 1
14. Belajar Tailwind CSS #14 Responsive design part 2
15. Belajar tailwind CSS #15 Responsive design part 3
16. Belajar Tailwind CSS #16 Dark Mode di Tailwind
17. Belajar Tailwind CSS #17 Theme Variables part 1
18. Belajar Tailwind CSS #18 Theme Variables part 2
19. Belajar Tailwind CSS #19 Theme Variables part 3
20. Belajar Tailwind CSS #20 Tailwind CSS - Colors part 1
21. Belajar Tailwind CSS #21 Tailwind - Colors part 2
22. Belajar Tailwind CSS #22 Adding custom styles part 1
23. Belajar Tailwind CSS #23 Adding custom styles part 2
24. Belajar Tailwind CSS #24 Adding custom styles part 3
25. Belajar Tailwind CSS #25 Detecting classes in soure files
26. Belajar Tailwind CSS #26 Functions and directives
27. Belajar Tailwind CSS #27 Preflight Tailwind CSS
28. Belajar Tailwind CSS #28 Aspect-Ratio Tailwind CSS
29. Belajar Tailwind CSS #29 Tailwind CSS columns
30. Belajar Tailwind CSS #30 Tailwind CSS Break-After
31. Belajar Tailwind CSS #31 Tailwind CSS Break-Before
32. Belajar Tailwind CSS #32 Break-Inside Tailwind CSS
33. Belajar Tailwind CSS #33 Box Decoration Break Tailwind CSS
34. Belajar Tailwind CSS #34 Box Sizing Tailwind CSS
35. Belajar Tailwind CSS #35 Tailwind CSS Display
36. Belajar Tailwind CSS #36 Tailwind CSS Float
37. Belajar tailwind CSS #37 Tailwind CSS Clear
38. Belajar Tailwind CSS #38 Tailwind CSS Isolation
39. Belajar Tailwind CSS #39 Tailwind CSS Object-fit
40. Belajar Tailwind CSS #40 Tailwind CSS Object-position
41. Belajar Tailwind CSS #41 Overflow Tailwind CSS
42. Belajar Tailwind CSS #42 Tailwind CSS overscroll-behavior
43. Belajar Tailwind CSS #43 Tailwind CSS Position
44. Belajar Tailwind CSS #44 Tailwind CSS top,right,bottom,left
45. Belajar Tailwind CSS #45 Visibility di Tailwind CSS
46. Belajar Tailwind CSS #46 Z-index di Tailwind CSS
47. Belajar Tailwind CSS #47 Tailwind CSS flex-basis
48. Belajar Tailwind CSS #48 Flex-direction Tailwind CSS
49. Belajar Tailwind CSS #49 Flex-wrap Tailwind CSS
50. Belajar Tailwind CSS #50 Flex Tailwind CSS
51. Belajar Tailwind CSS #51 Flex-grow Tailwind CSS
52. Belajar Tailwind CSS #52 Flex-shrink Tailwind CSS
53. Belajar Tailwind CSS #53 Tailwind CSS order
54. Belajar Tailwind CSS #54 Grid-template-columns Tailwind CSS
55. Belajar Tailwind CSS #55 Grid-column Tailwind CSS
56. Belajar Tailwind CSS #56 Grid-template-rows Tailwind CSS
57. Belajar Tailwind CSS #57 Grid-row Tailwind CSS
58. Belajar Tailwind CSS #58 Grid-auto-flow Tailwind CSS
59. Belajar Tailwind CSS #59 Grid-auto-columns Tailwind CSS
60. Belajar Tailwind CSS #60 Grid-auto-rows Tailwind CSS
61. Belajar Tailwind CSS #61 Gap di Tailwind CSS
62. Belajar Tailwind CSS #62 Justify-content di Tailwind CSS
63. Belajar Tailwind CSS #63 Justify-items di Tailwind CSS
64. Belajar Tailwind CSS #64 Justify-self di Tailwind CSS
65. Belajar Tailwind CSS #65 Align-content di Tailwind CSS
66. Belajar Tailwind CSS #66 Align-items di Tailwind CSS
67. Belajar Tailwind CSS #67 Align-self di Tailwind CSS
68. Belajar Tailwind CSS #68 Place-content di Tailwind CSS