I have such a html project structure:
<div id="root">
<div class="main-c container-fluid>
</div>
...
<div>
My index.tsx
:
import './index.css';
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);
root.render(
<React.StrictMode>
<HashRouter>
<App />
</HashRouter>
</React.StrictMode>
);
app:
import "bootstrap/dist/css/bootstrap.min.css"
function App() {
return (
<Container className="main-c" fluid>
...
</Container>
);
}
And I don’t understand how to access the backgroud-color field from root?
That is, if i open the page elements in the browser, it shows that root has such a body css:
body {
margin: 0;
font-family: var(--bs-body-font-family);
font-size: var(--bs-body-font-size);
font-weight: var(--bs-body-font-weight);
line-height: var(--bs-body-line-height);
color: var(--bs-body-color);
text-align: var(--bs-body-text-align);
background-color: var(--bs-body-bg);
-webkit-text-size-adjust: 100%;
-webkit-tap-highlight-color: transparent;
}
And here the background color has the color #fff
, and I want to make #fff0
. But how to access this field?
i tried:
index.css:
:root{
background-color: #fff0;
}
But nothing happens. How to access this field?
I have tried all the options below:
body {
background-color: #fff0;
}
#root{
background:#fff0;
}
But none of them works. I.e.
color is still white.
5
Answers
In React, you can access the root/body div element of your application using the document object. Here’s an example:
Get the root/body element
const rootElement = document.getElementById('root');
// Update the background color of the root/body element
rootElement.style.backgroundColor = 'red';
In the above example, we use the getElementById method of the document object to retrieve the root/body element of the application, and then update its background color to red using the style property.
However, it’s generally not recommended to directly manipulate the DOM in React, as it can lead to unexpected behavior and make your code harder to maintain. Instead, you should use React’s component-based approach and state management to update the UI.
Or using CSS
To select the root/body element in CSS, you can use the body selector:
If you want to target the root div element specifically (which is often used as the root element for React applications), you can use its id selector:
#root { /* your styles here */ }
if changing body color is the outcome you are looking for, this would work:
in src/index.css
for access an element by id you need
#
selector onindex.css
:also you can access an element in
index.js
and change it’s style :Edit :
browser always apply last css, for override a
bootstrap
css style you need to importcustom css file
afterbootstrap
file :and another option is using of
!important
,when you use
!important
, browser ignore styles that come after it!If you’re trying to access the root div element, there are two solutions you can try.
The first solution is to use another div as the actual root.
The second solution is to store the root div using
document.querySelector
in a constant variable and share it across your components. This approach is useful if you need to access the root div from multiple components.Try this: