{"id":372576,"date":"2022-08-24T06:11:47","date_gmt":"2022-08-24T13:11:47","guid":{"rendered":"https:\/\/css-tricks.com\/?p=372576"},"modified":"2022-08-24T06:11:48","modified_gmt":"2022-08-24T13:11:48","slug":"using-css-cascade-layers-to-manage-custom-styles-in-a-tailwind-project","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/using-css-cascade-layers-to-manage-custom-styles-in-a-tailwind-project\/","title":{"rendered":"Using CSS Cascade Layers to Manage Custom Styles in a Tailwind Project"},"content":{"rendered":"\n
If a utility class only does one thing, chances are you don\u2019t want it to be overridden by any styles coming from elsewhere. One approach is to use The Tailwind config file has an Cascade layers allow us to group styles into \u201clayers\u201d. The precedence of a layer always beats the specificity of a selector. Specificity only matters inside each layer. Establishing a sensible layer order helps avoid styling conflicts and specificity wars. That\u2019s what makes CSS Cascade Layers a great tool for managing custom styles alongside styles from third-party frameworks<\/a>, like Tailwind.<\/p>\n\n\n\n A Tailwind source Let\u2019s take a look at the official Tailwind docs<\/a> about directives:<\/p>\n\n\n\n Directives are custom Tailwind-specific at-rules you can use in your CSS that offer special functionality for Tailwind CSS projects. Use the In the output CSS file that gets built, Tailwind\u2019s CSS reset \u2014 known as Preflight<\/a> \u2014 is included first as part of the base styles. The rest of Confusingly, Tailwind has its own For example, to append your own styles to the The The CSS standard is much more powerful. Let\u2019s get back to that\u2026<\/p>\n\n\n Here\u2019s how we can rewrite this to use the CSS standard Unlike the Tailwind directive, these don\u2019t get compiled away. They\u2019re understood by the browser. In fact, DevTools in Edge, Chrome, Safari, and Firefox will even show you any layers you\u2019ve defined.<\/p>\n\n\n\n!important<\/code> to be 100% certain the style will be applied, regardless of specificity conflicts.<\/p>\n\n\n\n
!important<\/code> option that will automatically add
!important<\/code> to every utility class. There\u2019s nothing wrong with using
!important<\/code> this way, but nowadays there are better ways to handle specificity. Using CSS Cascade Layers<\/a> we can avoid the heavy-handed approach of using
!important<\/code>.<\/p>\n\n\n\n\n\n\n\n
.css<\/code> file usually starts something like this:<\/p>\n\n\n\n
@tailwind base;\n@tailwind components;\n@tailwind utilities;\n@tailwind variants;<\/code><\/pre>\n\n\n\n
@tailwind<\/code> directive to insert Tailwind\u2019s
base<\/code>,
components<\/code>,
utilities<\/code> and
variants<\/code> styles into your CSS.<\/p><\/blockquote>\n\n\n\n
base<\/code> consists of CSS variables needed for Tailwind to work.
components<\/code> is a place for you to add your own custom classes. Any utility classes you\u2019ve used in your markup will appear next. Variants are styles for things like hover and focus states and responsive styles, which will appear last in the generated CSS file.<\/p>\n\n\n
The Tailwind
@layer<\/code> directive<\/h3>\n\n\n
@layer<\/code> syntax. This article is about the CSS standard, but let\u2019s take a quick look at the Tailwind version (which gets compiled away and doesn\u2019t end up in the output CSS). The Tailwind
@layer<\/code> directive is a way to inject your own extra styles into a specified part of the output CSS file.<\/p>\n\n\n\n
base<\/code> styles, you would do the following:<\/p>\n\n\n\n
@layer base {\n h1 {\n font-size: 30px;\n }\n}<\/code><\/pre>\n\n\n\n
components<\/code> layer is empty by default \u2014 it\u2019s just a place to put your own classes. If you were doing things the Tailwind way, you\u2019d probably use
@apply<\/code><\/a> (although the creator of Tailwind recently advised against it<\/a>), but you can also write classes the regular way:<\/p>\n\n\n\n
@layer components {\n .btn-blue {\n background-color: blue;\n color: white;\n }\n}<\/code><\/pre>\n\n\n\n
Using the CSS standard
@layer<\/code><\/h3>\n\n\n
@layer<\/code>:<\/p>\n\n\n\n
@layer tailwind-base, my-custom-styles, tailwind-utilities;\n\n@layer tailwind-base {\n @tailwind base;\n}\n\n@layer tailwind-utilities {\n @tailwind utilities;\n @tailwind variants;\n} <\/code><\/pre>\n\n\n\n