Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test for nth-child using Modernizr?

I'm trying to use modernizr to test for :nth-child browser support but I'm not sure how to do it, I found this one http://jsfiddle.net/laustdeleuran/3rEVe/ which tests for :last-child but I don't know how to change it to detect :nth-child (I was also thinking about using it like that since I believe that browsers that don't support :last-child don't support :nth-child either but I'm not sure)

Can you guys help me? Thanks in advance!

like image 502
Javier Villanueva Avatar asked Oct 03 '11 01:10

Javier Villanueva


People also ask

How do I know my nth child?

Definition and Usage. The :nth-child(n) selector matches every element that is the nth child of its parent. n can be a number, a keyword (odd or even), or a formula (like an + b). Tip: Look at the :nth-of-type() selector to select the element that is the nth child, of the same type (tag name), of its parent.

What does P Nth child 2n 2 do?

As per W3Schools CSS Selector Reference: p:nth-child(2) : Selects every <p> element that is the second child of its parent. p:nth-of-type(2) : Selects every <p> element that is the second <p> element of its parent.

How does modernizr detect browser?

We can access various properties of this object 'Modernizr' for feature detection using “Modernizr. featureName”. For example, Modernizr. video will return “true” if the browser supports the video element, and false if the browser doesn't.

How do you target a third element in CSS?

If you look back to the earlier example nth-child(3n+1) , the CSS would be applied to every third element, initially offset by one. So for p:nth-child(3n + 1) the first p tag would be affected and then every third tag.


2 Answers

I just wrote a function to detect the :nth-child support for you

function isNthChildSupported(){
var result = false,
    test =  document.createElement('ul'),
    style = document.createElement('style');
test.setAttribute('id', 'nth-child-test');
style.setAttribute('type', 'text/css');
style.setAttribute('rel', 'stylesheet');
style.setAttribute('id', 'nth-child-test-style');
style.innerHTML = "#nth-child-test li:nth-child(even){height:10px;}";
for(var i=0; i<3; i++){
    test.appendChild(document.createElement('li'));   
}
document.body.appendChild(test);
document.head.appendChild(style);
  if(document.getElementById('nth-child-test').getElementsByTagName('li')[1].offsetHeight == 10) {result = true;}
document.body.removeChild(document.getElementById('nth-child-test'));
document.head.removeChild(document.getElementById('nth-child-test-style'));
  return result;
}

Usage:

isNthChildSupported() ? console.log('yes nth child is supported') : console.log('no nth child is NOT supported');

You can see this works in action here http://jsbin.com/epuxus/15

Also There is a difference between jQuery :nth-child and CSS :nth-child.

jQuery :nth-child is supported in any browser jQuery supports but CSS :nth-child is supported in IE9, Chrome, Safari and Firefox

like image 84
Mohsen Avatar answered Oct 11 '22 14:10

Mohsen


I remember there was a Modernizr selectors plugin that tested for selectors support, but I can't find it right now. You can take a look at this: http://javascript.nwbox.com/CSSSupport/ which is similar.

like image 27
Lea Verou Avatar answered Oct 11 '22 14:10

Lea Verou