This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 9 years ago.
I have html like this:
<div class="first">
<div class="second home">
</div>
</div>
<div class="first">
<div class="second">
</div>
</div>
Is it possible to style div element with class first if child div has home class?
Can do it using jquery
$($('.home').parent('div')).addClass('first');
try something like this
$('.home').parent('div').addClass('first');
All child element with home than
$('.home').each(function(){
$(this).parent('div').addClass('first');
});
Related
This question already has answers here:
Is there a "previous sibling" selector?
(30 answers)
Is there a CSS parent selector?
(33 answers)
Closed 1 year ago.
In mu angular project, I have simple divs like
<div class="parent">
<div class="child1">
div1
</div>
<div class="child2 green">
div2
</div>
</div>
.green {
background-color:green
}
You could see that div2 has background color green(it is added dynamically from a third-party plugin), how could I write scss code to make the div1 also shows green when div2 has the class 'green', is it possile?
This question already has answers here:
Is there a CSS selector for element without any class?
(1 answer)
Can I write a CSS selector selecting elements NOT having a certain class or attribute?
(10 answers)
Closed 2 years ago.
I have two div and I want to apply some css on the first div that has no class or id .How can I do it ??
Html:
<div>some content</div>
<div class="text-1">
You can use the :not() selector:
div:not([class]) {
color: red;
}
<div>some content</div>
<div class="text-1">some more</div>
Basically this says select any div that doesn't have the class attribute.
This question already has answers here:
CSS: Select element only if a later sibling exists
(9 answers)
Closed 5 years ago.
I have the following structure, where second element may or may not appear.
<div class="wrapper">
<div class="firstElement"></div>
<div class="secondElement"></div>
</div>
I want to conditionally set styles on .firstElement ONLY if .secondElement exists.
Is there a way to do this with PURE CSS? With either sibling selectors/ parent selectors?
Thanks!
In general, no. CSS reads forwards/down the DOM - it won't read backwards/up. But with this markup, you could use :not(:last-child)
.firstElement:not(:last-child) {
color: red
}
<div class="wrapper">
<div class="firstElement">target this</div>
<div class="secondElement"></div>
</div>
<div class="wrapper">
<div class="firstElement">not this</div>
</div>
This question already has answers here:
Is there a "previous sibling" selector?
(30 answers)
Closed 7 years ago.
I want to display the toggle_embed class only if the a element has has-embed class. Is there any way I can solve this using CSS?
<div class="comment HAS_EMBEDDED">
<div class="toggle_embed">Embedded content</div>
<a class="has-embed">#name</a>
<a>Text</a>
</div>
NO. There's no previous selector in css. So, you can't do this just with css, you may use jQuery for this.
But if you want to use pure css solution then what about changing the markup like below?
<div class="comment HAS_EMBEDDED">
<a class="has-embed">#name</a>
<div class="toggle_embed">Embedded content</div>
<a>Text</a>
</div>
Then you can use css like this:
.toggle_embed{
display: none;
}
.has-embed + .toggle_embed{
display: block;
}
Note: Changing the markup, you may have to re-work for your layout.
This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 9 years ago.
I have the following HTML
<div id"mainDiv">
<ul id="cat1">
</ul>
</div>
<div id"mainDiv">
<ul id="cat2">
</ul>
</div>
I would like to select the "mainDiv" which has a child ul "cat1", in my CSS as I want to apply some styling on that div. But not the all maindiv's
Any ideas?
Your markup is invalid:
<div id"mainDiv">
should be
<div id="mainDiv">
Since duplicate ID's are invalid in HTML, your question is really invalid in this context.
You should either use a class OR rethink your structure.
Example for the first div:
<div class="mainDiv firstdiv">
and subsequent divs:
<div class="mainDiv">
CSS:
.firstdif{}
put your CSS in that.
No CSS selector for this currently, so you're going to have to resort to some JavaScript/jQuery:
$('#cat2').parent().css(/* add it here */);