Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
20
rated 0 times [  24] [ 4]  / answers: 1 / hits: 42840  / 8 Years ago, mon, august 8, 2016, 12:00:00

I have a situation where I need to give dynamic width to the div so I need to use this divStyle = {width: calc(100% - 276px)} in my React Js code. But doing so it gives an error that calc is not a function.



I know this can be achieved using Jquery but I dont want to introduce JQuery to my application. If there is any kind of workaround or hack that might solve this then please share.



code:



customFormat = 'hello-div'
divStyle = {width: calc(100% - 276px)}
return (
<div className={customFormat} style={divStyle}>
Hello World
</div>
)

More From » css

 Answers
18

If you need some more specific CSS you need to put it into quotes - react inline styles doc



<div style={{width: 'calc(100% - 276px)'}}></div>


In your exact case



customFormat = 'hello-div'
divStyle = {width: 'calc(100% - 276px)'}
return (
<div className={customFormat} style={divStyle}>
Hello World
</div>
)


In case you need to overwrite multiple widths (fallbacks) for browser compatibility



divStyle = {width: 'calc(100% - 276px)',
fallbacks: [
{ width: '-moz-calc(100% - 276px)' },
{ width: '-webkit-calc(100% - 276px)' },
{ width: '-o-calc(100% - 276px)' }
]}

[#61106] Friday, August 5, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stephonkeandrer

Total Points: 392
Total Questions: 94
Total Answers: 100

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
;