import React from 'react'; const style = { h2 : { fontSize: '20px'; color: 'black'; } & span: { color: 'white'; } } const Main = () => { return ( <h2 style={style}>Hello<span>Test</span></h2> ); } export default Main;
Note: can be dynamic. H2 is the parent tag and I have applied the style in the same tag and I want it to apply to the child tags as well ().
JSX style attributes are similar to HTML style attributes. Both the style attribute and attribute only accept CSS properties. Therefore, the use of selectors, pseudo-elements or pseudo-classes is not allowed.
Using style attributes
Replace the following:
and:
or clearer:
However, since you only want to define styles for h2 elements, we have more options:
CSS/SCSS
Using CSS on separate files:
Among them, the
.your-css.css
file containsEven nested (if you use a preprocessor like .scss)
CSS-in-JS
Considering a more "React" solution, we could use CSS-in-JS like
styled-components
Learn more