简体   繁体   中英

React js css inline style margin right not working

I want to implement margin-right inline CSS to the below react span element:

import React, { Component } from "react";   
class NavBar extends Component {
  state = {
    totalCounters: 0,
  };      
  render() {    
    let styles = {
        margin-right: '20px',
        width: '250px',
        height: '250px',
        backgroundColor: 'yellow',
      };
    return (
      <nav className="navbar navbar-light bg-light">
        <a className="navbar-brand" href="#">
          Navbar
        </a>
        <span style={styles} className="badge badge-pill badge-secondary">
          {this.props.totalCounters}
        </span>
      </nav>
    );
  }
}
export default NavBar;

But it shows syntax error, while no error if replacing margin-right with margin . So how to do it?

React uses camelCase for inline style properties. Try marginRight: '20px' , just like you did with backgroundColor .

try this: way a)

let styles = {
        marginRight: '20px',
        width: '250px',
        height: '250px',
        backgroundColor: 'yellow',
      };


 <span style={styles} className="badge badge-pill badge-secondary">

way b)

<span style={{margin-right:"20px", width: '250px',height: '250px',background: 'yellow'}} className="badge badge-pill badge-secondary">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM