So I'm trying to apply a specific style to only the first class that occurs on the page with the class name of .one_third. I have three columns and three rows with this class, therefore having 9 of these classes altogether. I am trying to make only the first one, being the top left, black in background.
I was attempting to create it myself with Pseudo-classes, and I came up with this:
.one_third:first-child
Although that made all the classes on the far left black, instead of just the top left one.
I then tried this, to apply it against my content div that holds all the classes, yet that failed to do anything.
#content > .one_third:first-child
How could I achieve this?
EDIT:
This is my exact HTML markup:
For example,
<div class="jey">
<p>Number 1</p>
<p>Number 2</p>
<p>Number 3</p>
</div>
<div class="jey">
<p>Number 1</p>
<p>Number 2</p>
<p>Number 3</p>
</div>
Apply style
.jey:first-of-type{
background-color:Red;
}
See this http://jsfiddle.net/jeyashri_shri/Td8bx/
and this one also worked in your code
#content .one_third:first-child {
color:red;
}
But first-child to work in IE, a '<!DOCTYPE>' must be declared.
it work on IE7+ but not on IE6 or lower
Change:
<div class="one_third> //you forgot to add closing quote
to
<div class="one_third">
and
.one-third:first-child //class name is incorrect
to
.one_third:first-child
Fiddle here.
You can check any of the following, if one of them works for you:
.clear div.one_third:first-child{
color:red;
}
.columns div.one_third:first-child{
color:red;
}
As you have already used this: #content div.one_third:first-child{color:red;} that's why I put those. Check and let me know.
Related
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>
As we know, there's several CSS shortcuts (greater than, classes joined without spacing etc. etc.) but I'm wondering if we can state multiple parent CSS classes, while the children remain the same ?
For ease, I'm using a simple example...
Instead of:
.node-type-sponsors .container .page-header,
.node-type-supporters .container .page-header
{color:blue;}
Is there a shorter way of writing this by specifying the parent classes to be affected, but only listing the children once...
eg:
.node-type-sponsors,.node-type-supporters
[I assume something would go here to signify we've moved onto child elements]
.container .page-header
{color:blue;}
No it isnt possible with out sass/less etc
As an hack if you have exactly same scenario that you have a pattern matching in an class name either at start or end you can do something like, you can see for starting match attribute
[class^="node-type"] .container{}
Working Demo:
[class^="node-type"] .container{
color:red;
}
<div class="node-type-sponsors">
<p class="container ">abc</p>
</div>
<div class="node-type-supporters">
<p class="container">abc</p>
</div>
In this snippet, every <p> element that has a parent of div, or span with the specified classes got yellow background. You are looking for the > combinator.
.this-is-a-div, .this-is-a-span > p {
background-color: yellow;
}
<span class="this-is-a-span">
<p>Paragraph in a span.</p>
</span>
<div class="this-is-a-div">
<p>Paragraph 1 in the div.</p>
<p>Paragraph 2 in the div.</p>
</div>
<p>Paragraph 3. Not in a div.</p>
<p>Paragraph 4. Not in a div.</p>
You can read more about it here. Hope that helps.
I created a website that has different navigation menus. In 2 menus, I use the same HTML class element.
I have a .css file that styles that class element in 1 menu. However, in another menu, I would like to style the elements differently.
Yes, I know I can rename the class name, but to be consistent with what I have right now in the structure of my markup, and also the fact that the class name is used to style multiple other elements, how would I be able to apply different styles to 2 different elements with the same class name?
Can this be done using some kind of if statement condition in CSS?
For example, in 1.html:
<div class="classname"> Some code </div>
In 2.html:
<div class="classname"> Some different code </div>
Since I just want to style this "one" element differently in 2.html, can I just add an id attribute along with the class attribute, and use both the id and class and somehow as the selector?
Once again, I would not like to remove the class name at all, if possible.
Thanks!
I'll just add that typically when there are multiple menus you might have them wrapped in a different structure. Take for instance:
<nav class='mainnav'><div class="classname one"> Some code </div></nav>
<div class='wrapper'><div class="classname"> Some different code </div></div>
You can easily target these:
.mainnav>.classone {}
.wrapper>.classone {}
Or if the parent html has a class:
<div class='ancestor1'><div><div class="classname one"> Some code </div></div></div>
<div class='ancestor2'><div><div class="classname one"> Some code </div></div></div>
.ancestor1 .classname {}
.ancestor2 .classname {}
Obviously this depends on where in the html they might be.
You can add another class name to each element.
<div class="classname one"> Some code </div>
<div class="classname two"> Some different code </div>
And then aplpy different rules to them:
.classname.one {
border: 1px solid #00f;
}
.classname.two {
border: 1px solid #f00;
}
Edit:
Updated Demo link: http://jsfiddle.net/8C76m/2/
If you must keep only one class for each element, you may try the nth-child or nth-of-type pseudo-class:
.classname:first-child {
font-size: 2em;
}
.classname:nth-of-type(2) {
color: #f00;
}
Ref:
http://www.w3schools.com/cssref/sel_firstchild.asp and http://www.w3schools.com/cssref/sel_nth-of-type.asp
Just give each one a different id
#firsthtml .classname {
}
#sechtml .classname {
}
Be sure to use the space, as #firsthtml.classname is something totally different.
<div class="classname" id="firsthtml"></div>
<div class="classname" id="sechtml"></div>
You could also use two different class names
<div class="classname secondclassname"></div>
Define secondclassname in your css with the additional css
.classname.secondclassname{
}
You can also do something like this:
<div class="classname"> Some code </div>
<div class="classname second"> Some different code </div>
And the CSS for the first .classname would be something like that:
.classname:not(.second) {}
For the second element it goes easily:
.classname.second {}
I know this is a poor way of doing it, the suggestions from previous answers are helpful, but try this maybe:
First menu:
<div class="classname"> Some code </div>
Second menu:
<div class="classname" style="margin-bottom:0;color:Black;width:100px;height:100px"> Some other code </div>
How am I abled to select the first, second and third span related to layer 1, independent from if it is the first child of a div at layer 2?
<div class="LAYER1">
<div class="LAYER2">
<span>FIRST<span>
</div>
<div class="LAYER2">
<span>SECOND</span>
<span>THIRD</span>
</div>
</div>
By using span:first-child I get FIRST and SECOND but I just want to get FIRST.
EDIT: I'm aiming for making a selection like "get me the first, second and third span children from LAYER1". SECOND and THIRD could be in the first LAYER2 div, too and the solution should not depend on that.
EDIT2: Example at http://jsfiddle.net/3hAEc/3/
Selecting only <span>FIRST</span> works like this (assuming the browser supports it)
.LAYER1 .LAYER2:first-child span {
// ...
}
As you can see here: http://jsfiddle.net/3hAEc/
how about you add ids or classes to them, then select them that way ?
<div class="LAYER1">
<div class="LAYER2">
<span id="first_span">FIRST<span>
</div>
<div class="LAYER2">
<span id="second_span">SECOND</span>
<span id="third_span">THIRD</span>
</div>
</div>
Try this :
.LAYER1 .LAYER2:first-child span:first-child /* first one*/
.LAYER1 .LAYER2+.LAYER2 span:first-child /* second one*/
.LAYER1 .LAYER2+.LAYER2 span:first-child+span /* third one*/
But this is only applicable for the current DOM.
Finally I used the following JS solution by utilizing jQuery:
// for the first span, use 0 and increase for further children
$(".LAYER1").find("span").eq(0);
<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...
}