34

My project is currently Vuejs which use BootstrapVue components (seems to use bootstrap 4 css).

I am trying to use Tailwind css for new custom components.

Is it possible to use both of them at same time?

Thank you.

1

7 Answers 7

54

You can solve classes conflict using a prefix

// tailwind.config.js
module.exports = {
  prefix: 'tw-',
}

BUT, most likely you will have a problem with normalize.css, which used in @tailwind base

Sign up to request clarification or add additional context in comments.

3 Comments

hi, can i know why normalize.css will cause a problem and why should we disable it by preflight: false?
Just because, each framework may use différents reset styles. We can even say that normalize.css is a micro framework by itself and there are others reset framework and also fully custom reset.
Starting from TailwindCSS v4, the way prefixes are added has changed. For more details, see here: Using prefix in TailwindCSS v4
21

Option 1: Adopt or recreate classes

If you only need one or two classes, for example from the color system of Tailwind, you could also copy them. Some characters would have to be masked, e.g:

// style.css

.hover\:text-blue-900:hover,
.text-blue-900 {
  color: #183f6d;
}

That's what I did at the beginning of a project, where bootstrap is the main framework. If it should be several colors and functions, you can also build this with SCSS quickly. In the long run, however, in my opinion, not the best and cleanest solution.

Example for this:

// style.scss

(...)
@each $name, $hexcode in $tailwind-colors {
    .hover\:text-#{$name}:hover,
        .text-#{$name} {
            color: $hexcode
        }
    }
}

Full code (Github Gist)

Option 2: Integrate Tailwind

But as soon as more functionalities should be added, or you want to build it cleaner, you can do here with the prefix mentioned by the documentation as Ostap Brehin says.

// tailwind.config.js

module.exports = {
  prefix: 'tw-',
}

The normalized definitions can be removed by disabling preflight:

// tailwind.config.js

module.exports = {
  corePlugins: {
    preflight: false,
  }
}

Better check the generated CSS file.

Here is my full tailwind.config.js file:

// tailwind.config.js

module.exports = {
    content: [
        './**/*.php',
        '../Resources/**/*.{html,js}',
    ],
    safelist: [
        'tw-bg-blue-800/75',
        {
            pattern: /(bg|text)-(blue)-(800)/,
            variants: ['hover'],
        },
    ],
    prefix: 'tw-',
    theme: {
        extend: {},
    },
    corePlugins: {
        preflight: false,
    },
    plugins: [],
}

2 Comments

Or just use unocss.dev
good lord. why did you curse my eyes with a project that uses regex to save time on compiling a stylesheet. That's a no from me dog.
13

Yes, you can use Tailwind and Bootstrap together.

However, you need to do some configuration to your tailwind. The first thing is to add a prefix and then turn off the preflight. And if you are not using Tailwind JIT then also make the important as true. If you are using Tailwind JIT then you can use "!" before the class to make it important. For example "!tw-block".

Here is a suitable configuration for Tailwind JIT/CDN:

  <script>
    tailwind.config = {
      prefix: "tw-",
      corePlugins: {
         preflight: false,
      }
    }
  </script>

If you are not using CDN, use this configuration:

module.exports = {
    content: ["./**/*.html"],
    prefix: "tw-",
    important: true,
    corePlugins: {
        preflight: false,
    }
}

I have written a whole blog on it: https://developerwings.com/tailwind-and-bootstrap-together/

1 Comment

I tried this solution for the configuration and got both bootstrap and tailwind working together flawlessly. One thing I'll add is that with "preflight: false" is that some of the tailwind UI components were not rendering properly. In order to solve this I added a separate preflight css file and added a .preflight class to certain tailwind components. This article explains how (dev.to/ajscommunications/scoping-normalized-preflight-css-c29) Also, I used (github.vue.tailwind-prefix.cbass.dev) to quickly add the "tw-" prefix to my tailwind code.
6

Possible, Yes. Is it recommended, NO.

There are many classes that are gonna contradict with each other e.g.

.container (Bootstrap)
.container (Tailwind)

.clearfix (B)
.clearfix (T)

And the list goes on...


My advice would be to either stick with BootstrapVue or Tailwind. My personal preference, Tailwind.

5 Comments

Actually, I dont like to rewrite all bootstrapVue components again, though I love tailwind :(
is there any bootstrap like opinionated library built on top of tailwind with all the components like alert, grid, btns, cards, collapse, etc. sometimes you don't want to leave the comfort of bootstrap but want tailwind utilities at the same time
@SumitWadhwa DaisyUI seems to do that, but I haven't used it.
@SumitWadhwa Tailwind UI is the official offering for this, though it's a paid product. tailwindui.com
I don't agree. When migrating (If want to switch, but there are too many components to do it all at once in a single large PR.) a project, there is often a need to use both frameworks simultaneously. The TailwindCSS prefix system offers a perfect solution for this. Although you don't recommend it, it's an essential solution for project migrations.
2

As long as there's no name collision in 2 libs (which I don't think they are), they will work just fine.

But I don't think you should. Because it will break the unification of the project and will make it harder to maintain.

2 Comments

There actually are quite a few name collisions as shown in other answers...
I don't think this would really be a good answer.
1

As other answers have pointed out, the TailwindCSS prefix system is perfectly suited to differentiate its own classes from those of other frameworks. However, the other answers were focused on TailwindCSS v3, and the declaration and use of prefixes has changed starting from v4.

Starting from TailwindCSS v4, class names are no longer extended; instead, the prefix appears as a main utility. It must be applied before everything else. The goal is to make it easier to distinguish TailwindCSS class names from custom class names when reading the code.

From TailwindCSS v4 onwards

If you use a prefix, the following is valid. The prefix should be connected with a colon (:) rather than a hyphen (-). Additionally, the prefix should always be used as the first utility. By default, this would be tw:content, but due to other utilities, you need to gradually place the appropriate utilities in between.

@import "tailwindcss" prefix(myprefix);
myprefix:text-red-500
myprefix:hover:text-red-500

Preflight

TailwindCSS comes with a CSS reset and default settings. If you don't want to use this during shared usage, you should modify the TailwindCSS installation like this:

/* @import "tailwindcss"; */
/* This includes everything by itself, but if we need to use it without preflight, unfortunately, we will have to individually declare the necessary imports. */

@layer theme, base, components, utilities;
@import "tailwindcss/theme.css" layer(theme);
/* @import "tailwindcss/preflight.css" layer(base); */
@import "tailwindcss/utilities.css" layer(utilities);

Import TailwindCSS with important by default

In certain CSS specificity cases, Bootstrap may remain stronger than TailwindCSS.

To address this, it might be worth importing TailwindCSS with !important during the migration from Bootstrap to ensure its classes always remain stronger than those of Bootstrap.

@import "tailwindcss" important;

Or with prefix:

@import "tailwindcss" prefix(myprefix) important;

Comments

0

is there any bootstrap like opinionated library built on top of tailwind with all the components like alert, grid, btns, cards, collapse, etc. sometimes you don't want to leave the comfort of bootstrap but want tailwind utilities at the same time

That is pretty much what we were looking for. Finally found https://tw-elements.com/

Bootstrap components recreated with Tailwind CSS, but with better design and more functionalities

It has an AGPL licence so you can use it for free as long as your project is open source.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.