css selectors - Css - how to select the first of type on page -


this question has answer here:

most of pages have h2's , h1.

the h2's can inside divs/articles/sections etc - in other words there no guaranteed structure.

here's typical scenario:

<h1></h1> <section>     <h2>i want one</h2>     <p></p> </section> <section>     <h2>but not one</h2>     <p></p> </section> 

i want selector guarantees first occurance on page - in case first h2 wherever maybe.

i've taken , tried first-of-type selector found applies own container, every h2 selected here because first within parent section tag.

surely in 2015, able this.

use jquery. tried css methods, failed. excellent question btw.

<script> $(document).ready(function(){ $( "h2:first" ).css( "color", "red" ); });  </script> 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -