- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Styling in React.js
Styling in React.js can be done in below two ways
- css style sheets
- inline style
Let’s see CSS style sheets first
We have App.js file as shown below −
import React, {Component} from 'react'; import './App.css'; class App extends Component { render(){ return ( <div className="App"> <p className="myColoredText">Styling React Components</p> </div> ); } } export default App;
In App.js file we have imported an App.css file which contains css class myColoredText
Please note
- We used name of css class in double quotes with attribute className
- JSX uses camelCase notations for writing attributes on html element.
We have App.css as below −
.myColoredText{ color:blue; background:yellow; margin:auto; width:20%; padding:20px; }
Issue with writing css classes with above approach is that the css classes becomes global once webpack build final css file.
Then css classes can get overridden by other css files having same css class.
Solution for avoiding the CSS classes from getting global
Use of css module prevents this from happening. The css file names should end with module.css extension.
This solution creates unique css classes in workflow which prevents them from overriding
The css classes will become fileName_cssClassName__hashValue
With this logic, the above used App.css file should be renamed to App.module.css
We can use both the global and module.css files in our application.
For common css we can create a global css file without using module.css extension.
Let’s see the inline styling of react components
Inline styles are created using JavaScript objects (key value property).
Example
import React, {Component} from 'react'; import './App.css'; class App extends Component { render(){ let myInlineStyle={ boxShadow:'0 2px 3px #ccc', border:'2px solid blue', marginTop:'50px' } return ( <div className="App"> <p className="myColoredText" style={myInlineStyle}>Styling React Components</p> </div> ); } } export default App;
There is a problem with inline styling as well. For hovering the elements using mouse requires third party libraries like radium.
We can install radium as below −
on the application directory, run the following command npm install --save radium
Add radium in jsx file.
add import for radium import Radium from 'radium';
Once installed, add hovering object in inline styling object as below −
let myInlineStyle={ boxShadow:'0 2px 3px #ccc', border:'2px solid blue', marginTop:'50px', ':hover':{ background:'white' } }
With radium, App.js file looks like below −
import React, {Component} from 'react'; import './App.css'; import Radium from 'radium'; class App extends Component { render(){ let myInlineStyle={ boxShadow:'0 2px 3px #ccc', border:'2px solid blue', marginTop:'50px', ':hover':{ background:'white' } } return ( <div className="App"> <p className="myColoredText" style={myInlineStyle}>Styling React Components</p> </div> ); } } export default Radium(App);
The important thing is we have to wrap or export component in Radium like below −
export default Radium(App);
Now, on hovering it shows the background as white. This way we can handle the inline styling. It’s basically a JavaScript object.
- Related Articles
- SVG morphing in React JS
- Adding Lottie animation in React JS
- SVG drawing in React JS frontend
- Creating a Particle Animation in React JS
- Creating a Customizable Modal in React JS
- Creating animated loading skeletons in React JS
- Drawing arrows between DOM elements in React JS using react-archer
- Creating an Airbnb Rheostat Slider in React JS
- Creating a Rich Text Editor in React JS
- Device Detection and Responsive Design in React JS
- Using pointer light for better lighting in React JS with React-Three-Fiber
- SVG zoom-in and zoom-out in React JS
- How to make a resizable element in React JS
- Making a timer in React JS using reactspring animation
- Creating an Excel-like data grid in React JS
- Drag and Drop a File feature in React JS
