Conditionally set style if sibling element exists in PURE CSS [duplicate] - html

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>

Related

How to apply css on a div that has no class or id? [duplicate]

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.

Select most top level element [duplicate]

This question already has answers here:
CSS selector for first element with class
(23 answers)
How to apply css to top level element only
(2 answers)
Is there a CSS parent selector?
(33 answers)
Closed 3 years ago.
I have a html code :
<div class="body">
<p></p>
<div class="select">
<p class="p1"></p>
<div class="select"></div>
<p class="p2"></p>
</div>
</div>
I want to select div element with class select. There are two element of this type which I want the parent one. How can I do that? Pay attention that the above code is just an example and the code can become complex arbitrarily.
Two div elements with same class may not be direct child and parent of each other and I want to do this by css-selectors if possible.
Style the .select and then overwrite those styles in .select .select:
.select {
/* top level select element */
color: red;
}
.select .select {
/* reset styles in nested select element */
color: initial;
}
<div class="body">
<p>Text</p>
<div class="select">
<p class="p1">Top Level Select</p>
<div class="select">Nested Select</div>
<p class="p2">Top Level Select</p>
</div>
<div>
<p class="p1">Text</p>
<div class="select">Top Level Select</div>
<p class="p2">Text</p>
</div>
</div>

Display div only if another element has a class [duplicate]

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.

Styling parent if it contains child of specific class [duplicate]

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');
});

CSS selector getting parent when you know the child [duplicate]

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 */);