Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add !important into React inline CSS style

Tags:

css

reactjs

How to add !important into my inline CSS property? If I add it after 2px it just ignore whole style.

import React from "react";

export default class Todo extends React.Component {
    render() {
        const {text} = this.props;

        const cardStyles = {
            borderWidth: '2px'
        };

        return (
            <div class="card mb-2 border" style={cardStyles}>
                <div class="card-body">
                    <h5 class="card-title m-0">{text}</h5>
                </div>
            </div>
        )
    }
}
like image 964
Baterka Avatar asked Aug 14 '18 22:08

Baterka


People also ask

Does important work in inline CSS?

Inline styles are styles defined using the style attributes. They can also be normal or important.

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.

What is important in react JS?

One of the main benefits of using React JS is its potential to reuse components. It saves time for developers as they don't have to write various codes for the same features. Furthermore, if any changes are made in any particular part, it will not affect other parts of the application.

What is valid for inline styling in react JS?

To use inline styles in React, use the style attribute, which accepts a Javascript object with camelCased properties.


2 Answers

Apparently it is not supported https://github.com/facebook/react/issues/1881#issuecomment-262257503

meanwhile you can use a hack doing:

        <div ref={element => { 
             if (element) element.style.setProperty('border', '2px', 'important'); 
           }}
        />
like image 81
ncubica Avatar answered Oct 27 '22 10:10

ncubica


It appears it currently isn't supported based on this GitHub issue https://github.com/facebook/react/issues/1881

like image 45
Ollie F Avatar answered Oct 27 '22 09:10

Ollie F