Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reactjs - how to set inline style of backgroundcolor?

I want to set the style properties of some elements but I don't have the syntax correct. Can anyone suggest where I am wrong?

import React from 'react'; import debug from 'debug'  const log = debug('app:component:Header');  var bgColors = { "Default": "#81b71a",                     "Blue": "#00B1E1",                     "Cyan": "#37BC9B",                     "Green": "#8CC152",                     "Red": "#E9573F",                     "Yellow": "#F6BB42", };  export default class SideBar extends React.Component {    constructor(props) {     super(props);    }     render() {      return (       <a style="{{backgroundColor: {bgColors.Default}}}" >default</a>     <a style="{{backgroundColor: {bgColors.Blue}}}" >blue</a>     <a style="{{backgroundColor: {bgColors.Cyan}}}" >cyan</a>     <a style="{{backgroundColor: {bgColors.Green}}}" >green</a>     <a style="{{backgroundColor: {bgColors.Red}}}"  >red</a>     <a style="{{backgroundColor: {bgColors.Yellow}}}" >yellow</a>            );   }  } 

UPDATE: for anyone looking at this please see comments this is not working code.

like image 437
Duke Dougal Avatar asked Jun 25 '15 07:06

Duke Dougal


People also ask

How do you change the style of inline React?

To set inline styles in React: Set the style prop on the element to an object. Set the specific CSS properties and values to style the element. For example, <div style={{backgroundColor: 'salmon', color: 'white'}}> .

How do I change the style color in React?

To set text color in React, we can set the style prop to an object with the color property. to set the style prop of the h1 element to an object that has the color property set to 'red' . Now we should see that the color of the text is red.

How do you add a background inline style in React?

To set a background image with inline styles in React: Set the style prop on the img element. Set the backgroundColor property in the style object. For example, backgroundImage: url(${MyBackgroundImage}) .


2 Answers

https://facebook.github.io/react/tips/inline-styles.html

You don't need the quotes.

<a style={{backgroundColor: bgColors.Yellow}}>yellow</a> 
like image 139
Guilherme Medeiros Avatar answered Sep 28 '22 06:09

Guilherme Medeiros


Your quotes are in the wrong spot. Here's a simple example:

<div style={{backgroundColor: "#FF0000"}}>red</div> 
like image 32
Craigo Avatar answered Sep 28 '22 08:09

Craigo