Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use CSS/Jquery calc function in React Js

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> ) 
like image 277
iamsaksham Avatar asked Aug 08 '16 07:08

iamsaksham


People also ask

How do you use Calc in React CSS?

To use the calc() function in React:Set the style prop on the element. Pass the result of calling calc() as the value for a CSS property. The call to calc() should be wrapped in a string, e.g. 'calc(100% - 600px)' .

Can I use CSS calc within JavaScript?

Yes, calc() will work when setting styles in javascript.

Can I use Calc CSS?

calc() The calc() CSS function lets you perform calculations when specifying CSS property values. It can be used anywhere a <length> , <frequency> , <angle> , <time> , <percentage> , <number> , or <integer> is allowed.

Can you use both jQuery and React?

No. No approach is correct and there is no right way to use both jQuery and React/Angular/Vue together. jQuery manipulates the DOM by, for example, selecting elements and adding/deleting stuff into/from them.


1 Answers

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)' } ]} 
like image 155
Majky Avatar answered Oct 13 '22 02:10

Majky