Adding Base Styles

Best practices for adding your own global base styles on top of Tailwind.

Base (or global) styles are the styles at the beginning of a stylesheet that set useful defaults for basic HTML elements like <a> tags, headings, etc. or apply opinionated resets like the popular box-sizing reset.

Tailwind includes a useful set of base styles out of the box that we call Preflight, which is really just modern-normalize plus a thin layer of additional more opinionated styles.

Preflight is a great starting point for most projects, but if you’d ever like to add your own additional base styles, here are some recommendations for doing it idiomatically.


Using classes in your HTML

If you just want to apply some global styling to the html or body elements, consider just adding existing classes to those elements in your HTML instead of writing new CSS:

  1. <!doctype html>
  2. <html lang="en" class="text-gray-900 leading-tight">
  3. <!-- ... -->
  4. <body class="min-h-screen bg-gray-100">
  5. <!-- ... -->
  6. </body>
  7. </html>

Using CSS

If you want to apply some base styles to specific elements, the easiest way is to simply add them in your CSS.

  1. @tailwind base;
  2. @tailwind components;
  3. @tailwind utilities;
  4. @layer base {
  5. h1 {
  6. @apply text-2xl;
  7. }
  8. h2 {
  9. @apply text-xl;
  10. }
  11. }

By using the @layer directive, Tailwind will automatically move those styles to the same place as @tailwind base to avoid unintended specificity issues.

Using the @layer directive will also instruct Tailwind to consider those styles for purging when purging the base layer. Read our documentation on optimizing for production for more details.

It’s a good idea to use @apply or theme() to define these styles to avoid accidentally deviating from your design system.

@font-face rules

You can use the same approach to add your @font-face rules for any custom fonts you’re using:

  1. @tailwind base;
  2. @tailwind components;
  3. @tailwind utilities;
  4. @layer base {
  5. @font-face {
  6. font-family: Proxima Nova;
  7. font-weight: 400;
  8. src: url(/fonts/proxima-nova/400-regular.woff) format("woff");
  9. }
  10. @font-face {
  11. font-family: Proxima Nova;
  12. font-weight: 500;
  13. src: url(/fonts/proxima-nova/500-medium.woff) format("woff");
  14. }
  15. }

Using a plugin

You can also add base styles by writing a plugin and using the addBase function:

  1. // tailwind.config.js
  2. const plugin = require('tailwindcss/plugin')
  3. module.exports = {
  4. plugins: [
  5. plugin(function({ addBase, theme }) {
  6. addBase({
  7. 'h1': { fontSize: theme('fontSize.2xl') },
  8. 'h2': { fontSize: theme('fontSize.xl') },
  9. 'h3': { fontSize: theme('fontSize.lg') },
  10. })
  11. })
  12. ]
  13. }

Any styles added using addBase will be inserted into the base layer alongside Tailwind’s other base styles.

←Dark Mode Extracting Components→

Edit this page on GitHub