Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE8: what to use in place of nth-of-type(n)?

I've inherited the following CSS code to initially hide the latter elements of a series of paragraphs and a series of list items.

.profileSection p:nth-of-type(n+2) {
    display: none;
}

.profileSection li:nth-of-type(n+6) {
    display: none;
}

Obviously, this code does not work in IE8. What is an alternate way to hide these elements?

like image 580
dmr Avatar asked Dec 11 '12 21:12

dmr


People also ask

What is nth child() in CSS?

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. Version: CSS3.

What does nth of type mean?

The :nth-of-type selector allows you select one or more elements based on their source order, according to a formula. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling elements.

Can you use nth of type with a class?

There cannot be a way to select :nth-of-type() of a class, because :nth-of-type() only selects the nth child of its type.


2 Answers

Here is a discussion on it:

http://www.thebrightlines.com/2010/01/04/alternative-for-nth-of-type-and-nth-child/

The writer mentions that you can reference specific children element by using

tagname + tagname + etc

Or get generic children by using

* + * + etc

I personally would just add a special class to those items.

like image 152
Pow-Ian Avatar answered Oct 04 '22 14:10

Pow-Ian


+, the adjacent sibling selector, would allow you to select all siblings which are immediately adjacent. In your case: .profileSection p+p. (If you must do this, consider wrapping it in something to prevent other browsers from seeing it, like conditional comments.)

But + won't help if your markup contains something other than <p> elements right next to each other. For example:

<p>Alpha</p>
<h4>Header</h4>
<p>Beta</p>

If you don't already have some kind of shiv or moderizr functionality on the site (which would help with many other similar issues), it would be easiest to add a special class to the elements, and select using that class.

like image 30
KatieK Avatar answered Oct 04 '22 13:10

KatieK