Apply Styles to the Application for Character Counter

The next step is to add styles to each element to make it visually appealing using CSS. We can create n styling files one for each component. In this project, we will create two CSS files, one for the character counter component and another for the App component.

  • Modify the character counter component to add styling for each element. Using the className attribute, we can match the styling to the element. Make sure to import the style file into the component.
// CharacterCounter.jsx



import "./CharacterCounter.css";



function CharacterCounter() {

  return (

    <div className="container">

      <div className="input">

        <textarea

          id="input"

          placeholder="Enter a text"

          value={text}

        />

      </div>

      <div className="result">

        <div className="character-count">

          <span id="characterCount">0</span>

          <span>Characters</span>

        </div>

        <div className="character-count-without-spaces">

          <span id="characterCount">0</span>

          <span>Characters (Excluding Spaces)</span>

        </div>

        <div className="word-count">

          <span id="wordCount">0</span>

          <span>Words</span>

        </div>

      </div>

    </div>

  );

}



export default CharacterCounter;
  • The next step is to create a styling file for character counter. Create a file in the component folder ChacterCounter.css. And write styles for each element.
/* ChacterCounter.css */



/* Main body styling */

body{

    height100vh;

    display: flex;

    flex-direction: column;

    align-items: center;

    justify-content: center;

    background-colorrgb(224223223);

}



/* Main container styling */

.container{

    width: max-content;

}



/* Text area styling */

#input{

    resize: none;

    height200px;

    width700px;

    border3px solid black;

    padding2px;

    margin25px 0;

    font-size1rem;

}



/* Display area styling */

.result{

    display: flex;

    flex-direction: row;

    justify-content: space-between;

    font-size1.5rem;

}
  • Now, let’s add some styling to the App component.
// App.js



import "./App.css";

import CharacterCounter from "./components/CharacterCounter";



function App() {

  return (

    <div className="App">

      <div className="header">

        <h1 className="title">Character Counter App</h1>

      </div>

      <CharacterCounter />

      <a href="https://www.guvi.in/" className="copywright">

        Made by Guvi💚

      </a>

    </div>

  );

}



export default App;
  • Modify the App.css file to include new stylings.
/* App.css */



*{

  margin0;

  padding0;

}

.App{

  display: flex;

  flex-direction: column;

  align-items: center;

  justify-content: center;

}

.title{

  font-size2rem;

  margin20px;

}

.copywright{

  margin-top50px;

}
  • Once you add the styling, the application will look like the below output.