August 4, 2015 10:49 am

Useful HTML5 Features, Part 5

CSS-like JavaScript selectors HTML5 brought us CSS-like JavaScript selectors and they have a pretty decent support. This is really handy and was one of the main benefits of the jQuery library. The lengthy JavaScript selectors such asdocument.getElementsByClassName("container")[0].getElementsByTagName("p")[0] are on the way to history.
Useful HTML5 Features

[wpdm_file id=145]DEMO

Browser support

The following browsers support the querySelector property:

  1. IE8 has partial support
  2. IE9+ supports it fully.
  3. Firefox 31+
  4. Chrome 31+
  5. Safari 7+
  6. Opera 29+

Generally, it is supported by 96.92% of the Internet users out there so you can decide to use it without any fallbacks (statistics can be found in Can I Use).


First, we add some basic markup:

<div class="container">
        <p>Retrieve me!</p>
        <p>Get it!</p>

We can use querySelector and querySelectorAll. querySelector returns the first element that matches the given string with a CSS selector while querySelectorAll returns us all elements that match the CSS selector.

var gotIt = document.querySelector(".container p");
    //logs the first found paragraph - this is Retrieve me!

If we did not use querySelector and wanted to get that paragraph (which does not have a class on its own) we would have to type something like: document.getElementsByClassName("container")[0].getElementsByTagName("p")[0]… What a relief!

If we want to get both (all) paragraphs with the div with class of container we could use:

var gotThem = document.querySelectorAll(".container div p");
    //logs both paragraphs

Cool things you can do with CSS3 attribute selectors

We are going to show you how you can easily add an image or text to a link indicating that it is a .pdf, .doc file or whatever you like. We are also going to show how you can signal the users that a particular link will move them away from your website.

In our markup, we have a div with links:

<div class="random-links">
    <a href="1.pdf">A book on the Evolution</a>
    <a href="2.doc">A brief history of mankind</a>
    <a href="doc3.pdf">Are Dinosaurs Real?</a>
    <a href="pdf4.doc">What do men know about women
    <a href="pdf4.docx">What do men know about women
    <a href="">PHPGang</a>
        <a href="/folder/file.html"></a>

Firstly, we will add a .pdf icon to the links that lead to a .pdf file:

a[href$='.pdf']:after {
            content: url("pdf.png");
            padding: 5px;
            padding-bottom: 15px;

a[href$='.pdf'] is instructing the browser to search for anchors which have a href attribute whose value ends with .pdf. $= followed by text searches the attribute for a value that ends with the provided text.

We have used the :after pseudo-element and its feature content to add an image to the end of each matched anchor.

Let us repeat the process with the files that end with .doc:

a[href$='.doc']:after {
            content: url("word.png");
            padding: 5px;
            padding-bottom: 15px;


If you see the links now you will see that <a href="pdf4.docx">What do men know about women</a> does not have a Microsoft Word icon. This is because it does not end with .doc, it ends with .docx.

Now, to add an external icon to the websites pointing to other domains we would have to use another attribute selector that seeks the beginning of an attribute for a certain value: ^=.

a[href^='http']:before {
            content: url("external.png");
            padding: 5px;
            padding-bottom: 15px;

This searches for any anchor with a href attribute that starts with http (it could be http:// or https://). Notice also that this time we insert the icon before the content of the anchor itself with the :before pseudo-element.

Here is what we end up with:

screenshot html5 feature 5


I hope you found the today’s piece of HTML5 useful indeed. If you found this article interesting or you have questions, do not hesitate to write a comment. I will be glad to reply to any enquiries.

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: facebook, twitter

Tutorial Categories:

One response to “Useful HTML5 Features, Part 5”

  1. Rui says:

    Great article

Leave a Reply

Your email address will not be published. Required fields are marked *