Day 3: Styling Components

On the third day of learning ReactJS, I learnt ways to style components in React using External and Inline CSS using ‘style’ attribute.

External CSS and inline styling in React are similar to their counterparts in HTML but have some key differences and considerations:

External CSS:

External CSS involves writing styles in a separate CSS file and linking it to the HTML/JSX file. In React, we import external CSS files into your components using the import statement. Once imported, we can apply classes or IDs defined in the CSS file to your JSX elements using the className attribute (using camelCase). This allows for easy maintenance and reuse of styles across multiple components. Styles defined in external CSS files can affect the entire application, which can lead to unintended style conflicts.

//Second.js
import './Second.css'
export const Second = () =>{
    return (
        <h1 className = "heading2">This is my second component</h1>
    )
}
export const Second2 = () =>{
    return(
    <>
        <h1>This is my another second component</h1>
        <p>Hello there!</p>
    </>
    )
}        
//Second.css
:root{
    --bg-primary: #010101;
    --text-primary: #fff;
    --size: 40 px;
}

.heading2{
    background-color: var(--bg-primary);
    color: var(--text-primary);
    font-size: var(--size);
}        

Inline Style Attribute:

Inline styling involves applying styles directly to individual HTML/JSX elements using the style attribute. In React, you define styles as JavaScript objects within the component's JSX. You apply these styles by passing the JavaScript object to the style attribute of the JSX element. This allows for dynamic styling based on component state or props. It also voids issues with global scope and style conflicts since styles are scoped to specific elements. However, it overrides other styles, including styles applied by external CSS files or CSS frameworks, which can lead to unexpected behavior.

const First = () =>{
    const h1Style={
        backgroundColor: "#589320",
        color:"yellowgreen"
    }
    return (
        <h1 style = {h1Style}>This is my first component</h1>
    )
}

export default First        

Comparison with HTML:

External CSS:

The usage of external CSS in React is quite similar to HTML. Both involve linking external CSS files to apply styles globally or to specific elements using classes or IDs.

//react component
import './Second.css'

//HTML equivalent
<link rel="stylesheet" type="text/css" href="Second.css">        

Inline Style Attribute:

While inline styling is also possible in HTML using the style attribute, the syntax and usage in React are slightly different due to JSX's nature. In React, styles are defined as JavaScript objects rather than strings, allowing for dynamic styling and better integration with component logic.

//react component
<h1 style = {h1Style}>This is my first component</h1>

//HTML Equivalent
<h1 class="h1-style">This is my first component</h1>        

要查看或添加评论,请登录

Aastha S.的更多文章

  • ExpressJS

    ExpressJS

    What is ExpressJS? Express.js is a Node.

  • Day 13: Forms in React (Formik and Yup)

    Day 13: Forms in React (Formik and Yup)

    On the thirteenth day, I learned about forms in React, the Fragment tag, and react-icons in the react application…

  • Day 12: localStorage

    Day 12: localStorage

    It can be infuriating to close a webpage while filling out a form accidentally. We lose all the data already filled and…

  • Day 11: Layout, Offset, and the useParams Hook

    Day 11: Layout, Offset, and the useParams Hook

    Layout and Offset In React, "offset" and "limit" are commonly used when dealing with paginated data. These terms are…

  • Day 10: Using API data and Pagination in React

    Day 10: Using API data and Pagination in React

    APIs in React APIs (Application Programming Interfaces), in React, are used to fetch data from external sources, such…

  • Day 9: Environment Variables and Toastify in React

    Day 9: Environment Variables and Toastify in React

    What are Environment Variables? Environment variables in React serve as dynamic configurations for our application…

  • Day 8:React Hooks (Part 2)

    Day 8:React Hooks (Part 2)

    After a thorough introduction to React Hooks and the useState hook on day 7, we learned about the useEffect hook in…

  • Day 7: React Hooks

    Day 7: React Hooks

    On the seventh day of learning React, we learned about React hooks, their use, and their applications. React Components…

  • Day 6: Dynamic React Components using Props

    Day 6: Dynamic React Components using Props

    React utilizes a component-based architecture that enables developers to create reusable and modular UI elements. A…

  • Day 5: React Layouts, Outlets, and Links

    Day 5: React Layouts, Outlets, and Links

    On the fifth day of learning react, we grabbed on concepts like layouts, outlets, and links in React. They are…

社区洞察

其他会员也浏览了