Google Play’s minimal tabs with CSS3 & jQuery

The tab navigation is an element you often meet in your daily browsing. There are so many ways, so many styles, but the idea is the same: you click a tab and see its content without a page refresh.

In this article you’ll learn how to build some new CSS3 & jQuery tabs inspired by Google Play‘s design.

View demo

Before going further, you may want to check the other tabs tutorials I wrote:

CSS3 tabs with beveled corners
CSS3 tabs with beveled corners

CSS3 & jQuery folder tabs
CSS3 & jQuery folder tabs

The HTML

Getting back to this tutorial, here’s the markup, simple as usual. You may notice the similarity between an anchor’s name attribute and related content’s id block. Further, this will be our hook.

<ul id="tabs">
    <li><a href="#" name="#tab1">One</a></li>
    <li><a href="#" name="#tab2">Two</a></li>
    <li><a href="#" name="#tab3">Three</a></li>
    <li><a href="#" name="#tab4">Four</a></li>    
</ul>

<div id="content"> 
    <div id="tab1">...</div>
    <div id="tab2">...</div>
    <div id="tab3">...</div>
    <div id="tab4">...</div>
</div>

The CSS

The goal here, as always, is to do it without using any images and with as little CSS as possible.

That’s all, this is not just a CSS piece, it’s the whole CSS used to create these tabs. I think it’s pretty cool, not even pseudo-elements were used here, just CSS borders.

#tabs {
  overflow: hidden;
  width: 100%;
  margin: 0;
  padding: 0;
  list-style: none;
}

#tabs li {
  float: left;
  margin: 0 -15px 0 0;
}

#tabs a {
  float: left;
  position: relative;
  padding: 0 40px;
  height: 0; 
  line-height: 30px;
  text-transform: uppercase;
  text-decoration: none;
  color: #fff;
  border-right: 30px solid transparent;
  border-bottom: 30px solid #3D3D3D;
  border-bottom-color: #777\9;
  opacity: .3;
  filter: alpha(opacity=30);	  
}

#tabs a:hover,
#tabs a:focus {
  border-bottom-color: #2ac7e1;
  opacity: 1;
  filter: alpha(opacity=100);
}

#tabs a:focus {
  outline: 0;
}

#tabs #current {
  z-index: 3;
  border-bottom-color: #3d3d3d;
  opacity: 1;
  filter: alpha(opacity=100);	  
}

Deconstructing it

Here’s where the magic happens:

#tabs a {
  height: 0; 
  line-height: 30px;
  border-right: 30px solid transparent;
  border-bottom: 30px solid #3D3D3D;	  
}

Styles excerpt


CSS border tabs technique example

The jQuery

Comparing to my previous tabs article, this time I think I improved a bit the jQuery code. Also, this time you have the possibility to access the tabs directly by URL, e.g. mywebsite.com/tabs.html#tab2.

function resetTabs(){
    $("#content > div").hide(); //Hide all content
    $("#tabs a").attr("id",""); //Reset id's      
}

var myUrl = window.location.href; //get URL
var myUrlTab = myUrl.substring(myUrl.indexOf("#")); // For mywebsite.com/tabs.html#tab2, myUrlTab = #tab2     
var myUrlTabName = myUrlTab.substring(0,4); // For the above example, myUrlTabName = #tab

(function(){
    $("#content > div").hide(); // Initially hide all content
    $("#tabs li:first a").attr("id","current"); // Activate first tab
    $("#content > div:first").fadeIn(); // Show first tab content
    
    $("#tabs a").on("click",function(e) {
        e.preventDefault();
        if ($(this).attr("id") == "current"){ //detection for current tab
         return       
        }
        else{             
        resetTabs();
        $(this).attr("id","current"); // Activate this
        $($(this).attr('name')).fadeIn(); // Show content for current tab
        }
    });

    for (i = 1; i <= $("#tabs li").length; i++) {
      if (myUrlTab == myUrlTabName + i) {
          resetTabs();
          $("a[name='"+myUrlTab+"']").attr("id","current"); // Activate url tab
          $(myUrlTab).fadeIn(); // Show url tab content        
      }
    }
})()

View demo

In the end...

This technique has a small drawback, the tabs do not behave as they should if you're using IE6. But, with your permission, I'd say we could skip IE6 this time. However, if you really want a graceful degradation for IE6, it can be done with some specific targeting. Maybe I'll do that in the future, or maybe not :)

I'm looking forward to read your comments, hope you enjoyed this. Thanks for reading it!

