CSS Tabs

by Ajay Patel

HTML

<ul id="tabs">
    <li><a href="#" title="tab1">Twitter</a></li>
    <li><a href="#" title="tab2">Facebook</a></li>
    <li><a href="#" title="tab3">Youtube</a></li>
    <li><a href="#" title="tab4">All</a></li>
</ul>

<div id="content">
    <div id="tab1">A</div>
    <div id="tab2">B</div>
    <div id="tab3">C...</div>
    <div id="tab4">D...</div>
</div>

CSS

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

#tabs li{
  float: left;
  margin: 0 .5em 0 0;
  margin-left:5px;
}

#tabs a{
  position: relative;
  background: #4497B3;
  background-image: linear-gradient(to bottom, #fff, #4497B3);
  padding: .7em 3.5em;
  float: left;
  text-decoration: none;
  color: #fff;
  border-radius: 5px 0 0 0;
  box-shadow: 0 2px 2px rgba(0,0,0,.4);
}

#tabs a:hover,
#tabs a:hover::after,
#tabs a:focus,
#tabs a:focus::after{
  background: #6dc3e0;
  color:#fff;
}

#tabs a:focus{
  outline: 0;
}

#tabs a::after{
  content:'';
  position:absolute;
  z-index: 1;
  top: 0;
  right: -.5em;
  bottom: 0;
  width: 1em;
  background: #4497B3;
  background-image: linear-gradient(to bottom, #fff, #4497B3);
  box-shadow: 2px 2px 2px rgba(0,0,0,.4);
  transform: skew(10deg);
  border-radius: 0 5px 0 0;
}

#tabs #current a,
#tabs #current a::after{
  background: #fff;
  z-index: 3;
  color:#4497B3;
}

#content
{
    background: #fff;
    padding: 2em;
    height: 220px;
    position: relative;
    z-index: 2;
    border-radius: 0 5px 5px 5px;
    box-shadow: 0 -2px 3px -2px rgba(0, 0, 0, .5);
}

JavaScript

$(document).ready(function() {
        $("#content div").hide(); // Initially hide all content
        $("#tabs li:first").attr("id","current"); // Activate first tab
        $("#content div:first").fadeIn(); // Show first tab content

    $('#tabs a').click(function(e) {
        e.preventDefault();
        $("#content div").hide(); //Hide all content
        $("#tabs li").attr("id",""); //Reset id's
        $(this).parent().attr("id","current"); // Activate this
        $('#' + $(this).attr('title')).fadeIn(); // Show content for current tab
    });
})();