Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS first child of specific class

Tags:

css

I have the following piece of HTML code:

<span class="ui-icon ui-icon-triangle-1-s"></span>
<span class="route-line">4</span>
<span class="route-line">33</span>

I would like to add some style on the first span with class="route-line". Is it possible with CSS? It is something like nth-of-type; if it had existed, it would have been called nth-of-type-with-class.

Thank you!

like image 557
linkyndy Avatar asked Jul 04 '12 21:07

linkyndy


4 Answers

In such cases you can use the sibling selector. Like so:

.ui-icon + .route-line{
    color: red;
}

It will style only the span which follows the element with a class ui-icon.

like image 71
skip405 Avatar answered Nov 08 '22 02:11

skip405


First span with class .route-line followed for any other element that don't have class .route-line

*:not(.route-line) + span.route-line
{
    background-color:yellow;
}
like image 36
Leo Avatar answered Nov 08 '22 01:11

Leo


Why don't you apply another class to the element you want to style.

<span class="ui-icon ui-icon-triangle-1-s"></span>
<span class="route-line custom-route-line">4</span>
<span class="route-line">33</span>

And the css would look like this

.custom-route-line{/*your styling goes here*/}

Note both styles of classes (route-line & custom-route-line) will apply to that element. Take a look here

like image 2
menislici Avatar answered Nov 08 '22 01:11

menislici


What you were looking for is span.route-line:first-of-type, but it's poorly supported.

like image 1
pozs Avatar answered Nov 08 '22 03:11

pozs