DEV Community

Cover image for Tips when adding Tailwind to an existing project (Bootstrap, etc.)
Nwanguma Victor
Nwanguma Victor

Posted on

3

Tips when adding Tailwind to an existing project (Bootstrap, etc.)

Opting out from tailwind reset (preflight)

Begin, by opting out of Tailwind reset called "preflight" to avoid conflict with existing CSS resets (i.e. mordern-normalize) by setting preflight to false

tailwind.config.js


module.exports = {
  mode: 'jit',
  purge: [],
  darkMode: false,
  corePlugins: {
    preflight: false // set preflight to false
  }, 
  theme: {
    extend: {},
  },
  variants: {},
  plugins: [],
}
Enter fullscreen mode Exit fullscreen mode

Namespacing all tailwind classes with a prefix

Prefixing all tailwind classnames with a prefix (e.g tw-) helps prevent clashes with existing classnames

tailwind.config.js


module.exports = {
  mode: 'jit',
  purge: [],
  darkMode: false,
  corePlugins: {
    preflight: false
  },
  prefix: 'tw-', // set prefix to 'tw-'
  theme: {
    extend: {},
  },
  variants: {},
  plugins: [],
}
Enter fullscreen mode Exit fullscreen mode

Example

<div class="bg-blue-500 h-40 w-40">
  ...
<div>
Enter fullscreen mode Exit fullscreen mode

becomes

<div class="tw-bg-blue-500 tw-h-40 tw-w-40">
  ...
<div>
Enter fullscreen mode Exit fullscreen mode

Increasing the specificity of tailwind classes

By nature tailwind classnames are low in specificity, so the chances of underlying CSS overriding it is very high. We are going to give more weight to our utilities by flagging them as !important in our config file.
Note ! Using !important is bad practice, but its important to know when to use it

tailwind.config.js


module.exports = {
  mode: 'jit',
  purge: [],
  darkMode: false,
  corePlugins: {
    preflight: false
  },
  prefix: 'tw-',
  important: true, // set important to true
  theme: {
    extend: {},
  },
  variants: {},
  plugins: [],
}
Enter fullscreen mode Exit fullscreen mode

Refrences

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay