Css3 Nth of Type Restricted to Class

css3 nth of type restricted to class

Unfortunately, there is no way (at least none I know of) to select nth-of-type of a class, since nth-of-class doesnt exist. You will probably have to add a new class to every third .module manually.

how to use nth-of-type for classes -- not elements

With only CSS hacks, without modifying your markup, you can do something like the below:

[class*=' type-'], [type^='type-']{ /* Set all the divs to float: left initially */
float: left;
content: url('http://static.adzerk.net/Advertisers/db5df4870e4e4b6cbf42727fd434701a.jpg');
height: 100px; width: 100px;
}

.type-2 + .type-2 + div{
clear: both; /* Clear float for a div which follows two div with class type-2 */
}

.type-3 + .type-3 + .type-3 + div {
clear: both; /* Clear float for a div which follows three div with class type-3 */
}

.type-4 + .type-4 + .type-4 + .type-4 + div {
clear: both; /* /* Clear float for a div which follows four div with class type-4 */
}

Demo Fiddle

I need an explanation on how nth-of-type works

That is because nth-of-type starts with the first element of a certain type, in your case a div, and not the first element with the selected class

The :nth-of-type() pseudo-class represents an element that has an+b
siblings with the same expanded element name before it in the document
tree, for any zero or positive integer value of n, and has a parent
element.

More info:

  • https://www.w3.org/wiki/CSS/Selectors/pseudo-classes/:nth-of-type
  • https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-of-type

CSS step over or skip alternating div class :nth-of-type or :nth-child

This seems to work:

