How to properly select these elements? - css

<div id="main-content">
<div>
<div>target me
<div>don't target me</div>
</div>
</div>
<div>
<div>target me too
<div>don't target me</div>
</div>
</div>
</div>
I've tried this:
#main-content div>div {
}
But this ALSO targets the divs saying "don't target me" I wish not to target those divs.
Of course we can use Id's or classes, but the point is to declare a general rule for all.
Please advice.

Just refine the selector a bit to enforce the hierarchy: #main-content > div > div
http://jsfiddle.net/zXaLU/
As a note, when using structural selectors it's nice to reference non-generic tags.
Example: #main-content > NAV > UL is more meaningful than #main-content > DIV > DIV

If you want styles only to apply to the outer of the two divs, you need to use two style definitions. The first sets the style for the div targeted and the second for the inner div not to be targeted:
#main-content div>div {
/* set some styles */
}
#main-content div>div>div {
/* reset the styles defined before */
}
In general the inner div (not targeted) inherits all the styles of its parent div, so in order to nullify that effect, you have to explicitly reset all those styles again.
EDIT
After all comments: If "targeting" does not include usual CSS inheritance, Tim Medora's answer is more suitable. My answer tried to account for inheritance as well.

How [dooes one] properly select [the specified] elements?
The "proper" way would be to give the items you want to select a class that is indicative of their status:
<div id="main-content">
<div>
<div class="someclass">target me
<div>don't target me</div>
</div>
</div>
<div>
<div class="someclass">target me too
<div>don't target me</div>
</div>
</div>
</div>
...and then you can simply use the class selector:
.someclass {
...styles...
}
But if you're unable to modify the markup, you can still use the child selector chain:
#main-content > div > div {
...styles...
}

Related

How do i style two same class divs differently?

