question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't use css Id or class to style react component

See original GitHub issue

Prerequisites

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)

  1. 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;

  1. 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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
larissa-ncommented, Aug 26, 2019

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 component
  • loader: 'css-loader', options: { modules: true } in webpack.config.renderer.dev.babel.js

That’s generally the cleaner way to do.

What you are looking for is global css:

  • import 'app.css' and <div className={classname}> in your component
  • loader: '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.

2reactions
reZachcommented, Mar 28, 2019

@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 call import './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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found