-1

Note: No need to understand my code, just focus on the 3 components files Palette.js, Palette.css and footer.js

Firstly I created my footer component in Palette.js and write CSS for its styling in Palette.css, which is linked to only Palette.js. in order to use the footer in different files, I created a new footer component in a separate file[footer.js] but forget to import styles from Palette.css which are used to style my footer, I removed my whole footer component from Palette.js and import my footer component from footer.js in Palette.js My footer component looks like [footer.js without any stylesheet]

import React, { Component } from 'react'
function Footer(props){
     const { creater, emoji, paletteName} = props;
     return (
          <footer>     
               <h2 className="palette-creater">Palette Created by : {creater}</h2>
               <div className="palette-identity">
                    <span className="palette-emoji">
                         {emoji}
                    </span>
                    <span className="palette-name">
                         {paletteName}
                    </span>
               </div>
               <h3 className="palette-copyright">All rights reserved @ Colors.io 2021</h3>             
          </footer>
     )
}
export default Footer;

My Palette.js looks like

import React, { Component } from 'react'
import ColorBox from './ColorBox';
import NavBar from './NavBar';    
import Footer from './footer' ;// imported my footer
import './Palette.css';
//styles for footer component in footer.js are still in this file

export default class Palette extends Component{
 render(){
    return (
      <Footer creater={creater} emoji={emoji} paletteName={paletteName} />
    )
  }
}

As you can see my footer.js has no styles and my Palette.js has a CSS file palette.css which has styles for my footer. Also, the styles for my footer in the Palette.css looks like

footer{
     background-color: #ced0d1;
     font-family: 'Red Hat Display', sans-serif;
     padding: 5px;
     text-align: center
}

.palette-creater{
     
     padding-top: 10px;
     padding-bottom: 10px;
     margin: 0;
     font-weight: 500;
     letter-spacing: 3px;
     
}

.palette-identity{
     display: flex;
     justify-content: space-around;

}

.palette-name{
     font-size: 1.1rem;
     font-weight: 400;
}
.palette-emoji{
     display: block;
     font-size: 1rem;
}

.palette-copyright{
     margin:0;
     padding-bottom: 5px;
}

How my styles are applying to the footer component from Palette.css, which is not even linked with the footer.js, and also no matter wherever I import my footer component from footer.js and used it, it's always applying styles from Palette.css. My application is pretty large, I cannot post all the code.

Example just think of that I make a new file app.css and linked it with only App.js and writes all the styles of the application in app.css, then all the components take styles from here.

2
  • 1
    CSS is not scoped. Any CSS that any of your components imports will end up being added globally in your HTML page and will affect any element with a matching class name. Commented Apr 19, 2021 at 6:39
  • Ohh Thanks....It just cleared my whole confusion..I totally agreed with your fact. Thank you soo much!! Commented Apr 19, 2021 at 6:42

1 Answer 1

1

I believe you'll be able to find your answer here: React CSS - how to apply CSS to specific pages only

The key element from the above link is that "Create React App bundles all your css files into one so all the styles will be available everywhere in you app (on every rendered component). Be aware that CRA is a Single-Page Application (SPA) so you can't think in "pages" but rather in "rendered component" in the DOM."

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.