Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React js css inline style margin right not working

Tags:

css

reactjs

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?

like image 440
Jeremy Parker Avatar asked Apr 01 '19 20:04

Jeremy Parker


People also ask

How do you give a margin right in React JS?

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

Why CSS is not working in React JS?

This error is generated because the compiler is only able to import files from the src folder. Here, the CSS file is saved outside the src folder, so the compiler failed to import it. To make this code work, you just have to save the CSS file inside the src folder.

Can We Use inline CSS in React?

In React, inline styles are not specified as a string. The style attribute accepts a JavaScript object with camelCased properties. Below are the basic steps for defining inline CSS: 1. Change the CSS property name to its camelCase version like "background-color" to "backgroundColor", "font-size" to "fontSize", etc.


Video Answer


1 Answers

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

like image 183
larz Avatar answered Sep 19 '22 15:09

larz