Tailwind container meet bootstrap ones #9140
Replies: 1 comment 1 reply
-
Assumptions
BreakpointsFirst we need to port the breakpoints since the container will change from ReplaceReplacing is the more straight-forward, since we can pretty much copy-paste from Bootstrap: module.exports = {
theme: {
screens: {
// Don't need xs since Tailwind uses min-width approach
// to its media queries.
sm: '576px',
md: '768px',
lg: '992px',
xl: '1200px',
xxl: '1400px',
},
},
}; ExtendWith extending, we have to interweave the Bootstrap breakpoints with Tailwind's since "breakpoints need to be sorted from smallest to largest in order to work as expected with a min-width breakpoint system": const defaultTheme = require('tailwindcss/defaultTheme');
module.exports = {
theme: {
screens: {
// Don't need `xs` since Tailwind uses min-width approach
// to its media queries.
'bs-sm': 576px,
sm: defaultTheme.screens.sm, // 640px
// Bootstrap's `md` is the same as Tailwind's `md` but
// added here as an alias so we can be consistent.
'bs-md': 768px,
md: defaultTheme.screens.md, // 768px
'bs-lg': 992px,
lg: defaultTheme.screens.lg, // 1024px
'bs-xl': 1200px,
xl: defaultTheme.screens.xl, // 1280px
'bs-xxl': 1400px,
'2xl': defaultTheme.screens['2xl'], // 1536px
},
},
}; Max widthAssign Bootstrap container max-widths to Tailwind's Replacemodule.exports = {
theme: {
screens: {
// …
},
extend: {
maxWidth: {
sm: `${540 / 16}rem`,
md: `${720 / 16}rem`,
lg: `${960 / 16}rem`,
xl: `${1140 / 16}rem`,
xxl: `${1320 / 16}rem`,
}
},
},
}; Extendmodule.exports = {
theme: {
screens: {
// …
},
extend: {
maxWidth: {
'bs-sm': `${540 / 16}rem`,
'bs-md': `${720 / 16}rem`,
'bs-lg': `${960 / 16}rem`,
'bs-xl': `${1140 / 16}rem`,
'bs-xxl': `${1320 / 16}rem`,
},
},
},
}; Base stylingAll the container classes in Bootstrap have this styling: .container, .container-lg, .container-md, .container-sm, .container-xl, .container-xxl
--bs-gutter-x: 1.5rem;
--bs-gutter-y: 0;
width: 100%;
padding-right: calc(var(--bs-gutter-x) * .5);
padding-left: calc(var(--bs-gutter-x) * .5);
margin-right: auto;
margin-left: auto;
} So we can convert to Tailwind utility classes as follows.
So every container will have the classes Putting it all together
Here's an example Tailwind Play using the replace technique, with the Bootstrap version to compare. |
Beta Was this translation helpful? Give feedback.
-
Hello,
I want to use Tailwind container with same breakpoints and max-width values as bootstrap
Any idea about this?
Thank in advance
Beta Was this translation helpful? Give feedback.
All reactions