.custom-speakers>div:nth-child(4n+5){ clear:both; background:#ddd; border:1px solid #FFF;}
<!doctype html><html><head><meta charset="UTF-8"><title>STEP OVER EVERY MODAL NTH-CHILD</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

</head>
<body><div class="container"> <div class="row custom-speakers"> <div class="speaker col-sm-6"><h2>SPEAKER 1-2</h2></div> <div class="modal fade"></div> <div class="speaker col-sm-6"><h2>SPEAKER 2-2</h2></div> <div class="modal fade"></div> <div class="speaker col-sm-6"><h2>SPEAKER 3-2</h2></div> <div class="modal fade"></div> <div class="speaker col-sm-6"><h2>SPEAKER 4-2</h2></div> <div class="modal fade"></div> <div class="speaker col-sm-6"><h2>SPEAKER 5-2</h2></div> <div class="modal fade"></div> <div class="speaker col-sm-6"><h2>SPEAKER 6-2</h2></div> <div class="modal fade"></div> <div class="speaker col-sm-6"><h2>SPEAKER 7-2</h2></div> <div class="modal fade"></div> <div class="speaker col-sm-6"><h2>SPEAKER 8-2</h2></div> <div class="modal fade"></div> <div class="speaker col-sm-6"><h2>SPEAKER 9-2</h2></div> <div class="modal fade"></div> <div class="speaker col-sm-6"><h2>SPEAKER 10-2</h2></div> <div class="modal fade"></div> <div class="speaker col-sm-6"><h2>SPEAKER 11-2</h2></div> <div class="modal fade"></div> </div></div></body></html>

nth-of-type including other elements

This is a misunderstanding of :nth-of-type(). It's looking for the type of element (a div), that is also (odd), and only applying the styles to those that are also of class="box".

Your 5th div is a splitter, which is still counted even though it doesn't have class="box". It just doesn't have the styles applied because your selector specifies .box nth-of-type. Your next odd one is <div class="box">2</div> in between the splitters.

In case you haven't caught on yet, it's counting all the divs in your container. Just because you have an even number as text inside the .box divs, doesn't make them evenly numbered entries in your "list" of divs.

Since you said in a comment that you can't edit the markup, here is a jQuery implementation (thanks to BoltClock for help):

var container = document.getElementById("search-results");var descendants = container.getElementsByTagName("div");var x, i = 0;for (x = 0; x < descendants.length; x++) {    var nth = $(descendants).eq(x);    if (nth.hasClass("splitter")) {     i = 0;     continue;    }    if (nth.hasClass("box")) {        if (i % 2 == 0) {            nth.addClass("odd");        }        i++;    }};
.box {    width: 48%;    float: left;    height: 30px;    background: #ccc;}
.odd { margin-right: 4%; background-color: red;}
.splitter { width: 100%; float: left;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div id="search-results">    <div class="box">1</div>    <div class="box">2</div>    <div class="box">3</div>    <div class="box">4</div>    <div class="splitter">splitter</div>    <div class="box">1</div>    <div class="box">2</div>    <div class="box">3</div>    <div class="splitter">splitter</div>    <div class="box">1</div>    <div class="box">2</div>    <div class="box">3</div>    <div class="box">4</div>    <div class="box">5</div>    <div class="box">6</div>    <div class="box">7</div>    <div class="box">8</div></div>

nth-of-type doesn't work as expected

The nth-of-type pseudo selector is certainly working here as expected.

From MDN:

The :nth-of-type(an+b) CSS pseudo-class matches an element that has
an+b-1 siblings with the same element name before it in the document
tree

So

.test3:nth-of-type(3) {height: 100px;background: black;}

'works' because the div with class test3 also happens to be the third div within the container (div with class test)

however

.test3:nth-of-type(1) {height: 100px;background: black;}

doesn't select the test3 div because it isn't the first div in the container

Is nth-of-type work same as nth-child in this case?

In this case they are the same because all of the children in the container are divs.


In general, it could help to think about pseudo classes as a filter which matches only a subset of the matched selector (ie the part of the selector before the colon) when it is in the state that the pseudo class describes.

So for example .test:hover means: select all elements with class test - but only the ones which are being hovered.

Similarly here with nth-of-type: .test3:nth-of-type(3) means:

Select all elements with class test3, but only if it's the 3rd of that kind of element within it's container.

So take the following demo:

.test:nth-of-type(3) {  color:red;}
<div>  <ul>    <li class="test">li 1</li>    <li class="test">li 2</li>    <li class="test">li 3</li>    <li class="test">li 4</li>    <li class="test">li 5</li>  </ul>  <span class="test">span 1</span>  <span class="test">span 2</span>  <span class="test">span 3</span>  <span class="test">span 4</span>  <span class="test">span 5</span></div>

nth-of-type is not working

The CSS hasn't native tools how to do that. A little hackish code could be

.p .b ~ .b {background: red;} /* set properties to 2nd, 3rd, 4th, ... .b elements */.p .b ~ .b ~ .b {background: none;}  /* reset properties to 3rd, 4th, ... .b elements */
<div class="p">    <div class="a">The first paragraph.</div>    <div class="a b">The second paragraph.</div>    <div class="a b">The third paragraph.</div>    <div class="a">The fourth paragraph.</div></div>
<br>
<div class="p"> <div class="a">The first paragraph.</div> <div class="a b">The second paragraph.</div> <div class="a b">The third paragraph.</div> <div class="a b">The fourth paragraph.</div> <div class="a">The fifth paragraph.</div></div>

Can I combine :nth-child() or :nth-of-type() with an arbitrary selector?

This is a very common problem that arises due to a misunderstanding of how :nth-child(An+B) and :nth-of-type() work.

In Selectors Level 3, the :nth-child() pseudo-class counts elements among all of their siblings under the same parent. It does not count only the siblings that match the rest of the selector.

Similarly, the :nth-of-type() pseudo-class counts siblings sharing the same element type, which refers to the tag name in HTML, and not the rest of the selector.

This also means that if all the children of the same parent are of the same element type, for example in the case of a table body whose only children are tr elements or a list element whose only children are li elements, then :nth-child() and :nth-of-type() will behave identically, i.e. for every value of An+B, :nth-child(An+B) and :nth-of-type(An+B) will match the same set of elements.

In fact, all simple selectors in a given compound selector, including pseudo-classes such as :nth-child() and :not(), work independently of one another, rather than looking at the subset of elements that are matched by the rest of the selector.

This also implies that there is no notion of order among simple selectors within each individual compound selector1, which means for example the following two selectors are equivalent:

table.myClass tr.row:nth-child(odd)
table.myClass tr:nth-child(odd).row

Translated to English, they both mean:

Select any tr element that matches all of the following independent conditions:

  • it is an odd-numbered child of its parent;
  • it has the class "row"; and
  • it is a descendant of a table element that has the class "myClass".

(you'll notice my use of an unordered list here, just to drive the point home)

Selectors level 4 seeks to rectify this limitation by allowing :nth-child(An+B of S)2 to accept an arbitrary selector argument S, again due to how selectors operate independently of one another in a compound selector as dictated by the existing selector syntax. So in your case, it would look like this:

table.myClass tr:nth-child(odd of .row)

Of course, being a brand new proposal in a brand new specification, this probably won't see implementation until a few years down the road.

In the meantime, you'll have to use a script to filter elements and apply styles or extra class names accordingly. For example, the following is a common workaround using jQuery (assuming there is only one row group populated with tr elements within the table):

$('table.myClass').each(function() {
// Note that, confusingly, jQuery's filter pseudos are 0-indexed
// while CSS :nth-child() is 1-indexed
$('tr.row:even').addClass('odd');
});

With the corresponding CSS:

table.myClass tr.row.odd {
...
}

If you're using automated testing tools such as Selenium or scraping HTML with tools like BeautifulSoup, many of these tools allow XPath as an alternative:

//table[contains(concat(' ', @class, ' '), ' myClass ')]//tr[contains(concat(' ', @class, ' '), ' row ')][position() mod 2)=1]

Other solutions using different technologies are left as an exercise to the reader; this is just a brief, contrived example for illustration.


1 If you specify a type or universal selector, it must come first. This does not change how selectors fundamentally work, however; it's nothing more than a syntactic quirk.

2 This was originally proposed as :nth-match(), however because it still counts an element relative only to its siblings, and not to every other element that matches the given selector, it has since as of 2014 been repurposed as an extension to the existing :nth-child() instead.

nth-of-type not working unless html element changed

For the even .employee-group issue change the .employee-group:nth-of-type(odd) to

.employee-group:nth-of-type(4n+1)

Explaination: Your requirement wanted to skip every other item within a odd sequence i.e. instead of (1,3,5,7,9,11....) the properties should hit (1,5,9,13....) etc. To do that we gave the parameter a formula to calculate that so what the css does is takes the index (0,1,2,3) as n for each .employee-group element and calculates where to apply the properties based on formula result which goes as

(4n+1) = target number

(4x0+1)= 1

(4x1+1)= 5

(4x2+1)= 9

(4x3+1)= 13

...and so on.

That should solve your problem i believe.

Hope this helps.



Related Topics



Leave a reply



Submit