Here is my Tailwindcss version 3.0.24.
I want to create a prefix css class like this:
<div class="myTestVariant:text-red-600">hello world</div>
and try to complie css by Tailwindcss like:
#media (max-width: 400px) {
myTestVariant\:text-red-600:active {
color: rgb(220, 38, 38, var(1));
// color may setted by tailwind.config.js
}
}
#media (min-width: 401px) {
myTestVariant\:text-red-600:hover {
color: rgb(220, 38, 38, var(1));
// color may setted by tailwind.config.js
}
}
And I tried to use addVariant to customize my variant. Here is my tailwind.config.js
const plugin = require('tailwindcss/plugin')
module.exports = {
content: [
'./index.html',
'./src/**/*.{vue,js,ts,jsx,tsx}',
],
theme: {
extend: {},
},
plugins: [
plugin(function({ addVariant, e, postcss }) {
addVariant('myTestVariant', ({container, separator }) => {
const supportsRule1 = postcss.atRule({ name: 'media', params: 'screen and (min-width: 401px)' })
const supportsRule2 = postcss.atRule({ name: 'media', params: 'screen and (max-width: 400px)' })
supportsRule1.append(container.nodes)
supportsRule2.append(container.nodes)
container.append(supportsRule1)
container.append(supportsRule2)
supportsRule1.walkRules(rule => {
rule.selector = `.${e(`myTestVariant${separator}`)}${rule.selector.slice(1)}:active`
})
supportsRule2.walkRules(rule => {
rule.selector = `.${e(`myTestVariant${separator}`)}${rule.selector.slice(1)}:hover`
})
})
}),
],
}
Actually, it doesn't work.
But when I only add one media queries, it's work.
const plugin = require('tailwindcss/plugin')
module.exports = {
content: [
'./index.html',
'./src/**/*.{vue,js,ts,jsx,tsx}',
],
theme: {
extend: {},
},
plugins: [
plugin(function({ addVariant, e, postcss }) {
addVariant('myTestVariant', ({container, separator }) => {
const supportsRule1 = postcss.atRule({ name: 'media', params: 'screen and (min-width: 401px)' })
supportsRule1.append(container.nodes)
container.append(supportsRule1)
supportsRule1.walkRules(rule => {
rule.selector = `.${e(`myTestVariant${separator}`)}${rule.selector.slice(1)}:active`
})
})
}),
],
}
I think the problem is: I am not familiar with postCss API.
Can someone do me a favor? please.
You can add the custom screen breakpoint in the tailwind.config.js as follows:
module.exports = {
theme: {
screens: {
//like you can add extra-extra-small screen as
'xxs': '400px',
// => #media (min-width: 400px) { ... }
'xs': '401px',
// => #media (min-width: 401px) { ... }
'sm': '640px',
// => #media (min-width: 640px) { ... }
'md': '768px',
// => #media (min-width: 768px) { ... }
'lg': '1024px',
// => #media (min-width: 1024px) { ... }
'xl': '1280px',
// => #media (min-width: 1280px) { ... }
'2xl': '1536px',
// => #media (min-width: 1536px) { ... }
}
}
}
You can find more here https://tailwindcss.com/docs/screens#adding-smaller-breakpoints
Related
when entering the vale "xs", i get an error saying "(max-width: 575.98px) isn't a valid CSS value."
and when entering any other value i get the following error "This Breackpoint 'sm' isn't supported yet"
is it even possible to apply this idea with SASS ?
Here is my code:
$breakpoints: (
"xs": (max-width: 575.98px),
"sm": ((min-width: 576px) and (max-width: 767.98px)),
"md": ((min-width: 768px) and (max-width: 991.98px)),
"lg": ((min-width: 992px) and (max-width: 1199.98px)),
"xl": ((min-width: 1200px) and (max-width: 1399.98px)),
"xxl": (min-width: 1400px),
);
#mixin breakpoint($user-value) {
#each $size, $value in $breakpoints{
#if $user-value == $size {
#media #{$value} {
#content;
}
}#else {
#error "This Breackpoint '#{$user-value}' isn't supported yet";
}
}
};
body {
#include breakpoint(sm) {
background-color: blue;
}
}
i wanted to minimize the number of code i'm writing with this SASS mixin
so i figured something out, check out this edited code :
$breakpoints: (
"xs": "max-width: 575.98px",
"sm": "(min-width: 576px) and (max-width: 767.98px)",
"md": "(min-width: 768px) and (max-width: 991.98px)",
"lg": "(min-width: 992px) and (max-width: 1199.98px)",
"xl": "(min-width: 1200px) and (max-width: 1399.98px)",
"xxl": "min-width: 1400px",
);
#mixin breakpoint($user-value) {
$value: map-get($breakpoints, $user-value);
#if $value {
#media (#{$value}) {
#content;
}
}#else {
#warn "This Breackpoint '#{$user-value}' isn't supported yet";
}
};
body {
#include breakpoint(xxl) {
background-color: blue;
}
}
I'm trying to dynamically create a variable, but that doesn't seem to be possible in SCSS:
$res-mat-xs: 0;
$res-mat-sm: 600px;
$res-mat-md: 960px;
$res-mat-lg: 1280px;
$res-mat-xl: 1920px;
#mixin media-min($var) {
#media only screen and (min-width: $var) { #content; }
}
#mixin media-max($var) {
#media only screen and (max-width: $var - 1px) { #content; }
}
#mixin media-from-to($var1, $var2) {
#media only screen and (min-width: $var1) and (max-width: $var2 - 1px) { #content; }
}
$media: 'min', 'max', 'from-to';
$variants: 'very-small', 'small', 'default', 'large', 'very-large';
$breakpoints: 'xs', 'sm', 'md', 'lg', 'xl';
#each $breakpoint in $breakpoints {
.typo-style-#{$breakpoint}-#{$variants}-#{$breakpoint} {
#include media-min($res-mat-#{$breakpoint}) {
#include typo-style('default', 'important');
}
}
}
In addition, I am totally overwhelmed with the from-to ($media) and variants.
The class names for the from-to should look like this:
.typo-style-very-small-from-sm-to-md
.typo-style-large-from-sm-to-lg
How can I make these dynamic variables?
tailwind CSS container width in 2xl is not desired for me .
how can I change it ?
I want to remove its default width in 2xl .
How can I do it?
Add only necessary breakpoints to tailwind.config.js. Default values you can see in tailwind theme config
module.exports= {
theme: {
screens: {
'sm': '640px', // => #media (min-width: 640px) { ... }
'md': '768px', // => #media (min-width: 768px) { ... }
'lg': '1024px', // => #media (min-width: 1024px) { ... }
'xl': '1280px', // => #media (min-width: 1280px) { ... }
}
}
}
Or if you need some custom solution not directly connected with your "screens" configuration (as suggested by JHeth) you can configure container behavior separately like this:
module.exports= {
theme: {
container: {
screens: {
'sm': '100%',
'md': '100%',
'lg': '1024px',
'xl': '1280px',
'2xl': '1600px',
}
}
}
}
I have a row component that can take different grid layouts. One of the row has 3 columns. When switching to a tablet breakpoint, I need to switch to a two rows layout: 2 columns + 1 column that take the full width.
const RowTemplate = {
TwoLeft: { desktop: "2fr 1fr", tablet: "repeat(2, 1fr)", mobile: "1fr" },
Three: { desktop: "repeat(3, 1fr)", tablet: "repeat(2, 1fr)", mobile: "1fr" },
};
const Row = styled.div<{
format: { desktop: string; tablet: string; mobile: string };
}>`
display: grid;
#media (min-width: 400px) {
grid-template-columns: ${({ format }) => format.mobile};
> &:last-child {
grid-template-columns: 1fr;
}
}
#media (min-width: 800px) {
grid-template-columns: ${({ format }) => format.tablet};
}
#media (min-width: 1200px) {
grid-template-columns: ${({ format }) => format.desktop};
}
`;
export default function Dashboard() {
return (
<Row format={RowTemplate.Three}>
<Column1/>
<Column2/>
<Column3/>
<Tile
</Row>
)
}
Currently, the last column goes to a second line, but only takes 50% of its width. How tp fix this?
I have one question for u guyz (and gals ;) )
does anybody know any existing plugin for Tailwind CSS which make font sizes responsive (except for lower and upper screen size where font size is fixed) aka. like that:
body { font-size: calc(16px + (26 - 16) * ((100vw - 768px) / (1280 - 768))); }
#media screen and (max-width: 768px) { body { font-size: 16px; }}
#media screen and (min-width: 1280px) { body { font-size: 26px; }}
lower screen width - fixed font size
screen sizes in between - responsive font size from 16px to 26px
wider screen width - fixed font size
1- Define font sizes for each breakpoint on tailwind.config.js
module.exports = {
theme: {
extend: {
fontSize: {
'body-lg': '1rem',
'body': '.875rem',
}
}
}
}
2- Create classes on global.css by importing definitions from config file.
#layer base {
body {
#apply text-body;
}
#screen lg { // applying font size for lg breakpoint
body {
#apply text-body-lg;
}
}
}
Just put some rule like the following e.g.:
<project/path/to>/tailwind.css
html {
font-size: 16px;
#screen md {
font-size: 17px;
}
#screen lg {
font-size: 26px;
}
}
No need for a plugin.
Here's my setup for React with styled-components and twin.macro, it uses hardcoded breakpoints as I couldn't find a way to interpolate breakpoints within tailwind.config.js:
// tailwind.config.js
/** #type {import('tailwindcss').Config} */
const plugin = require("tailwindcss/plugin");
module.exports = {
plugins: [
plugin(function ({addUtilities}) {
addUtilities({
".fs-0": {
fontSize: "2.281rem",
lineHeight: 1.1,
"#media (min-width: 768px)": {
fontSize: "3.583rem",
},
},
".fs-1": {
fontSize: "1.802rem",
"#media (min-width: 768px)": {
fontSize: "2.488rem",
},
},
".fs-2": {
fontSize: "1.424rem",
"#media (min-width: 768px)": {
fontSize: "1.728rem",
},
},
".fs-3": {
fontSize: "1.266rem",
"#media (min-width: 768px)": {
fontSize: "1.44rem",
},
},
".fs-4": {
fontSize: "1.125rem",
"#media (min-width: 768px)": {
fontSize: "1.2rem",
},
},
".fs-5": {
fontSize: "1rem",
},
});
}),
],
};
// components/GlobalStyles.tsx
import React from "react";
import {createGlobalStyle} from "styled-components";
import tw, {GlobalStyles as BaseStyles} from "twin.macro";
const CustomStyles = createGlobalStyle`
h1 { ${tw`fs-1`} }
h2 { ${tw`fs-2`} }
h3 { ${tw`fs-3`} }
h4 { ${tw`fs-4`} }
h5, h6 { ${tw`fs-5`} }
`;
function GlobalStyles() {
return (
<>
<BaseStyles />
<CustomStyles />
</>
);
}
export default GlobalStyles;
// pages/_app.tsx
import React from "react";
import GlobalStyles from "components/GlobalStyles";
const MyApp = (): JSX.Element => {
return (
<>
<GlobalStyles />
</>
);
};
export default MyApp;
Sources
Adding utilities to Tailwind docs
createGlobalStyles with styled-components
Typographic scales (default and md) generated with Type Scale