In React, one can create an object with styling information like background image, height, width, etc and refer to the object in the style attribute in the HTML element. Since the inline CSS is written in a JavaScript object, properties with two names, like background-color, must be written with camel case syntax.
Syntax :
class MyHeader extends React.Component {
render() {
const mystyle = {
backgroundColor: "white",
// CSS CODE
};
return (
<div>
<h1 style={mystyle}>Hello Style!</h1>
// All styling define in mystyle
// object will applied to h1 element.
</div>
);
}
}
Example 1: Set width and height of background image in div element to 100% and 200px .
Javascript
import React from 'react'; import './App.css'; function App() { const myStyle={ backgroundImage:"url(" + width:'100%', height:'200px', }; return ( <div style={myStyle}> <h1 style={{color:'green'}}> Geeks For Geeks </h1> <p style={{color:'white'}}> Set height and width of background image inline style react. </p> </div> ); } export default App; |
Output: Here, all style define in myStyle object is applicable to the div element. One can check the width and height of the background image in the div element is 100% and 200px.
Example 2: Set width and height of background image in div element to 20% and 200px .
Javascript
import React from 'react'; import './App.css'; function App() { const myStyle={ backgroundImage:"url(" + width:'20%', height:'200px', }; return ( <div style={myStyle}> <h1 style={{color:'green'}}> Geeks For Geeks </h1> <p style={{color:'white'}}> Set height and width of background image inline style react. </p> </div> ); } export default App; |
Output: Here, all style define in myStyle object is applicable to the div element. One can check the width and height of the background image in the div element is 20% and 200px.
Note: Similarly, one can define many other CSS styles in an object and call the object in the style attribute of the respective HTML element. All the CSS styles defined in the object will be applied to that particular HTML element as shown in the above example.
Recommended Posts:
- HTML width/height Attribute vs CSS width/height Property
- How to set the width and height of an image using HTML ?
- What is the difference between React Native and React?
- What is the difference between display: inline and display: inline-block in CSS?
- What is the difference between inline-flex and inline-block in CSS?
- How to get the width and height of an image ?
- How to get the height and width of an Image using ReactJS?
- How to get the image size (height & width) using JavaScript ?
- CSS | border-inline-width Property
- CSS | border-inline-start-width Property
- CSS | border-inline-end-width Property
- How to dynamically set the height and width of a div element using jQuery ?
- 8 Ways to Style React Components
- CSS | Height and Width
- How to change the width and height of Twitter Bootstrap's tooltips?
- How to animate div width and height on mouse hover using jQuery ?
- How to calculate Width and Height of the Window using JavaScript?
- How to specify the width and height of the canvas using HTML ?
- How to adjust the width and height of iframe to fit with content in it ?
- How to set the div height to auto-adjust to background size?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


