I try to include some font faces into a tailwind-preset I want to use across multiple projects.
In the documentation it is only described how to define font-faces within the base layer of a css file, see here: https://tailwindcss.com/docs/adding-base-styles#font-face-rules.
However to include it in a distributable preset the way to go (from my knowledge), is to include it in a plugin. However I do not get what is the problem.
My approach is the following:
const defaultTheme = require('tailwindcss/defaultTheme')
const plugin = require('tailwindcss/plugin')
module.exports = {
prefix: '',
mode: 'jit',
purge: {
content: [
'./src/**/*.{html,ts,css,scss,sass,less,styl}',
]
},
darkMode: false, // or 'media' or 'class'
theme: {
extend: {
fontFamily: {
sans: ['myfont', ...defaultTheme.fontFamily.sans]
},
}
},
variants: {
extend: {},
},
plugins: [ plugin(function ({ addBase}) {
const fonts = {
'#font-face': [
{
fontFamily: 'My Font',
fontStyle: 'normal',
fontWeight: 400,
src:
'url("./fonts/my-font.woff") format("woff");'
},
]
};
addBase(fonts);
})],
};
Your plugin looks correct, but the fontFamily defined in the #font-face rule should be the same as the fontFamily defined in the font-family CSS rule. Try changing myfont to My Font in your theme extension config.
See this Tailwind Play REPL for an example.
Related
I have a Next.js-Tailwind project to which I want to add custom classes so that I can use them anywhere in the project.
I've tried a couple of things like this but doesn't seem to work.
tailwind.config.js:
module.exports = {
purge: [
'./pages/**/*.{js,ts,jsx,tsx}',
'./components/**/*.{js,ts,jsx,tsx}',
],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {
height: {
'60': '60rem',
'40': '40rem',
'55': '55rem',
'180': '180px',
},
colors: {
'primary': '#0B003A',
},
},
},
variants: {
extend: {},
},
plugins: [require('#tailwindcss/typography')],
}
What all do I need to do to use these classes globally across the project?
I am using TailwindCSS for the first time, and that in a Next.js project. I followed their docs on "how to use tailwind with Nextjs" and tried adding colors in the tailwind.config.js, but it ended up breaking all colors. Other styles work.
I watched a YouTube video on Tailwind, but the guy was using it on regular HTML/CSS project. He outputted the file in a public/styles.css by running tailwindcss build styles/globals.css -o public/styles.css but I am using styles/globals.css in Next.js by following the docs.
My tailwind.config.js file:
module.exports = {
purge: ["./pages/**/*.{js,ts,jsx,tsx}", "./components/**/*.{js,ts,jsx,tsx}"],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
colors: {
//ADDED THIS
white: {
0: "#fff",
},
},
},
variants: {
extend: {},
},
plugins: [],
};
Using theme.colors to add new colors will fully overwrite the default Tailwind color palette.
Either you define all the colors that you want to use in theme.colors explicitly.
const colors = require('tailwindcss/colors')
module.exports = {
//...
theme: {
colors: {
black: colors.black,
// Define all desired colors
white: "#fff"
}
},
//...
};
Or, if you still want to have access to all the default colors and simply need to extend them, use theme.extend.colors instead.
module.exports = {
//...
theme: {
extend: {
colors: {
white: "#fff"
}
}
},
//...
};
I'm trying to import TypeKit fonts into my Tailwind config. I have included the .css file like so in my index: https://use.typekit.net/kcz3fka.css. Inside my tailwind.config.cjs file I have the definition:
module.exports = {
purge: ['index.html', 'src/**/*.tsx'],
mode: 'jit',
theme: {
extend: {
container: {
center: true,
},
fontFamily: {
display: ['"katarine-web"', ...theme.fontFamily.sans],
body: ['"katarine-web"', ...theme.fontFamily.sans],
},
},
},
plugins: [
require('#tailwindcss/typography'),
require('#tailwindcss/aspect-ratio'),
],
};
I've tried escaping the katarine name as well as with and without double quotes. Neither seem to work as expected. What should I be doing to ensure Katarine can be used?
try this,
module.exports = {
purge: ['index.html', 'src/**/*.tsx'], // include your purge target files
mode: 'jit',
theme: {
extend: {
container: {
'center': true,
},
fontFamily: { // if you want extend 'katrarine-web', then just add them instead other originals.
'display': ['katarine-web'],
'body': ['katarine-web'],
},
},
},
plugins: [
require('#tailwindcss/typography'),
require('#tailwindcss/aspect-ratio'),
],
};
also, check this Doc.
Happy coding :)
In order to have the user be able to select their color scheme in my website, I added a few themes in my style.css file. For example, one is "theme-light"
.theme-light {
--color-bg-primary: theme('colors.white');
--color-bg-typing: theme('colors.white');
--color-bg-input: theme('colors.white');
--color-bg-redo: theme('colors.blue.500');
--color-bg-redo-hover: theme('colors.red.500');
--color-bg-nav: theme('colors.red.500');
--color-bg-nav-active: theme('colors.red.500');
--color-text-primary: theme('colors.gray.900');
--color-text-secondary: theme('colors.gray.600');
--color-text-current: theme('colors.gray.900');
--color-text-correct: theme('colors.green.400');
--color-text-wrong: theme('colors.red.400');
--color-text-stats: theme('colors.gray.600');
--color-placeholder-primary: theme('colors.gray.600');
}
And I edited my tailwind.config.js file like so:
module.exports = {
future: {
removeDeprecatedGapUtilities: true,
purgeLayersByDefault: true,
},
purge: [],
theme: {
backgroundColor: {
primary: 'var(--color-bg-primary)',
typing: 'var(--color-bg-typing)',
input: 'var(--color-bg-input)',
redo: 'var(--color-bg-redo)',
'redo-hover': 'var(--color-bg-redo-hover)',
nav: 'var(--color-bg-nav)',
'nav-active': 'var(--color-bg-nav-active)',
},
textColor: {
primary: 'var(--color-text-primary)',
secondary: 'var(--color-text-secondary)',
current: 'var(--color-text-current)',
correct: 'var(--color-text-correct)',
wrong: 'var(--color-text-wrong)',
stats: 'var(--color-text-stats)',
},
placeholderColor: {
primary: 'var(--color-placeholder-primary)',
},
},
variants: {},
plugins: [require('#tailwindcss/typography')],
};
My question is: now I am unable to set anything like I was before in the html (like "text-blue-400" or "text-opacity-50"). How can I get the default configurations working again?
oh lol. all i had to use extend: https://tailwindcss.com/docs/theme
TailwindCSS 1.2.0
What I'm doing wrong? if I add fontSize as below text-7xl doesn't show up as the new optional value and text-6xl disappear.
module.exports = {
important: true,
theme: {
fontFamily: {
'theme-f1': ['"Oswald"', "sans-serif"],
'theme-f2': ['"Lora"', "serif"],
'theme-f3': ['"Bebas Kai"', "sans-serif"],
'theme-f4': ['"Open Sans"', "sans-serif"],
},
fontSize: {
'7xl': '7rem',
},
extend: {
colors: {
'theme-c1': '#006c32',
'theme-c1-b': '#6c8213',
'theme-c2': '#000000',
'theme-c3': '#ffffff',
}
},
},
variants: {
letterSpacing: ['responsive', 'hover', 'focus'],
},
plugins: [],
}
Currently you are overriding the default font sizes, you have to extend them if you want to add new font sizes without overriding the default ones:
module.exports = {
important: true,
theme: {
fontFamily: {
'theme-f1': ['"Oswald"', "sans-serif"],
'theme-f2': ['"Lora"', "serif"],
'theme-f3': ['"Bebas Kai"', "sans-serif"],
'theme-f4': ['"Open Sans"', "sans-serif"],
},
extend: {
fontSize: {
'7xl': '7rem',
},
colors: {
'theme-c1': '#006c32',
'theme-c1-b': '#6c8213',
'theme-c2': '#000000',
'theme-c3': '#ffffff',
}
},
},
variants: {
letterSpacing: ['responsive', 'hover', 'focus'],
},
plugins: [],
}
Afterwards compile your assets and you should have the default font sizes and your custom font size available.
You can read more about extending the default theme in the docs:
If you'd like to preserve the default values for a theme option but
also add new values, add your extensions under the theme.extend key.
For example, if you wanted to add an extra breakpoint but preserve the
existing ones, you could extend the screens property:
// tailwind.config.js
module.exports = {
theme: {
extend: {
// Adds a new breakpoint in addition to the default breakpoints
screens: {
'2xl': '1440px',
}
}
}
}