Author -  Sai gowtham

How to combine multiple inline style objects in React

In this tutorial, we will learn about how to combine multiple inline style objects into a single style object in React.

In React, we can add a inline styles to the element using JavaScript Object with camelCase properties instead of a CSS string.

Example:

App.js
import React from 'react';

const box = {
    color: "green",
    fontSize: '23px'
}

export default function App(){
    return (
      <div style={box}>         <h1>Hello react</h1>
      </div>
    )
}

Combining multiple objects

If you have a multiple inline style objects, you can combine them by using a spread operator.

App.js
import React from 'react';

const box = {
    color: "green",
    fontSize: '23px'
}

const shadow = {
    background: "orange",
    boxShadow: "1px 1px 1px 1px #cccd"
}

export default function App(){
    return (
      <div style={{...box, ...shadow}}>         <h1>Hello react</h1>
      </div>
    )
}

In the above code, we are combining the two objects ( box, shadow) into a single style object using the spread operator.

Similarly, you can also use the Object.assign() method.

import React from "react";

const box = {
  color: "green",
  fontSize: "23px"
};

const shadow = {
  background: "orange",
  boxShadow: "1px 1px 1px 1px #cccd"
};

export default function App() {
  return (
    <div style={Object.assign({}, box, shadow)}>      <h1>Hello react</h1>
    </div>
  );
}

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY