I have a <Parent />
component with a single <Child />
:
<Parent>
<Child />
</Parent>
I want to pass a CSS class from the parent into the <Child />
, so I do this:
<Parent>
<Child className={ styles.pill } />
</Parent>
The <Child />
has a className
prop and applies the class using the classnames package, like this:
<div className={ classNames(styles.pill, props.className) } />
But you see the <Child />
has a class called styles.pill
with these styles:
.pill {
align-content: stretch;
align-items: center;
color: var(--grey__text--dark);
cursor: pointer;
display: flex;
flex-flow: row nowrap;
justify-content: flex-start;
}
The class the parent passed in contains these styles:
.pill {
align-items: flex-end;
flex-flow: column wrap;
}
With this setup I can’t guarantee that the styles passed in from the parent will override the styles within the <Child />
component, which is exactly what I want them to do. Currently you never know which will trump the other. It’s seemingly random with every build of the project.
How can I ensure that the CSS properties in class passed in from the parent override any similar properties in the Child component?
To use CSS child selectors in React Material UI, we can start the property names of the style properties with &.
Use the CSS child combinator In your parent stylesheet, you can use the direct children selector > to select any direct children. You can also combine this operator with the star operator, but be careful with this one since it may slow the browser if used to frequently on a page If we assume all your Child component is a div:
But sooner or later, you would realize there is one thing we cannot easily do anymore in CSS modules – a rather important (if not fundamental) thing in CSS: selecting and overwriting the styles of the (deep nested) child component that’s in a different module from the parent.
This first one is to wrap each of your child components in a div and then add a class on it which then you can reference in your stylesheet: You can pass the class name as props and then add this props to any tag you want in your Child component. On the other hand, you have to do this for every components that you would like to have a class.
It is a matter of CSS specificity, there are many ways to insure the override.
Multiple selector trick, in order to enlarge the specificity you can double the selector size by duplicating the class name, in you case, .pill.pill { ...}
will be more specific than the original, and therefore will override the style.
Usage of !important
, you can specify that in the class of the parent, .pill
's declaration will contain !important
at the end.
Multiple different selectors, the parent will pass additional different className
, that child component will address, .pill.parent-pill {...}
, the parent will pass the .parent-pill
class to the child.
Those are just 3 easy ways to override the child styles there are more.
You can use Object.assign
to merge one/many objects into a target object, giving precedence to each subsequent object:
const foo = { a: '1', b: '2' }
const bar = { a: '3' }
console.log(Object.assign(foo, bar));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With