Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React inline style with const and "text"

Tags:

css

reactjs

I want to have a const style and inline hardcoded style to my element lets say I have this:

const style = {
    cursor: "pointer",
    border: "2px solid #b1b1b1",
    borderRadius: "8px",
    padding: "0px 10px 0px 10px",
    backgroundColor: "green"
}

But I also want additional style to the element lets say width

<div style={style{width: "20%"}}>test</div>

This is not working, how can I achieve this?

like image 270
pat Avatar asked May 13 '26 15:05

pat


1 Answers

You can use the spread syntax for assigning your styles.

<div style={{...style, ...{width: "20%"}}}>test</div>

const style = {
    cursor: "pointer",
    border: "2px solid #b1b1b1",
    borderRadius: "8px",
    padding: "0px 10px 0px 10px",
    backgroundColor: "green"
}

const newStyles = {...style, ...{width: "20%"}};

console.log(newStyles);
like image 152
Ana Liza Pandac Avatar answered May 18 '26 17:05

Ana Liza Pandac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!