Tailwind container meet bootstrap ones #9140
Replies: 4 comments 8 replies
-
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.
-
@wongjn - Great solution. I am trying to replicate this with TailwindCSS 4.0 beta 8. I'm getting stuck on translating the configuration above to 4.0 in combination with the use of a prefix and disabling preflight. Here is my app.css... @layer theme, base, components, utilities;
@import 'tailwindcss/theme' layer(theme) prefix(tw);
@import 'tailwindcss/utilities' layer(utilities) prefix(tw);
@theme {
--font-sans: 'Avenir', 'Myriad Pro', 'Helvetica Neue', sans-serif;
--color-plum: oklch(45.31% 0.0979 300.21);
--color-lilac: oklch(81.77% 0.0435 303.58);
--color-error: var(--color-red-500);
--breakpoint-bs-sm: calc(540 / 16) rem;
--breakpoint-bs-md: calc(720 / 16) rem;
--breakpoint-bs-lg: calc(960 / 16) rem;
--breakpoint-bs-xl: calc(1140 / 16) rem;
--breakpoint-bs-xxl: calc(1320 / 16) rem;
--width-screen-bs-sm: var(--breakpoint-bs-sm);
--width-screen-bs-md: var(--breakpoint-bs-md);
--width-screen-bs-lg: var(--breakpoint-bs-lg);
--width-screen-bs-xl: var(--breakpoint-bs-xl);
--width-screen-bs-xxl: var(--breakpoint-bs-xxl);
}
@layer utilities {
.shy-container {
@apply tw:w-full tw:px-3 tw:mx-auto tw:bs-sm:max-w-bs-sm tw:bs-md:max-w-bs-md tw:bs-lg:max-w-bs-lg tw:bs-xl:max-w-bs-xl tw:bs-xxl:max-w-bs-xxl;
}
} Any thoughts on where I'm going wrong? |
Beta Was this translation helpful? Give feedback.
-
Thank you @wongjn for taking the time to clarify this! This is my first time using TailwindCSS which is why I wanted to start with 4.0 beta. Here's my semi-final config (will take out the @apply once I create a component). The upside is that it shows what the utility classes look like with the custom breakpoints and containers. @layer theme, base, components, utilities;
@import 'tailwindcss/theme' layer(theme) prefix(tw);
@import 'tailwindcss/utilities' layer(utilities) prefix(tw);
@theme {
--font-sans: 'Avenir', 'Myriad Pro', 'Helvetica Neue', sans-serif;
--color-plum: oklch(45.31% 0.0979 300.21);
--color-lilac: oklch(81.77% 0.0435 303.58);
--color-error: var(--color-red-500);
--breakpoint-bs-sm: 29.75rem; /* 476px */
--breakpoint-bs-md: 48rem; /* 768px */
--breakpoint-bs-lg: 62rem; /* 992px */
--breakpoint-bs-xl: 75rem; /* 1200px */
--breakpoint-bs-xxl: 87.5rem; /* 1400px */
--container-bs-sm: 33.75rem; /* 540rem */
--container-bs-md: 45rem; /* 720px */
--container-bs-lg: 60rem; /* 960px */
--container-bs-xl: 71.25rem; /* 1140px */
--container-bs-xxl: 82.5rem; /* 1320px */
}
@layer utilities {
.shy-container {
@apply tw:mx-auto tw:w-full tw:px-3 tw:bs-sm:max-w-bs-sm tw:bs-md:max-w-bs-md tw:bs-lg:max-w-bs-lg tw:bs-xl:max-w-bs-xl tw:bs-xxl:max-w-bs-xxl;
}
} |
Beta Was this translation helpful? Give feedback.
-
And here's my Svelte 5 component so I can remove the @apply from the config... <script lang="ts">
import type { Snippet } from 'svelte'
interface Props {
children?: Snippet
}
let { children }: Props = $props()
</script>
<div class="tw:mx-auto tw:w-full tw:px-3 tw:bs-sm:max-w-bs-sm tw:bs-md:max-w-bs-md tw:bs-lg:max-w-bs-lg tw:bs-xl:max-w-bs-xl tw:bs-xxl:max-w-bs-xxl">
{@render children?.()}
</div> |
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