Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React component styling being overridden by global styling

Okay, just to state the problem right away, I have a situation where stylesheet B which is being loaded (or so I thought) AFTER stylesheet A, and should therefore be overriding A's styling because of cascading, is in fact being loaded into the browser BEFORE A. The order is wrong.

I have a simple React component structure as follows:

App
  > Header
  > Home
  > Footer

In my 'index.js' I have the basic order of statements:

import './assets/theme/theme_specific/scss/style.scss';

render(
  <Router history={browserHistory}>
    <Route path="/" component={App}>
      <IndexRoute getComponent={lazyLoadComponent(Home)} />
      <Route path="/resume" getComponent={lazyLoadComponent(Resume)} />
  </Router>,
  document.getElementById("app")
);

Here's the structure in App.js:

class App extends React.Component {
  render() {
    return (
      <div>
        <Header />
        {this.props.children}
        <Footer />
      </div>
    );
  }
}

And at the top of Header.js I have the following:

import './Header.scss';

The Webpack loader that's processing .scss files is:

test: /\.scss$/,
loader: 'style!css?sourceMap!resolve-url!sass?sourceMap',

I've confirmed that there is no !important being used anywhere, and the styling itself is exactly the same.

What's happening is that "Header.scss" is being loaded FIRST, and 'style.scss' SECOND. In other words 'style.scss' is overriding the styles in 'Header.scss', versus the other way around as they appear in the code. The 'Computed' tab in Chrome inspector confirms this is the case.

Anybody have any idea what's going on here?

like image 813
NateQ Avatar asked Nov 18 '16 20:11

NateQ


People also ask

Can a component be styled with a global style sheet?

The important thing to remember is to place the GlobalStyle component as a sibling component to your main application component(s). And that's it! Global styling is now all set up with Styled Components.

How do you override a style in React?

To override a style, you have two options. Either you can pass a style object, or you can pass a function that will obtain props regarding the current internal state of the component, thus enabling you to modify styles dynamically depending on the component state, such as isError or isSelected .

What is the best way of styling in React?

Inline styles are the most direct away to style any React application. Styling elements inline doesn't require you to create a separate stylesheet. Style applied directly to the elements as compared to styles in a stylesheet also have higher precedence.

How do you override styles in material UI?

Overriding styles with class names If you want to override a component's styles using custom classes, you can use the className prop, available on each component.


1 Answers

The CSS is imported in the order you specify. If you'd like your custom styling to have the highest precedence, put import './Header.scss'; beneath all the other imports.

like image 53
David L. Walsh Avatar answered Oct 17 '22 18:10

David L. Walsh