EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

CSS in React

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » CSS Tutorial » CSS in React

CSS in React

Introduction to CSS in React

In react, js CSS can be used as any constant with all the attributes or we can create a separate file where we can put all the CSS. With the help of CSS in the react js, we can design the look and feel of the components. we can design the components according to our requirements. In reacting js it gives us flexibility where we can write css for common which means a CSS that can be used by many components and we can also write unique css which means we can write CSS which can be used by only one component.

How to use CSS to React?

There are multiple ways to use the css in the react js we will discuss two majorly used ways one is using as the constant on the same component and another is using on separate file which can be used by many other components. Let us discuss both the ways and benefits of them.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Writing the CSS as the constant inside components

In this approach, we define the constant, and inside the constant, we put all the important attribute design, for example, const x ={color: red, width:20px} and this constant will be assigned on any HTML tag as the value of the style attribute.For example if we have Html tag div then on <div style =x></div> .Here we have assigned the value of the x on the HTML tag div with the value of the x. With the help of this approach, we can write any css on the constant x and the value of x can be used anywhere on the component HTML tag. Here we are getting reusability but issue with this approach that the css which we design here will not be used by other components which means the value of x is limited to the current components and we can not be able to use the x on the other components.

Writing the CSS on the separate file

In this approach, we will create a separate file where we will put the all css logic. We need to create a class with any name like classname=”test-class” and on the file, we can put any css like .test-class{css attribute names}, in this, we can put any css. Here the main benefit of this approach is we can use this file in any component and hence we can resume the css for any number of components. But there is an issue with the approach, which is supposed we want a different look for different components, then in that case, if we made any change to these components it will impact the other components also as they are also using the same css. Hence it always depends on the requirements of us how we want to use this. If we need any unique design then we should use the css as the constant on the same file and if we feel that the given css will be the same for all the components then we can put css on the separate file.

An example of the above two is if we have a button and we know that all the button look will be the same throughout the application then we can use a commonplace for the css which is any file. In the same way, if we have the text of different purposes then we need to write the css on the components itself as the changes are unique for the components.

Examples to Implement CSS in React

In the below, we have given four examples of the react css, in the examples, the first three are explaining the way of uses of the css in the form of the constant which will be available for the same file only and in the fourth example we have taken a separate file where we have written the css and that file will be available to many other files of react components. In case if we wanted to run these examples then we can create a file with any name with HTML as the extension like xss.html and on the file put the react javascript code section on the javascript and the HTML on the HTML section and we can see the output of the execution of the files.

Popular Course in this category
CSS Training (9 Courses, 9+ Projects)9 Online Courses | 9 Hands-on Projects | 61+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,429 ratings)
Course Price

View Course

Related Courses
Bootstrap Training (2 Courses, 6+ Projects)jQuery Training (8 Courses, 5 Projects)

Example #1

Please see the below example along with the screen of the output:

Code:

var Shape = React.createClass({
//CSS constant for the react component shape design
stylerExample: {
height: "56px",
width: "56px",
background: "red"
},
render: function() {
return (
<div style = {this.stylerExample}></div>
);
}
});
React.render(
<Shape />,
document.getElementById('main'));
Html
<div id="main"></div>

Output:

CSS in React1

Example #2

Please see the below example along with the screen of the output:

Code:

var Shape = React.createClass({
//CSS constant for the react component shape design
stylerExample: {
height: "59px",
width: "59px",
backgroundColor: "red",
borderRadius: "70%"
},
render: function() {
return (
<div style = {this.stylerExample}></div>
);
}
});
React.render(
<Shape />,
document.getElementById('main'));
Html,
<div id="main"></div>
Screen,

Output:

CSS in React2

Example #3

Please see the below example along with the screen of the output:

Code:

var Shape = React.createClass({
//CSS constant for the react component shape design
stylerExample: {
borderBottom: "56px solid red",
borderLeft: "26px solid transparent",
height: "3",
borderRight: "26px solid transparent",
width: "135px"
},
render: function() {
return (
<div style = {this.stylerExample}></div>
);
}
});
React.render(
<Shape />,
document.getElementById('main'));
Html,
<div id="main"></div>

Output:

shape design

Example #4

Please see the below example along with the screen of the output:

Code:

var Shape = React.createClass({
render: function() {
return (
<div className="shape-class"></div>
);
}
});
React.render(
<Shape />,
document.getElementById('main'));
Html ,
<div id="main"></div>
CSS,
//This is the css with written on the separate file showing that other file components can also use them .
.shape-class {
border-left: 90px solid transparent;
height: 0;
border-right: 90px solid transparent;
width: 0;
border-bottom: 90px solid red;
}

Output:

file components

Conclusion

From this tutorial we learned the basic concept of the working of the react css, we learned the main uses and the various way we can use the css in the react. We focus on some of the important examples of the css in the react js which revealed the flow of it.

Recommended Articles

This is a guide to CSS to React. Here we discuss an introduction to CSS in React, how to use it with programming examples. You can also go through our other related articles to learn more –

  1. React Native Libraries
  2. React Native Layout
  3. CSS Button Border
  4. CSS Attribute Selector

