I am trying to create a react website using
npx create-react-app myapp
cd my app
later i followed the steps as per mentioned on tailwind css that are as followed:
npm install -D tailwindcss postcss autoprefixer
and then
npx tailwindcss init -p
followed by this i added the following statement to tailwindconfig:
module.exports = {
content: [
"./pages/**/*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
later added the following to index .css
#tailwind base;
#tailwind components;
#tailwind utilities;
my app.js looked like following:
import './index.css'
function App() {
return (
<div className="App">
<h1 className='text-orange-500' >Navbar</h1>
</div>
);
}
export default App;
still the tailwind is not taking any affect please help
folder structure is as followed;
the browser displays as it is
I have found the issue the issue was in tailwind config and along with that i deleted the postcss file. The new tailwind config:
module.exports = {
content: ['./src/**/*.{js,jsx,ts,tsx}'],
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) { ... }
'2xl': '1536px',
// => #media (min-width: 1536px) { ... }
},
},
plugins: [],
};
this worked for me still i am confused over the fact why it happened but anyways its working now
You might be having issue with tailwind.config.js can you try the below tailwind.config.js, In Create React App, the components are stored in src directory and you are targeting specific to pages and components directory, so going with .src/pages//, .src/pages/, .src/components//, .src/components/, may work you, Give it a try.
module.exports = {
content: [
".src/pages/**/*.{js,ts,jsx,tsx}",
".src/components/**/*.{js,ts,jsx,tsx}",
".src/components/*.{js,ts,jsx,tsx}",
".src/pages/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
Related
In my NextJS app I'm using an <h1> tag for the text and wrap it in a <div> but when I try to apply a color to the text, it doesn't work. I even added in my global.css but it throws an error.
<div className="mt-2 flex justify-start items-center text-2xl font-bold text-gray-300">
<Image
src={Logo}
width={40}
height={40}
/>
<h1>some text here</h1>
</div>
The code above does not throw an error, but it also doesn't apply the style to the text.
styles/global.css
#tailwind base;
#tailwind components;
#tailwind utilities;
#layer base{
body{
#apply bg-[#06202A] text-gray-300;
}
}
Applying styles in the base layer (above) throws the following error.
.../styles/globals.css The text-gray-300 class does not exist. If text-gray-300 is a custom class, make sure it is defined within a #layer directive.
tailwind.config.js
#type {import('tailwindcss').Config} */
module.exports = {
mode: "jit",
content: [
"./pages/**/*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
],
theme: {
colors: {
'main-bg-color': '#02172F',
'text-gray-color': '#fff'
},
extend: {},
},
plugins: [],
}
postcss.config.js
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
Why is this not working?
You are replacing the default colors with your new additions. To add colors, but keep the defaults, you should place your new colors within the extend property.
module.exports = {
mode: "jit",
content: [
"./pages/**/*.{js,ts,jsx,tsx}",
"./components/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {
colors: {
'main-bg-color': '#02172F',
'text-gray-color': '#fff'
},
},
},
plugins: [],
}
Also, the mode setting is no longer needed in Tailwind 3.0+. If you're still using V2, I would recommend upgrading if possible. If you cannot upgrade for some reason, I would like to point out that the default color classes are different between Tailwind 2 and 3, and you will have to reference the correct version's docs for the proper color set.
V2: https://v2.tailwindcss.com/docs/customizing-colors
V3: https://tailwindcss.com/docs/customizing-colors
On my project I use: TailwindCSS + Emotion + Tailwind Macro.
I just want to export TailwindCSS CSS rules to the currently generated styles.css file instead of rendering CSS rules on the DOM (html > head > style[]).
That way I would reduce the size of the app.js bundle, and of course, it will increase the size of the generated file: styles.cssbut I'm fine with that.
Any idea on how to do that?
Thanks!
Here are the config files:
tailwind.config.js
const defaultTheme = require('tailwindcss/defaultTheme');
const { darken } = require('polished');
const colors = require('tailwindcss/colors');
const theme = require(`./offers/${process.env.OFFER}/theme`);
module.exports = {
/**
* Useful for arbitrary values:
* https://tailwindcss.com/docs/adding-custom-styles#using-arbitrary-values
*/
content: [ './resources/assets/js/components/**/*.js' ],
theme: {
colors: {
transparent: 'transparent',
current: 'currentColor',
black: colors.black,
white: colors.white,
gray: colors.gray,
emerald: colors.emerald,
indigo: colors.indigo,
yellow: colors.yellow,
/**
* Palette Colors from XD Mockup.
*/
light: theme.color.light,
neutral: theme.color.neutral,
highlight: theme.color.highlight,
textMedium: theme.color.textMedium,
textMediumHover: darken(0.10, theme.color.textMedium),
textDark: theme.color.textDark,
focusPrimary: theme.color.focusPrimary,
focusPrimaryHover: darken(0.10, theme.color.focusPrimary),
focusPrimaryLight: theme.color.focusPrimaryLight,
focusSecondary: theme.color.focusSecondary,
focusSecondaryHover: darken(0.10, theme.color.focusSecondary),
focusSecondaryMed: theme.color.focusSecondaryMed,
},
fontFamily: {
nunito: ['Nunito', 'sans-serif'],
opensans: ['Open Sans', 'sans-serif'],
},
fontSize: {
...defaultTheme.fontSize,
'h1d': '40px',
'h1m': '28px',
'h2d': '28px',
'h2m': '24px',
},
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) { ... }
'2xl': '1536px', // => #media (min-width: 1536px) { ... }
},
},
};
webpack.mix.js
const mix = require('laravel-mix');
const path = require('path');
const theme = require(`./offers/${process.env.OFFER}/theme.js`);
const jsConfig = require('./resources/scripts/js-config');
require("mix-tailwindcss");
/**
* Prevent generating files: *.LICENSE.txt
*/
mix.options({
terser: {
extractComments: false,
terserOptions: {
output: {
comments: false,
},
},
},
});
/**
* Reference:
* https://laravel-mix.com/docs/6.0/extending-mix
*/
mix.extend('addWebpackLoaders', (webpackConfig, loaderRules) => {
loaderRules.forEach((loaderRule) => {
webpackConfig.module.rules.push(loaderRule);
});
});
mix.addWebpackLoaders([
{
test: /\.less$/,
use: [
{
loader: 'less-loader',
},
{
loader: 'text-transform-loader',
options: {
prependText:
`#offer: '${process.env.OFFER}';\n` +
`#cdn-url: '${process.env.CDN_URL}';\n`,
},
},
],
},
]);
mix.webpackConfig(webpack => {
return {
output: {
path: path.resolve(__dirname, 'public/cdn'),
publicPath: process.env.CDN_URL + '/',
chunkFilename: 'js/[name].js?v=##BUILD_NUMBER##',
},
plugins: [
new webpack.DefinePlugin({
'process.env.APP_ENV': `'${process.env.APP_ENV}'`,
'process.env.THEME': JSON.stringify(theme),
'process.env.JS_CONFIG': JSON.stringify(jsConfig(process.env.OFFER)),
}),
]
};
});
const package = require('./package.json');
const deps = Object.keys(package.dependencies);
mix.extract(deps);
mix.js('resources/assets/js/app.js', 'js');
mix
.less('resources/assets/less/styles.less', 'css')
.tailwind("./tailwind.config.js");
back when tailwindcss was new this is how CSS was generated, now it has built into webpack so I dont even use this now days.
npx tailwindcss build src/styles/index.css -o src/styles/output.css
note this will generate all css and file size can go up to Mbs.
so I recommend using postcss purgecss plugin along with this.
I faced with next issue. As the result of the next export, I receive wrong paths in CSS files, for example:
background: url(/images/oval_21.webp)
but should be
background: url(../../../images/oval_21.webp)
The same thing happens with fonts. Also I need to note I am using 'next-optimized-images' for handling images in export and here my config setup:
module.exports = withPlugins([
[optimizedImages, {
optimizeImages: false,
}],
{
assetPrefix: isDeployMode ? '.' : '',
reactStrictMode: true,
experimental: { esmExternals: true },
env: {
CQ_USE_ANALYTICS: JSON.stringify(config.useAnalytics),
CQ_AMPLITUDE_API_KEY: JSON.stringify(config.amplitudeApiKey),
},
images: {
domains: ["localhost"],
},
}
]);
This CSS rule used in the Sass file:
#cq-landing {
line-height: 1.5;
background-color: #fff;
font-family: 'SF Pro Display';
font-display: swap;
background: url(/images/oval_21.webp) top 0 center no-repeat;
...
}
And everything works fine with next dev and next start, issue reproduces only after export in static files.
what is the best way to load global styles and images for Storybook in NextJS?
Below are the following Storybook files (config.js and scs-loader.scss and webpack.config.js)
webpack.config.js
module.exports = ({ config }) => {
config.module.rules.push({
test: /\.(ts|tsx)$/,
loader: require.resolve('babel-loader'),
options: {
presets: [require.resolve('babel-preset-react-app')],
},
});
config.resolve.extensions.push('.ts', '.tsx');
config.module.rules.push({
test: /\.(svg|ico|jpg|jpeg|png|gif|eot|otf|webp|ttf|woff|woff2|cur|ani)(\?.*)?$/,
loader: require.resolve('file-loader')
});
return config;
};
config.js
import '!file-loader!style-loader!css-loader!sass-loader!./scss-loader.scss';
import { configure } from '#storybook/react';
configure(require.context('../components', true, /\.stories\.(j|t)sx?$/), module);
scss.loader.scss
#import '../styles/global.scss';
Using the above config I can get a successful Storybook build however I do not see any global styles nor do I see any images referenced in global.scss
If i remove the file-loader in config.js as follows
import '!style-loader!css-loader!sass-loader!./scss-loader.scss';
import { configure } from '#storybook/react';
configure(require.context('../components', true, /\.stories\.(j|t)sx?$/), module);
I see this error.
Error: Can't resolve '/images/hero.jpg' in 'D:\dev\personal\nextjs-ts\.storybook'
If I then go and remove all the images from global.scss I can then load Storybook and I can see the global styles however I've lost all the images. I need to try and get images and global styles working in tandem. Thanks.
Here is an example of how the images are referenced in global.scss.
.hero {
width: 100%;
height: 800px;
background: url('/images/hero.jpg');
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
How is it possible to get postcss-nesting and a #vue/cli v3 project built with the PWA plugin working?
So far I've tried
npm install postcss-nesting
I then created a src/main.css which contains
body {
h1 {
color: green;
}
}
Inside the main.js file I import the css import './main.css';
Then inside the postcss.config.js I've added it to the plugins (with others that work) e.g.
module.exports = {
plugins: {
'postcss-import': {},
'postcss-nesting': {},
}
}
When I then run npm run serve the CSS does not transform into body h1 as you can see
What would be the correct way to get this working?
Thanks
Nesting should be enabled inside package.json since vue-cli does not read the configuration from postcss.config.js or .postcssrc.js as mentioned here.
"postcss": {
"plugins": {
"autoprefixer": {},
"postcss-preset-env": {
"browsers": "last 2 versions",
"features": {
"nesting-rules": true,
"custom-media-queries": true,
"color-mod-function": true
}
}
}
},
Working example on this repository: https://github.com/dobladov/vue-cli-example-postcss
Also for the nesting is important to use the symbol &
<style>
body {
background-color: tomato;
& .foo {
color: purple;
}
}
</style>