Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add CSS to react created elements like data-reactroot

Tags:

Under my root div react creates another div "automatically". Is there a way to add a class to that div? I need to add height: 100% to it to prevent the background content to scroll in mobile when an overlay is shown.

This is how it is shown when i inspect the element on the site. I need to add height:100% to the data-reactroot div when a button is clicked. But that div is nowhere in my source code.

<div id="root">     <div data-reactroot data-reactid="1" data-react-checksum="161698... 

I could add this code in the container's componentDidMount()

    let root = document.querySelectorAll('[data-reactroot]')[0];     if (root) {         root.style.height = '100%';     } 

But isnt there a better way to do it? This feels like kind of hacky (in a bad way)

like image 232
user3711421 Avatar asked Dec 21 '16 09:12

user3711421


People also ask

How do I change the CSS of a element in React?

To change the style of an element on click in React:Set the onClick prop on the element. When the element is clicked, set the active state. Use a ternary operator to conditionally set the new styles based on the state variable.

Do you use CSS with React?

React doesn't do stylingReact components output HTML, and CSS styles the HTML. As far as the browser is concerned, React doesn't figure into it. The browser applies CSS rules to the HTML on the page, whether that HTML came from an . html file or was generated by React.

Can I use document getElementById in React?

The equivalent of the document. getElementById() method in React is using refs. To select an element, set the ref prop on it to the return value of calling the useRef() hook and access the dom element using the current property on the ref , e.g. ref. current .

Do React developers use CSS?

React developers build web applications or use React Native to develop mobile apps on the client-side of development. This requires the use of core front-end technologies like JavaScript, HTML, and CSS.


1 Answers

#root > [data-reactroot] { height: 100% } in your CSS should work.

Or just the tag itself:

[data-reactroot] { ... } 
like image 78
Fabian Schultz Avatar answered Sep 30 '22 06:09

Fabian Schultz