CSS Training (9 Courses, 9+ Projects)

9 Online Courses

9 Hands-on Projects

61+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
CSS Tutorial
  • CSS
    • Introduction To CSS
    • What is CSS?
    • Uses Of CSS
    • Advantages of CSS
    • Career In CSS
    • CSS Commands
    • Is Css Case Sensitive
    • CSS object-fit
    • Types of CSS Selectors
    • CSS Radio Button
    • CSS Attribute Selector
    • CSS first child of class
    • CSS Selector nth Child
    • CSS Parent Selector
    • CSS Child Selector
    • CSS Not Selector
    • CSS Descendant Selector
    • CSS Inline Style
    • Checkbox CSS
    • CSS Appearance
    • CSS Font Properties
    • CSS font-variant
    • CSS Pagination
    • CSS Table Styling
    • CSS Table Cell Padding
    • CSS Padding Color
    • CSS Text Formatting
    • CSS for Text-Shadow
    • CSS text-stroke
    • CSS text-indent
    • CSS Rotate Text
    • CSS Text Color
    • CSS Center Div
    • CSS Arrow
    • CSS Arrow Down
    • CSS offset
    • CSS Cursor
    • CSS Layout
    • CSS Grid Layout
    • Button in CSS
    • CSS Button Border
    • text-align in CSS
    • CSS Horizontal Align
    • CSS Position
    • CSS Box Sizing
    • CSS box-shadow
    • CSS Text Underline
    • CSS Text Outline
    • CSS Blinking Text
    • Text Decoration CSS
    • CSS Vertical Align
    • CSS Word Wrap
    • CSS Padding
    • CSS Font Color
    • CSS Color Generator
    • CSS Margin Right
    • CSS Margin Color
    • CSS Color Codes
    • CSS Color Transparent
    • CSS Color Chart
    • CSS Link Color
    • CSS z-index
    • CSS Curved Border
    • CSS Border Left
    • CSS left
    • CSS Gradient Generator
    • Radial Gradient in CSS
    • CSS Shape Generator
    • CSS Triangle Generator
    • CSS background-color
    • CSS Background Image
    • CSS background-clip
    • CSS background-blend-mode
    • CSS Drop Shadow
    • CSS line height
    • CSS line break
    • Sticky Footer CSS
    • CSS Header Design
    • CSS Border Style
    • CSS Border Generator
    • Sticky Sidebar CSS
    • CSS Transparent Border
    • CSS Border Radius
    • CSS translate
    • CSS transform
    • CSS 3D Transforms
    • CSS Text Transform
    • CSS Transition Effects
    • CSS Transition Property
    • CSS Animation Transition
    • Negative Margin CSS
    • CSS Navigation Bar
    • CSS Overflow
    • CSS overflow-wrap
    • CSS Lists
    • CSS list-style
    • CSS Order
    • CSS Box Model
    • CSS Inner Border
    • CSS Icon
    • Menu Icon CSS
    • CSS Multiple Borders
    • Opacity in CSS
    • CSS Float Right
    • CSS Clear Float
    • CSS clip
    • CSS disabled
    • CSS Border Padding
    • Border Images in CSS
    • CSS Visibility
    • CSS Validator
    • CSS Clearfix
    • CSS Counter
    • CSS Letter Spacing
    • CSS root
    • CSS zoom
    • CSS calc()
    • CSS.supports()
    • CSS Loader
    • Media Query CSS
    • CSS @keyframes
    • CSS @bottom
    • CSS page-break-after Property
    • CSS page-break
    • CSS Position Fixed
    • CSS skew()
    • CSS Row
    • CSS Masking
    • CSS Scrollbar
    • CSS Overlay
    • CSS Important
    • CSS Cursor Hand
    • CSS Inherit
    • CSS Position Relative
    • CSS Compressor
    • CSS tricks
    • CSS Outline Property
    • CSS Flexbox Properties
    • CSS flex-direction
    • CSS content property
    • CSS Typography
    • CSS Formatter
    • CSS nowrap
    • CSS Column
    • GridView CSS
    • CSS Viewport
    • CSS Minify
    • CSS Combinators
    • CSS in React
    • CSS Matrix 
    • CSS Pseudo Elements
    • CSS Pseudo Classes
    • CSS Pointer Events
    • CSS Resize
    • CSS Inheritance
    • CSS Interview Questions
    • Cheat Sheet CSS
  • CSS3
    • What is CSS3?
    • CSS3 Interview Questions
    • Cheat sheet CSS3
  • sass
    • How to Install SASS
    • SASS Interview Questions
    • What is Sass
    • SASS Comments
    • Sass Variables
    • SASS Import
    • SASS if else
    • SASS Nesting
    • SASS @each
    • SASS @at-root
    • SASS @extend
    • SASS @media
    • SASS @for
    • SASS Map
    • SASS Selectors
    • SASS Color Functions
    • SASS Mixins

Related Courses

CSS Training Course

Bootstrap Training Course

JQuery Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - CSS Training (9 Courses, 9+ Projects) Learn More