Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write inline styles in react

Tags:

reactjs

whats wrong with this line? var firstColStyle = {text-align: right};

This works: var firstColStyle = {width: 70};

Seems to be a odd syntax for styles?? The documentation about this issues is very poor. How to make elements reactive, so I can't use 20% instead of 300px.

With best regards

Gerhard

like image 246
Gerhard Avatar asked Jan 10 '16 09:01

Gerhard


People also ask

How do you write inline styles in React JS?

To use inline styles in React, use the style attribute, which accepts a JavaScript object with camel properties. Example: function MyComponent(){ return <div style={{ color: 'blue', lineHeight : 10, padding: 20 }}> Inline Styled Component</div>

Can We Use inline CSS in React?

Rule of Thumb. The official React documentation frowns upon the use of inline styling as a primary means of styling projects and recommends the use of the className attribute instead.

How do you write multiple inline styles in React?

Use the spread syntax to combine multiple inline style objects in React, e.g. style={{...style1, ... style2}} . The spread syntax will unpack the key-value pairs of the objects into a final object and the styles will get applied to the element. Copied!

How do you create an inline style?

An inline style may be used to apply a unique style for a single element. To use inline styles, add the style attribute to the relevant element. The style attribute can contain any CSS property.


1 Answers

Change firstColStyle to:

var firstColStyle = { textAlign: 'right' }

If you have to set px you can:

var someSize = { width : '2px' }

If percentage:

var otherWidth = { width : ' 10%' }
like image 191
Krzysztof Sztompka Avatar answered Sep 23 '22 14:09

Krzysztof Sztompka