Skip to content

Instantly share code, notes, and snippets.

@Lelectrolux
Last active February 6, 2024 10:07
Show Gist options
  • Save Lelectrolux/8f9a78491a5c9617078a73c091e01415 to your computer and use it in GitHub Desktop.
Save Lelectrolux/8f9a78491a5c9617078a73c091e01415 to your computer and use it in GitHub Desktop.
Tailwindcss breakpoint inspector, initial idea from https://gist.github.com/jonsugar/6bce22bd7d3673294caad36046c2b7cb

Tailwindcss plugin that adds a small box in the lower right corner of the screen showing the current breakpoint

Uses a position: fixed after css pseudo element on the body, so no extra markeup, and it get the breakpoint list by itself.

export default function({addBase, theme}) {
if (process.env.NODE_ENV === "production") return
const screens = theme('screens', {})
const breakpoints = Object.keys(screens)
addBase({
'body::after': {
content: `"Current breakpoint default (< ${screens[breakpoints[0]]})"`,
position: 'fixed',
right: '.5rem', // could replace with theme('spacing.2', '.5rem'), same for most of the other values
bottom: '.5rem',
padding: '.5rem .5rem .5rem 2rem',
background: 'no-repeat .5rem center / 1.25rem url(https://tailwindcss.com/favicon-32x32.png), #edf2f7',
border: '1px solid #cbd5e0',
color: '#d53f8c',
fontSize: '.875rem',
fontWeight: '600',
zIndex: '99999',
},
...breakpoints.reduce((acc, current) => {
acc[`@media (min-width: ${screens[current]})`] = {
'body::after': {
content: `"Current breakpoint ${current}"`
}
}
return acc
}, {})
})
}
/* this is the generated css */
body::after {
content: "Current breakpoint default (< 640px)";
position: fixed;
right: 0.5rem;
bottom: 0.5rem;
padding: 0.5rem 0.5rem 0.5rem 2rem;
background: no-repeat 0.5rem center / 1.25rem url(https://tailwindcss.com/favicon-32x32.png), #edf2f7;
border: 1px solid #e2e8f0;
color: #d53f8c;
font-size: 0.875rem;
font-weight: 600;
z-index: 99999;
}
@media (min-width: 640px) {
body::after {
content: "Current breakpoint sm";
}
}
@media (min-width: 768px) {
body::after {
content: "Current breakpoint md";
}
}
@media (min-width: 1024px) {
body::after {
content: "Current breakpoint lg";
}
}
@media (min-width: 1280px) {
body::after {
content: "Current breakpoint xl";
}
}
module.exports = {
// ...
plugins: [
require('./breakpointInspector')
]
}
@jonsugar
Copy link

jonsugar commented Oct 30, 2019

Nice work @Lelectrolux 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment