I have a div with the class .box-container which can contain several div of class .legend-panel, I want to apply some style only to the first child which is not hidden in the box-container.
I'm new on scss so I arrived to target the first child but not more.
.legend-panel:first-child{
bottom: 2.5em;
}
thanks for helping.
You can achieve what you want with a mixture of nots and sibling selectors. The following assumes that you are using a hidden class to hide your elements
.hidden {
display:none;
}
.legend-panel:not(.hidden):first-child, /* this styles the first visible if it is the first child */
.legend-panel.hidden + .legend-panel:not(.hidden) { /* this will style thie first show following a hidden panel */
color:red;
}
.legend-panel:not(.hidden):first-child ~ .legend-panel,
.legend-panel.hidden + .legend-panel:not(.hidden) ~ .legend-panel { /* these will reset all following visible back to original */
color: initial;
}
<div class="box-container">
<div class="legend-panel hidden">
hide
</div>
<div class="legend-panel hidden">
hide
</div>
<div class="legend-panel">
show - first
</div>
<div class="legend-panel hidden">
hide
</div>
<div class="legend-panel hidden">
hide
</div>
<div class="legend-panel">
show - not styled
</div>
</div>
<div class="box-container">
<div class="legend-panel">
show - first
</div>
<div class="legend-panel hidden">
hide
</div>
<div class="legend-panel hidden">
hide
</div>
<div class="legend-panel hidden">
hide
</div>
<div class="legend-panel">
show - not styled
</div>
<div class="legend-panel hidden">
hide
</div>
</div>
EDIT
As #Pete pointed out, the :first-of-type pseudo selector only takes into account the type of element (e.g. div or li), it does not work with classes, so this answer is wrong.
You will have to specifically target the first div that is not hidden. I'm going to assume you hide the div's by adding a class hidden to them.
Using the :not pseudo selector, you can ignore elements, you can use this to ignore all .legend-panel elements that have the class hidden, then you can use the :first-of-type pseudo selector to only select the first of the non-hidden .legend-panel elements. That should be all!
This should be a working approach:
.box-container{
.legend-panel{
// Make all panels blue
background-color: blue;
&:not(.hidden){
&:first-of-type{
background-color: red; // This should be the one
}
}
}
// Or in 1 selector
//.legend-panel:not(.hidden):first-of-type{
// background-color: red;
//}
}
Related
im trying to change the background a siblings div while hovering on the child that exist inside the parent
<div class="parent-container">
<div class="child-container-upper">
</div>
<div class="child-container-bottom">
</div>
</div>
im able to change the background of my bottom-container > upper-container
.child-container-upper {
&:hover {
~.child-container-bottom{background:purple}
}
}
I want while hovering on the bottom-container to effect to upper but it dosent seem to work.
Suggestions?
In the very simple case that you describe then it can be done by sensing when the parent is hovered on and the relevant child is not.
This snippet uses the CSS :not pseudo class to achieve this.
.parent-container:hover .child-container-upper:not(:hover) {
background: red;
}
.parent-container:hover .child-container-bottom:not(:hover) {
background: green;
}
<div class="parent-container">
<div class="child-container-upper">upper
</div>
<div class="child-container-bottom">bottom
</div>
</div>
I want to use css3 nth-child to select matched elements based in their index in the whole document (like jquery :eq() selector) not based in the parent element.
<div id="container">
<div class="result">
<div class="active">content 1</div>
</div>
<div class="result">
<div class="active">content 2</div>
</div>
<div class="result">
<div class="active">content 2</div>
</div>
</div>
This css code select all elements because every .active is 1st child respective to the parent .result
.active:nth-child(1) {
background: red;
}
I tried also to make the body as parent
body > .active:nth-child(1) {
background: red;
}
But it can't do the job.
I want nth-child(1) selects content 1
and nth-child(2) selects content 2
I think you want to use nth-child on .result.
#container .result:nth-child(1) .active {
background: red;
}
JSBin
I'm not sure if this is possible in CSS, but if it is, I would appreciate some help.
I have HTML similar to the following:
<div class="group"></div>
<div class="group"></div>
<div class="group subgroup"></div>
<div class="row"></div>
<div class="row"></div>
<div class="group"></div>
<div class="group subgroup"></div>
<div class="row"></div>
Is it possible to alternate the background colors of the row classes? Always starting with the same color? I've been having trouble achieving this using nth-child and I'm assuming it's because of the group/subgroup classes.
Manual html markup in jsfiddle of an example data set that could be returned and how it is designed to be styled:
http://jsfiddle.net/Qr5Za/
'always starting with the same color' means that the first row after
group/subgroup starts with red
If so, you can set background-color of the first .row red and the others magenta by:
.group ~ .row { /* select all rows comes after each group */
background-color: magenta;
}
.group + .row { /* select and override the first row after each group */
background-color: red;
}
JSBin Demo
These selectors are called General sibling combinator ~ and Adjacent sibling combinator +, you can find more details here.
Update
All new CSS3 selectors like :nth-child(n), :nth-of-type(n) matches every element that is the nth child or type, of its parent.
So the only way to achieve this, is putting .rows in a wrapper for each block:
<div class="group">This is a group</div>
<div class="group subgroup">This is a subgroup</div>
<div class="wrap">
<div class="row">This is the first row</div>
<div class="row">This is the second row</div>
<div class="row">This is the third row</div>
<div class="row">This is the forth row</div>
</div>
And selecting odd and even rows based on their position in the .wraper (their parent):
.row:nth-child(odd) {
background-color: red;
}
.row:nth-child(even) {
background-color: magenta;
}
JSBin Demo #2
.row:nth-of-type(n) + .row:nth-of-type(even){
background: green;
}
.row:nth-of-type(n) + .row:nth-of-type(odd){
background: orange;
}
.group.subgroup + .row:nth-of-type(n) {
background: blue;
}
Updated Demo
My code can add divs as child or sibling, I've created two classes and two :hover for each class but when I put the mouse on a child the parent hover is activated as well.
.Group {
background-color: white;
}
.Group2 {
background-color: white;
}
.Group:hover {
background-color: yellow;
}
.Group2:hover {
background-color: red;
}
<div class="Group">
root
<div class="Group">1st child
<div class="Group2">2nd child
<div class="Group">3rd child</div>
</div>
</div>
<div class="Group">1st child</div>
</div>
If you hover over a child element, you're also hovering over the parent, there's no way around that.
What you could do is set a different or additional class on the 1st child elements that don't have further children.
Highlighting background colors in nested blocks when hovering
If you are trying to activate the background colors of the nested div blocks, I think you might be looking for the following.
For the HTML, use distinct class names to identify each layer of nested div blocks:
<div class="Group">root
<div class="Group1">1st child
<div class="Group2">2nd child
<div class="Group3">3rd child</div>
</div>
</div>
<div class="Group1">1st child</div>
</div>
and the CSS:
.Group, .Group1, .Group2, .Group3 {
background-color:transparent;
}
.Group:hover {
background-color:yellow;
}
.Group1:hover {
background-color:pink;
}
.Group2:hover {
background-color:red;
}
.Group3:hover {
background-color:orange;
}
Demo fiddle: http://jsfiddle.net/audetwebdesign/Scr9G/
As you mouse over each nested div successively, the background color changes in sequence.
If you are trying to target a nested element with :hover without the effect bubbling up through the parent/ancestor blocks, you will need to use JavaScript/jQuery to create the selection rules that you need.
Quirky Hack Using <p> Tags
The following construction exhibits the behavior that the OP would like to see:
<p class="Group">root
<p class="Group">1st child
<p class="Group2">2nd child
<p class="Group">3rd child</p>
</p>
</p>
<p class="Group">1st child</p>
</p>
and the CSS is as before:
.Group {
background-color:white;
}
.Group2 {
background-color:white;
}
.Group:hover {
background-color:yellow;
}
.Group2:hover {
background-color:red;
}
Second demo fiddle: http://jsfiddle.net/audetwebdesign/cf2mn/
In this case, the OP was trying to nest <p> tags, which actually do not work like nesting other block elements like <div>.
When using <p> tags, the closing </p> tag is optional if followed by other flow elements like p, div, ul and so on.
In this case, the HTML snippet show above is equivalent to:
<p class="Group">root</p>
<p class="Group">1st child</p>
<p class="Group2">2nd child</p>
<p class="Group">3rd child</p>
<p class="Group">1st child</p>
which means that all the p tabs are siblings and there are no parent-child relationships, which is why the CSS appears to be working as the OP desired.
If div tags had been used instead of p tags, the resulting DOM would have exhibited the parent-child relationships and the CSS would have shown the original behavior that the OP did not want.
Using the p tags may give the desired effect for the CSS, but it works only because the DOM elements are siblings instead of parent-child. (In addition, the nested p tags will not validate.)
It is worth noting that:
CSS 2.1 does not define if the parent of an element that is ':active' or ':hover' is also in that state.
so it is better not to rely on the state of the parent element when apply a pseudo-element on a child element.
References:
About :hover: http://www.w3.org/TR/CSS2/selector.html#dynamic-pseudo-classes
About optional closing of p tag: http://www.w3.org/TR/html-markup/p.html#p
There's no way to prevent parent elements to get background color.. rather use this code
<div class="root">
Main Root
<div class="group">
First One
</div>
<div class="group2">
Second One
</div>
<div class="group">
Third One
</div>
.group:hover {
background: yellow;
}
.group2:hover {
background: red;
}
SEE THE DEMO HERE
Give separate ids to the divs and when a child div is hovered over, change its background image color and remove the background color of the parent all at the same time. I guess that is the only way around this
The simplest and best way is probably to use any other element, li for instance.
css:
.Group, .Group2, .Group3
{
background:white;
display: block;
list-style: none;
}
.parent
{
margin: 0;
display: block;
padding: 0;
}
.Group:hover
{
background: yellow;
}
.Group2:hover
{
background: red;
}
.Group3:hover
{
background: yellow;
}
html:
<ul class="parent" >
<li class="Group" >root
<li class="Group">1st child
<li class="Group2">2nd child
<li class="Group3">3rd child</li>
</li >
</li >
<li class="Group">1st child</li >
</li>
</ul>
You should add sub-element for content and use element+element pseudo selector.
.group-content:hover {
background-color: yellow;
}
.group-content:hover + .group {
background-color: red;
}
<div class="group">
<div class="group-content">1st child</div>
<div class="group">
<div class="group-content">2nd child</div>
<div class="group">
<div class="group-content">3nd child</div>
</div>
</div>
</div>
<div class="group">
<div class="group-content">1st child</div>
</div>
We have a DOM like this:
<div class="outer">
<div class="inner"> <!--// No "copyright" in this node //-->
<div class="content">...</div>
</div>
<div class="inner">
<div class="content">...</div>
<div class="copyright">...</div> <!--// DISPLAY THIS ONE //-->
</div>
<div class="inner">
<div class="content">...</div>
<div class="content">...</div>
<div class="content">...</div>
<div class="copyright">...</div> <!--// Hide this one //-->
</div>
<div class="inner">
<div class="content">...</div>
<div class="content">...</div>
<div class="copyright">...</div> <!--// Hide this one too, etc. //-->
</div>
<!--// etc. //-->
</div>
All elements with class "copyright" must be hidden, with exception of the very first one.
We tried to apply this approach, but unfortunately with no success. It must be a CSS only solution. Any idea?
Thanks for your help!
In this case, each .copyright is the first and only one of its kind in .inner, so you need to select by .inner instead. If you don't need to apply any special rules to the first child, you don't need to use the approach I describe in that other question; simply use this to hide the other elements:
.inner ~ .inner .copyright {
display: none;
}
This is still the top answer on Google for "css select first occurrence of class" so adding the simple technique I found to work.
This solution doesn't specifically solve the OP but does allow you to select the first element with a class amongst siblings.
You can use a combination of the sibling and not selectors as shown in this JSFiddle
For example:
.my-class:not(.my-class ~ .my-class) {
background: red;
}
How does this work?
The sibling selector (~) selects elements which are somewhere after other elements.
So this would select every element except the first one:
.my-class ~ .my-class {
background: red;
}
We then just use the :not selector to reverse this, i.e. select only the first element.
I have only tested this on Chrome but think it should work on most modern browsers.
Try this one JSfiddle
div.inner > .copyright { display:none; }
div.inner:first-child .copyright { display:block; background:#000; }