Let’s say I have the following parent classes: .TestPageA, .TestPageB, .TestPageC
.
My question is, how can I ignore the padding padding: 50px 0 44px 0;
under subComponent
just for .TestPageB
class. So if it’s .TestPageA
or .TestPageC
it can have the padding: 50px 0 44px 0;
. But if it’s .TestPageB
, then the padding will be padding: 40px 0 40px 0;
.
How can I set up my CSS properly for these types of scenario? Do I need to create a whole separate CSS block just for .TestPageB
to add it’s padding? Or is there a way to do it all in one block?
.TestPageA, .TestPageB, .TestPageC{
.TopSection, .BottomSection {
.subComponent {
padding: 50px 0 44px 0;
.leftSide {
padding: 0;
.title {
width: 100%;
float: left;
padding-bottom: 20px;
h2, h3 {
.borderBottom {
border-bottom: solid 1px #fff;
width: 100%;
}
}
h2.page {
margin: -6px 0 4px 0;
}
svg, h2, h3 {
float: left;
}
}
}
}
}
}
3
Answers
You can use it separately by removing the padding from the subcomponent and declaring some new custom CSS rules for the classes that you have
Just override the padding for the TestPageB, like this:
What you could do is to utilize
:not()
and:is()
pseudo classes:Here’s a live example:
FYI CSS nesting is already supported by native CSS for the majority of mordern browsers.