56 thoughts on “Google Play’s minimal tabs with CSS3 & jQuery

  1. Inspiring as always, thanks! ;D
    Skipping IE6 isn’t a big deal in my eyes, this browser is dead anyway.

  2. Constructive criticism: You’re JavaScript could use a bit of refactoring for performance, scalability, and overall cleanliness.

    Otherwise, nice tutorial :)

  3. Question: why do you use the “name” attribute to identify the anchor-ed element in place of the href attribute ?
    I mean, why is it like this
    One
    and not like this
    One
    ?
    Couldn’t you assign “current” to the className rather than the id?

    Edit:
    sorry, html code stripped out by the text editor; hope my question still makes sense…

    • Comparing to href, using name attribute helps preventing “back navigation” issue for the tabs.

      Also, the current id was my choice, you can always use a class instead.

  4. Hi Catalin

    As you mention in your article tabs are becoming more and more widely used to layout large amounts of content. As a result there are numerous blog posts and tutorials on the subject. I like the methods used in your tutorial and the emphasis on css as a posed to images. I will defiantly be using this technique in the near future.

  5. Hello! There is a problem when you have DIV inside tab content element. This DIV is hidden all the time – I suggest to change “content div” to “content > div” in JQ scprit – now you can use DIV element inside tabs content. It’s tested on FF 13 and works fine.

    Great job Catalin Rosu!
    Thanks!

  6. These CSS 3 JQuery Tabs look very great. I love how these Tabs look in Google Play website and I am glad that I have learned how to create such elegantly designed Tabs.

  7. I noticed in Chrome when put these tabs in the middle of a page and you click on a tab… it takes you back to the top of the page – rather than switch tabs in the same scroll position. Anyone have this issue – anyone have the solution?

  8. Hi, thanks for this tutorial. I notice the tabs jump when the page first loads up. i.e. all the content loads as one tab for a split second then forms into tabs. Do you know how I could stop the jumping?

    Thanks

    • That’s because the content is hidden with jQuery: $("#content > div").hide(); and this code is executed after the HTML loads.

      If you know which tab/content to show initially, maybe you could hide the other content using CSS instead. This way you will prevent the “jumping”.

      Good luck!

  9. Great post, great jsfiddle, great plugin.
    We are using it in a repository, awesome.
    Just to let you know, we had problems with on() jQuery event handler, so we managed it with click() directly (it should be used only on old jquery versions, prev to 1.8 I suppose, dunno remember right now):
    $tabs.find(‘a’).click(‘.myTabs’, base.handleTabClick);
    instead of
    $tabs.find(‘a’).on(‘click.myTabs’, base.handleTabClick);

  10. Sir,
    I have more doubts,
    1. why we use ‘resetTabs()’ fuction, coz in further coding we r doing the same job no?

    2. Can we do this in JS? I want to try coz am new to jQuery it looks hard?

    3. Can we Active/Deactive a class or id of a element at event function?

    am a newbie to tis. thanx in adv…..

  11. How can I place Disqus comments inside a tab? It doesn’t work. The code automatically hides Diqsus comments (inside , for example)
    Help me, please))

  12. Hey! Really good tutorials. I have a question though.
    Is there a way to update the url ( in adress bar ) with an hash tag when you click on a tab. I would like to use this method for a nav. When you click on a button, it loads the content from a div and then modify the url

    ie:
    One

    and would produce something like http://www.yourwebsite.com/#tab1

  13. I’m rather new to a lot of coding things – this is an area that I’m trying to learn, so forgive me if I’m being a noob.

    I’ve gotten everything loaded in properly and it works. I do however use a few other JS scripts in my site as well as that specific page that I’m working on (image script and poshytip). When I get yours working right, these no longer work and vice versa. Any ideas?

    Awesome work none the less – I’ve learned a lot just from this one tutorial and I thank you for it :)

  14. Is there a reason why i can’t click between tabs after changing the ul id to “tab” and replacing everything that is “tabs” rewritten to “tab”? Solutions are much appreciated :)

      • Will do. With that said, do you know what I may have done wrong? The only thing I did was to rename all the “tabs” in .css and .js to “tab” so that it will not affect other functions in my website. “#tab1″ is clickable and cursor is available on hover over but “#tab2″ is totally not working at all.

        • Joe,

          I think it’s a copy/paste problem there. Maybe you should consider choosing a safer name like: “minimal-tabs” or something like that in order to avoid any interactions.

          Be careful with namings when making replacements because I don’t see any reason why it shouldn’t be fine.

          Good luck and feel free to send me jsFiddle link once you’ll do the updates.

  15. Hi, I saw that you said that it is now possible to access the tabs by URL, but for me it doesn’t work… Did I do something wrong or it isn’t supposed to work ?

  16. Nice tutorial but I have a problem that I need help with solving:

    I did exacley as the tutorial said but my menu is small, and I want to increase it (make it bigger) its not like the one in the demo, its a smaller version of it.

    Thanks for the tutorial though, really nice work! :)

  17. I get everything to work, but when I press Home for example, its not in a black color so I know which site Im in. If I press Home on ur demo it turns black so I know that im on that page but I cant get that to work on mine.. Could u help me out?

  18. Hey, awesome tutorial here, this tutorial in combination with the JS in the comments was great. However, I am facing a problem now: I want to have two of these tab divs in the same document, how do I go about that. In the current state, only the second one is working, any idea why? Could it be because the site caches the variables, and the variable names are the same, meaning I have to change every single variable name as well?

    Furthermore, I am at the moment using two different scripts, because the references go to different divs (classes don’t work since that would trigger both divs to change tab). However, I would love it if someone could answer me if it’s possible to make it all into one javascript in a neat and tidy way.

  19. Never mind, that doesn’t work either; since the classes are the same, it will hide both all tabs except for the active one, meaning only one active tab/page and not one active tab/list as I’m wanting. Trying to use .parent and .closest now to find a nicer more universal solution for an infinite amount of tabbed lists!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>