So basically I've got a setup that spits out the code in the following fashion..
<div class="parent">
<div class="subparent">
<div class="TARGETCLASS"></div>
</div>
<div class="subparent">
<div class="TARGETCLASS"></div>
</div>
</div> //close for the parent class
Now what I'm trying to do is to style "TARGETCLASS" that comes above one way and the "TARGETCLASS" that comes second in another way. I tried n-th child, but unable to achieve the result I'm looking for. There's no way to add additional classes or ID to the existing "TARGETCLASS" class. Otherwise I wouldn't be posting this question :)
Also, the "subparent" class also is same. for both the targetclass classes. That's the issue
Thanks in advance for taking your time to answer this question for me.
Cheers!
Looks like you've got some mal-formed tags in your html. And nth-child should work just fine. Also, make sure you place the nth-child selector on the subparent class, and not TARGETCLASS. It's common to mis-place the child selector. Try this:
<div class="parent">
<div class="subparent">
<div class="TARGETCLASS">
first-child
</div>
</div>
<div class="subparent">
<div class="TARGETCLASS">
second-child
</div>
</div>
</div>
<style>
.parent .subparent .TARGETCLASS {
background-color:#f00;
}
.parent .subparent:nth-child(1) .TARGETCLASS {
background-color:#0f0;
}
</style>
fiddle: https://jsfiddle.net/8ejxokuj/
I would use nth-of-type selector like so:
.parent{}
.parent > .subparent {} //targets both subparents
.parent > .subparent:nth-of-type(2) {} //targets the second subparent
.parent > .subparent:nth-of-type(2) > .TARGETCLASS{} //targets the child of the second subparent
The nth-of-type() selector enables you to style a specific element amongst a series, in this case we targeted the second .subparent then specified the child we needed.
I hope this helps!
It seems, it is working by the nth child.
it is about how childrens are called. Not like "Ask parent to find nth child, but ask child, how far is he from parent"
.parent .subparent:nth-child(1) {background: #FEE; color:RED;}
.parent .subparent:nth-child(2) {background: #EEF; color:blue;}
<div class="parent">
<div class="subparent">
<div class="TARGETCLASS">aaa</div>
</div>
<div class="subparent">
<div class="TARGETCLASS">bbb</div>
</div>
//close for the parent class
</div>

Apply css only to first child and cancel inherintance

I have :
<div class="myclass">
<div >
<div>
<div>
</div>
</div>
</div>
</div>
Ok, I'm using .myclass div:first-child {} to give style to the first div but I discover how the style is applied by inheritance to the nested divs....????
Any idea what I'm doing bad ?
.myclass > div:first-child {} will only affect the direct child div.
More information on the various selectors available is here: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started/Selectors

CSS selector that applies only to the descendants with closest depth

I have two class HoverShow and HoverHidden.
HoverHidden elements should have at least one HoverShow ancestor and only be displayed when the closest HoverShow ancestor is hovered.
.HoverHidden
{
display: none;
}
.HoverShow:hover .HoverHidden
{
display: initial;
}
<div class="HoverShow">
<div>
Hover here to see message!
<div class="HoverHidden">
message!
</div>
</div>
</div>
The above works just fine.
But things get more complicated when HoverHidden has several HoverShow ancestors.
<div class="HoverShow">
<div>
Hover here to see message!
<div class="HoverHidden">
message!
<div class="HoverShow">
Now hover here to see another message!
<div class="HoverHidden">
another message!
<br />
Hey, wait... you shouldn't see that yet!
</div>
</div>
</div>
</div>
</div>
How can I adapt my CSS to make it work?
Please note that I have no rule concerning the depth of a HoverHidden under its closest HoverShow ancestor.
Your biggest issue with pure css is your final caveat of "Please note that I have no rule concerning the depth of a HoverHidden under its closest HoverShow ancestor." If it truly could be theoretically infinite, then no pure css solution exists. If there is some reasonable, practical limit for the level of nesting between HoverShow and HoverHidden, then you could do css like so (this allows up to 3 intermediate levels of nesting, so you can see how impractical this could get with too many more levels):
.HoverShow:hover > .HoverHidden,
.HoverShow:hover > :not(.HoverShow) > .HoverHidden,
.HoverShow:hover > :not(.HoverShow) > :not(.HoverShow) > .HoverHidden,
.HoverShow:hover > :not(.HoverShow) > :not(.HoverShow) > :not(.HoverShow) > .HoverHidden
{
display: initial;
}
See a fiddle example.
Please Note
A simple descendent .HoverShow:hover :not(.HoverShow) .HoverHidden will not work, as it will have positive hits on any descendent element that has no .HoverShow class, so as this fiddle shows, the third group does show on the hover of the second group because of the intervening nesting creating elements that do "not" have the .HoverShow class on them.
Use > , it only looks one level down the markup structure, no deeper.
.HoverShow:hover>.HoverHidden
{
display: initial;
}
In order to work this, you need to make a slight change in your markup
<div class="HoverShow">
Hover here to see message!
<div class="HoverHidden">
message!
<div class="HoverShow">
Now hover here to see another message!
<div class="HoverHidden">
another message!
<br />
Hey, wait... you shouldn't see that yet!
</div>
</div>
</div>
</div>
Fiddle Demo
Or you can try without changing the markup as follows
.HoverShow:hover >div>.HoverHidden,.HoverShow:hover >.HoverHidden
{
display: initial;
}
.HoverHidden
{
display: none;
}
Fiddle Demo
It can be done with the direct descendant selector:
.HoverHidden
{
display: none;
}
.HoverShow:hover > .HoverHidden{
display: initial;
}
WORKING Example with your original HTML: http://jsfiddle.net/8enBe/
WORKING Example with a deeper structure: http://jsfiddle.net/Eu93U/
important:
Do get this work, you need to keep the structure like
<div class="HoverShow">
<!-- next .HoverHidden descendant of this MUST BE a DIRECT descendant -->
<div class="HoverHidden">
<!-- next .HoverShow descendant of this MUST BE a DIRECT descendant -->
</div>
</div>
If you don't keep this structure, it won't work :)
Hope that helped.

Apply CSS/SASS rule to outer elements and not to inner elements

I'm currently working with a third party JS library that inserts content areas within the page/DOM, the library is Sir Trevor.
Now I wish to apply some custom CSS rules, for example:
.st-block:before {
#include roundedIcon(38px, $colorX, $colorY);
content: counter(mylistCounter, decimal);
counter-increment: mylistCounter;
margin-right: $margin-variable;
}
now this works great and a number is put before my divs with the .st-block class. However a DIV with this class can and some times does contain a child div with the same class, like so:
<div class="content">
<div id="st-block-16" class="st-block st-icon--add st-item-ready" data-type="listicle" data-instance="st-editor-8">
<!-- here's the child... grrr!!! -->
<div id="st-block-17" class="st-block st-icon--add st-item-ready" data-type="listicle" data-instance="st-editor-8">
Child Div Here...
</div>
</div>
<div id="st-block-18" class="st-block st-icon--add st-item-ready" data-type="listicle" data-instance="st-editor-8">
No Child Div
</div>
<div id="st-block-19" class="st-block st-icon--add st-item-ready" data-type="listicle" data-instance="st-editor-8">
No Child Div
</div>
</div>
How can I amend my CSS/SASS class to prevent the child/nested div with the same class being affected (in the example above the one with ID id="st-block-17")? PLEASE NOTE that I have no control over the alocation of IDs
Select only the classes which are only one level deeper then the div with class="content"
.content > .st-block

CSS immediate child selector

#main .container > div:not(.sites):not(.default) {
display: none;
}
The <h1> tag is visible while the below yui-ge div tag is hidden. If > only applies to immediate children how come my yui-ge is having the above CSS applied to it (both in Chrome and Firefox).
<div class='container'>
<div class='default selected'>
<h1>Page Title</h1>
<div class='yui-ge'> //for some reason, this tag remains hidden cause of the above CSS
//more div tags
</div>
</div>
//more HTML here
</div>
update
Look here: --LINK REMOVED--
Click the "Woot" tab.... no results are shown on the default woot tab - they remain hidden.
You have this CSS rule:
#main .woot > div:not(.sites):not(.default) {
display: none;
}
This rule applies to all DIVs inside the #main element, that do not have the classes sites or default and are children of a .woot element.
Your structure is:
<div id="main">
<div class="woot">
<div class="woot default selected">
<div class="yui-ge"> ... </div>
</div>
</div>
</div>
As you can see, the .yui-ge DIV does not have the class sites nor default and it is inside a .woot element. Therefore, it will be hidden.
The problem is that you have two DIVs in the ancestor chain that have the class woot.
If you hide an element, all it's child elements get hidden too.

Resources