Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add list-style-type to react inline style

Tags:

css

reactjs

I am trying to add a style to a list in React:

const listStyle = {
  list-style-type: none,   // Error
};

and then:

const Box = () => (
   <ul style={listStyle}>
     <li>one</li>
     <li>two</li>
   </ul> 
);

But I get an unexpected token error. Is list-style-type not supported?

Even doing it inline gives an error:

<ul >
  <li style={{list-style-type: "none"}}>One</li>
  <li style={{color: "red"}}><Checkbox />Two</li>     
</ul>
like image 920
EvilEddie Avatar asked Jan 12 '18 01:01

EvilEddie


People also ask

How to add inline styles to the post component in react?

In react, we can use the style attribute to add a inline styles to the dom elements, but we need to pass the styles as a javascript object instead of css string. Note: The JavaScript object properties should be camelCased. Let’s see an example. Now, we are adding inline styles to the Post component.

What is the best way to style react elements without CSS?

The easiest choice: Inline styles. Not the full flexibility of CSS, but decent basic styling at top level specificity. Every React HTML element has a style property that allows an object with all your styling. Objects can look like this:

Can inline styles be specified as a string?

In React, inline styles are not specified as a string. The style attribute accepts a JavaScript object with camelCased properties. For example: margin-top -> marginTop , border-radius -> borderRadius , font-weight -> fontWeight , background-color -> backgroundColor

How to add styling to the component using JavaScript?

This is one of the interesting ways to add styling to the component. We can create an object containing styling for each component and then interpolate the style object to the HTML Element. Since the styling is bound to the JavaScript Object, the styles are dynamic. As soon as the object updates the styles are also updated.


1 Answers

Your problem is with name of the style properties, it should be in the camel case format:

<li style={{ listStyleType: "none" }}>One</li>

Documentation on inline styling in react is here.

like image 113
Sergii Rudenko Avatar answered Oct 19 '22 07:10

Sergii Rudenko