Reputation: 129
I have a problem, I need to hide all divs inside parent div except the first one. Problem is, divs have no ID or anything.
Is there a possible way how to do it? Preferably by CSS or pure JS?
<div role="list" class="slds-form">
<div class="slds-grid">Visible</div>
<div class="slds-grid">Hide</div>
<div class="slds-grid">Hide</div>
</div>
Thank you for any advice :)
Upvotes: 0
Views: 144
Reputation: 2630
Can add an extra class to toggle show hide.
.slds-hide {
display: none;
}
.slds-show {
display: block;
}
<div role="list" class="slds-form">
<div class="slds-grid slds-show">Visible</div>
<div class="slds-grid slds-hide">Hide</div>
<div class="slds-grid slds-hide">Hide</div>
</div>
Upvotes: 1
Reputation: 74738
You can use :not(:first-child)
:
.slds-form>div:not(:first-child) {
display: none;
}
<div role="list" class="slds-form">
<div class="slds-grid">Visible</div>
<div class="slds-grid">Hide</div>
<div class="slds-grid">Hide</div>
</div>
Upvotes: 1
Reputation: 6797
This will work for you:
you can combine child selection(
:first-child
) with:not
to attain the result you want.
.slds-form > div:not(:first-child) {
display: none;
}
<div role="list" class="slds-form">
<div class="slds-grid">Visible</div>
<div class="slds-grid">Hide</div>
<div class="slds-grid">Hide</div>
</div>
Hope this was helpfull for you.
Upvotes: 1
Reputation: 370679
Use nth-child
:
.slds-form > div:nth-child(n + 2) {
display: none;
}
<div role="list" class="slds-form">
<div class="slds-grid">Visible</div>
<div class="slds-grid">Hide</div>
<div class="slds-grid">Hide</div>
</div>
Upvotes: 2