This question already has answers here:
Is there a CSS parent selector?
(33 answers)
On hover of child, change background color of parent container (CSS only)
(3 answers)
Closed 3 years ago.
.background{
width: 200px;
height: 100px;
background: ;
}
input:checked ~ div{
background: blue;
}
<input type="checkbox" id="hamburguesa">
<label for="hamburguesa">click here</label>
<div class="background">aasd</div>
how can i dothe first result on the second snippet?
there is some way to select a father element?
i want change events using only css without js
.background{
width: 200px;
height: 100px;
background: ;
}
input:checked ~ div{
background: blue;
}
<div class="hambur">
<input type="checkbox" id="hamburguesa">
<label for="hamburguesa">click here</label>
</div>
<div class="background">aasd</div>
CSS Selector Level 4 has a selector called ":has", but it isn't compatible with any browser yet, so you'll have to use javascript/jquery for now.
:has()
This is probably the best solution you can find right now:
Is there a CSS parent selector?
Related
This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Is there a "previous sibling" selector?
(30 answers)
Closed 11 months ago.
The community reviewed whether to reopen this question 11 months ago and left it closed:
Original close reason(s) were not resolved
Referring to this Stackover question from 2009 (Is there a "previous sibling" selector?), it seems that it was not possible then.
Here are two small examples that illustrate the problem
both elements touched by the CSS are under the triggering element.
In example two one Element is above the triggering element and the other remains below it. As a result, the sibling selector does not affect the element on top.
Example one
.toggle-switch {
padding:50px;
}
#nocheck {
margin-bottom: 2px;
}
#chkTest:checked ~ #check { color: green; }
#chkTest:checked ~ #nocheck { color: black; }
#check { color: black; }
#nocheck { color: blue; }
<div class="">
<div class="toggle-switch">
<input type="checkbox" id="chkTest" name="chkTest">
<label for="chkTest">
<span class="toggle-track"></span>
</label>
<div class="" id="nocheck">ENABLE</div>
<div class="col-3 col-md-3" id="check">DISABLE</div>
</div>
</div>
Example 2
.toggle-switch {
padding:50px;
}
#nocheck {
margin-bottom: 2px;
}
#chkTest:checked ~ #check { color: green; }
#chkTest:checked ~ #nocheck { color: black; }
#check { color: black; }
#nocheck { color: blue; }
<div class="">
<div class="toggle-switch">
<div class="" id="nocheck">ENABLE</div>
<input type="checkbox" id="chkTest" name="chkTest">
<label for="chkTest">
<span class="toggle-track"></span>
</label>
<div class="col-3 col-md-3" id="check">DISABLE</div>
</div>
</div>
It's the same issue as with a parent selector, CSS can only see DOWN the DOM not back up. As in it can only see future siblings, children, children of children etc.
Safari I think has implemented the :has() pseudo class in one of their dev versions but they are a small browser and Chrome has yet to implement anything.
You might get duped as a few questions on here, this is probably a useful one:
Is there a CSS parent selector?
Kevin Powell has a great video
https://www.youtube.com/watch?v=2-xdcDsqsAs
This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 3 years ago.
Need to make the after element show up when its input is checked.
I know that input elements do not accept pseudo elements, this is the main reason why I am not using the after element on the radio input.
.entireLine label:after input[type="radio"]:checked{
position: absolute;
top: 2px;
left: 15px;
width: 14px;
height: 14px;
border-radius: 50%;
background: #13abe1;
color: transparent;
content: "";
}
<div class="entireLine">
<label>
<input type="radio" name="person_type" value="J" checked="checked">
PESSOA JURÍDICA
</label>
</div>
Goal: make the after element show up when the input is checked.
Errors: None
You are trying to style the radio when it's checked, no the label:after pseudo element.
What you can do for that is using a sibling element of the radio and select it with a sibling selector: radio:checked + .new-element (or ~ if .new-element is not the adjacent element).
Your html will like like this:
<label>
<input type="radio"/>
<span class="new-element" />
</label>
This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 4 years ago.
I want to edit css style for a Class3 which is inside a Class2 right after a Class1.
but I only want to edit style if Class1 has a specialClass inside, like this:
<div class="class1">
<div class="specialClass"></div>
</div>
<div class="class2">
<span class="class3"></span>
</div>
I tried this, but it doesn't work:
.class1 .specialClass ~ .class2 .class3 {}
Is this even possible? If yes can someone explain me the error I made?
$(function(){
if($('.class1').find('.specialClass').length > 0){
$('.class3').css("background-color", "yellow");
}
})
div{
width: 100px;
height: 100px;
border: 1px solid black;
}
span{
padding-left: 50px;
border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="class1">
<div class="specialClass"></div>
</div>
<div class="class2">
<span class="class3"></span>
</div>
This question already has answers here:
How to combine class and ID in CSS selector?
(10 answers)
Closed 5 years ago.
What does it mean in css when a class comes right after an id (there is no space between them)? such as this:
#bgimage.header-image
Thanks
It means that this formula will be used for the same element, such as
<div id="bgimage" class="header-image"></div>
If you'll make space between #bgimage and .header-image then it will be used to:
<div id="bgimage"><div class="header-image"></div></div>
It means an element with id and class at the same time. Eg:
<div id="bgimage" class="header-image"></div>
See the below example for difference between the selectors.
/* _class should be a child of _id */
#_id ._class{
background: #ccc;
width: 100px;
height: 100px;
}
/* It should have both id and class */
#_id._class{
background: #faa;
width: 100px;
height: 100px;
}
<div id="_id">
<div class="_class">
Box 1
</div>
</div>
<div class="_class" id="_id">
Box 2
</div>
This question already has answers here:
What does an asterisk (*) do in a CSS selector?
(5 answers)
Closed 7 years ago.
What is the meaning of * in this code ?
.descendants * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
<div class="descendants" style="width:500px;">div (current element)
<p class="1">p (child)
<span>span (grandchild)</span>
</p>
<p class="2">p (child)
<span>span (grandchild)</span>
</p>
</div>
* in CSS represents "any element".
In the above example, this will target any and all sub-elements of .descendants. Notice how the text in the first div is black, and the p and span content is grey and has a border? That is because the CSS selector is targeting those elements.