Styling in React

Projected Time : 1 hr

Prerequisites

Motivation

Styling has been one of the building blocks of web development. Cascading Style Sheets is a style sheet language used for describing the presentation of a document written in a markup language like HTML. React only provides a virtual DOM, whereas the presentation part still lies with CSS.

Objectives

Specific Things To Learn

Materials

Lesson

Guided Practice

Let’s create a simple div, and style it using inline CSS and flexbox. Then we will achieve same using external CSS and Bootstrap separately.

To Differentiate we will add background color simple padding and dividing parent div into flex box.

Note - To compare the changes among the 3 methods of CSS, create a separate React app.

  1. Using inline CSS

    1. Let us give a background color and some margin on top and bottom to parent element and make its display as flex. > <div style={ { margin:"10px 0", backgroundColor:"orange", display:"flex"}} >
    2. Now add background-color to each child div with some padding and flex basis. > <div style={{flexBasis:"25%",padding:"10px",backgroundColor:"green"}}>Child Div 1</div>
    3. Your App.js will look like -
    <div className="App">
    {/* Start of Parent Div */}
      <div style={{margin:"10px 0",backgroundColor:"orange",display:"flex"}}>
      {/* Start of Child Divs */}
        <div style={{flexBasis:"25%",padding:"10px",backgroundColor:"green"}}>Child Div 1</div>
        <div style= {{flexBasis:"25%",padding:"10px",backgroundColor:"red"}} >Child Div 2</div>
        <div style={{flexBasis:"25%",padding:"10px",backgroundColor:"blue"}}>Child Div 3</div>
        </div>
        {/* End of Parent Div */}
    </div>
  2. Using external CSS

    1. Create an external CSS file and add a parent class with same styling as you did in inline CSS. 2. Create a class for each child element. 3. Your CSS file will look like - - ``` .parent { margin: 10px 0; display: flex; width: 100vw; background-color: darkorange; } .child1 { flex-basis: 25%; max-width: 25%; padding: 10px; background-color: green; } .child2 { flex-basis: 25%; max-width: 25%; padding: 10px; background-color: red; }

            .child3 {
              flex-basis: 25%;
              max-width: 25%;
              padding: 10px;
              background-color: blue;
            }
      .
      
      `
      .parent div {
      ;
      ;
      ;
      }
      
      {
      ;
      }
      
      {
      ;
      }
      
      {
      ;
      }
      
      `
      .
      `
      `
      `
      
      `
      ;
      ;
      '
      {
      return (
       <div className="App">
      }
          <div className="parent">
      }
      >
      >
      >
      }
      >
      
      >
      );
      }
      
      ;
      
      `
  3. Using Bootstrap
    1. Let’s add Bootstrap CDN in index.html file.
    2. Create a row and make child div as col-3.
    3. Since Bootstrap has different colors, we have to add backgroundColor as inline CSS to differentiate between child element div.
    4. App.js will look like -

Common Mistakes and Misconceptions

Supplemental Materials