Problem Statement
How do I apply css to all elements of a certain class except when first child is of certain id?
Examples
I have html as follows:
HTML Example 1
<div class="content-body">
<div>
</div>
</div>
HTML Example 2
<div class="content-body">
<div id="container-special">
</div>
</div>
css as follows:
.content-body {
max-width: 1050px;
}
Now, I would like the css to apply only when the first child div is not of id=container-special
, i.e, I would like the css to take effect only for example 1.
What I have tried
I have considered css selectors :first-child
, and not
, but I am unable to figure out how to apply them for my case.
Any help is appreciated.
Update1 [Solution]
Thanks to everyone who answered my question. Based on provided inputs, I used the following solution:
.content-body {
max-width: 1050px;
}
// called when component containing <div id="container-special> is loaded
fixParentContainerWidthOnMount() {
const cr = document.getElementById('container-special')
const parent = cr.parentElement
if (parent.className.includes('content-body')) {
parent.classList.remove('content-body')
}
}
// called when component containing <div id="container-special> is unloaded
fixParentContainerWidthOnDestroy() {
const cr = document.getElementById('container-special')
const parent = cr.parentElement
if (
parent.className.includes('container') &&
!parent.className.includes('content-body')
) {
parent.classList.add('content-body')
}
}