Can't use css Id or class to style react component
See original GitHub issuePrerequisites
- Using yarn
- Using node 10.x
- Using an up-to-date
master
branch - Using latest version of devtools. See wiki for howto update
- Tried solutions mentioned in #400
Expected Behavior
Apply css styling to react component
Current Behavior
No css styles is applied. However if I import css file import Sidebar from './Sidebar.css'
and apply add the class to the element like <div id = {Sidebar.sidebar}></div>
then it works.
Steps to Reproduce (for bugs)
- Create react component
import React, { Component } from 'react';
import './Sidebar.scss';
export class Sidebar extends Component {
render() {
return (
<div id = 'sidebar'>
<h1>Sidebar</h1>
</div>
)
}
}
export default Sidebar;
- Create css styling for the component
#sidebar, .sidebar {
width: 30%;
background-color: navy;
}
Context
Trying to learn how to build cross platform application using React and Electron.
Environment
- Node version : v10.15.2
- Version or Branch used : master
- Operating System and version : Windows 10
- Link to your project :
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:9 (2 by maintainers)
Top Results From Across the Web
Solving the React Error: Not Picking Up CSS Style | Pluralsight
In a CSS file, all styling takes place inside the id or the class attributes of an element. The id and class attributes...
Read more >React css using ref instead of id - Stack Overflow
No, you can't edit an element's style using ref in a stylesheet. As ref is not accessible in the DOM. so you can't...
Read more >How To Style React Components | DigitalOcean
In this tutorial, you'll learn three different ways to style React components: plain Cascading Style Sheets (CSS), inline styles with ...
Read more >How to Style Your React App – 5 Ways to Write CSS in 2021
You can write normal CSS styles, but can include nested styles and pseudo-classes (like hover). You can associate styles with any valid HTML ......
Read more >Styling in React: 5 ways to style React apps - LogRocket Blog
Learn about styling React components with inline styling, styled-components, CSS modules, Tailwind CSS, and Sass and CSS style sheets.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Not exactly, but maybe this helps: The webpack config determines how css is imported. This project uses css modules, meaning:
import styles from 'app.css'
and<div className={styles.classname}>
in your componentloader: 'css-loader', options: { modules: true }
in webpack.config.renderer.dev.babel.jsThat’s generally the cleaner way to do.
What you are looking for is global css:
import 'app.css'
and<div className={classname}>
in your componentloader: 'css-loader', options: { modules: false }
in webpack.config.renderer.dev.babel.js (or just no modules option, will be false by default)Many components rely on global css styling. You can only use one method or the other, so it’s an important distinction to make.
@tufailra97 Based on this answer, https://stackoverflow.com/a/36844502/1837080, your app is looking for css files in the
app
directory. This is why in app/index.js, the callimport './app.global.css'
works but not in a custom component.What I think will work for you is using this code in your component:
import styles from './Sidebar.scss';
and then when you want to use the style in the component,…<div id={styles.sidebar}>...</div>
. If you need to intermix css classes, look to this answer: https://stackoverflow.com/a/43552631/1837080.