How to import a CSS file in a React Component
You don't even have to name it if you don't need to:
e.g.
import React from 'react';
import './App.css';
see a complete example here (Build a JSX Live Compiler as a React Component).
How to import css file for into Component .jsx file
How are you compiling this? If it's webpack, you'd probably need to bring in the style-loader and include something like this in the module.loaders
array in your webpack config:
{
test: /\.css$/,
loader: "style!css"
}
Update: With the webpack.config.js now provided, we can confirm it needed a change in the module.loaders
array. OP was using a less loader and only checking for .less
files, so the exact loader object should read:
{
test: /\.(less|css)$/,
loader: 'style!css!less'
}
As suggested by @Q Liu. Leaving the original bit as if someone comes here with an error importing a .css file, it's likely what they need.
Import CSS in a react app
You can just import the css file like the below in your component
import './header.css';
Imagine your header.css file looks like
.header {
background-color: rgb(49, 118, 197);
height:100px;
border:10px solid red;
}
To use the style, you can use the class like ,
<div className="header">Hello World</div>
Hope this helps :)
Importing css file in jsx using relative path
import '../../custom_styles/sidebar.css';
in your mypage.js
file.
../
means that you're going one level up from your current file. In your case you have to move two ups.
Cant Import CSS files into React Project
Modifie your webpack.config.js to this and import your css file on your App component like this import './file.css';
(assuming that the css file is in the same directory as your App component)
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ["babel-loader"],
},
{
test: /\.css$/,
use: [ 'style-loader', 'css-loader' ]
}
]
},
resolve: {
extensions: ['*', '.js', '.jsx']
},
output: {
path: __dirname + '/dist',
publicPath: '/',
filename: 'bundle.js'
},
devServer: {
contentBase: './dist'
}
};
How to Import CSS Module React and JSX? ( Error )
I think it is a typo:
// remove style prop or it will be converted to style={true}
<div className="col-md-6 offset-md-3">
// use "style" prop instead of "styles"
<p style={styles.boilerplate}>some boilterplate text</p>
</div>
CSS classes are being read from files which are not imported in the component
The class which gets applied doesn't come from the .css file imported
at the top of the .jsx file. Is this the expected behaviour?
Yes, because the CSS rules that are defined in any of the imported files are not specifically scoped to their components.
Obviously I can give unique names to each css class but I was
wondering if there was a way to fix that.
Giving unique names manually is one way to fix that. But you can use things like CSS Modules etc.
Unable to import .css stylesheet in my JSX component
Got solution still answering here, it will help some people who needs to work react with grunt and babel. If you need to import your css file in your component like
import './Container.css';
you need this plugin : https://github.com/guybedford/require-css
by using this you can import it like :
import 'css!./Button_Styles.css';
this answer for people who are using grunt and require , not webpack and css-modules.
Related Topics
Css: How to Attach an Arrow to a Div and Make It Overlap the Border
Css: Setting Background-Image in CSS
Toggle Class with CSS Not Jquery
Target First Element of Div Using CSS
How to Remove Blue Border Around Links in IE9
Add a State Property to an Inline Style in React
How to Override Opacity for a Child
Max-Width of Img Inside Flexbox Doesn't Preserve Aspect Ratio
Css: What This Asterisk (*) Does
Css3 Flexbox Compatibility Problems with Firefox and Safari
Reliable Solution for Conditional Comments in Outlook.Com HTML Emails
What Is the Meaning of the "@Include" in .CSS Files
Can You Add New CSS Properties in Chrome Inspector
Javafx CSS Button with Image - How to Define the Size of the Image