Fully Functional CSS3-only Tabstrip Without any JavaScript

Try Semtex – the new HTML5, CSS3 and JavaScript UI framework!

The code for today is a fully functional CSS3-only tabstrip, which works without a single line of JavaScript and natively supports keyboard arrow navigation.

css3-tabstrip

The CSS

In short – we have a list with radio buttons, <label />s and <div />s. We hide each <div />, which is the content of the tab according to the checked state of the radio button. The <label /> is used as the button of the tab.

/*
 * @CSS3 Tabstrip
 * @author Martin Ivanov
 * @website http://wemakesites.net
 * @blog http://acidmartin.wordpress.com/
 * @twitter https://twitter.com/wemakesitesnet
 **/

.css3-tabstrip ul,
.css3-tabstrip li
{
    margin: 0;
    padding: 0;
    list-style: none;
}

.css3-tabstrip,
.css3-tabstrip input[type="radio"]:checked + label
{
    position: relative;
}

.css3-tabstrip li,
.css3-tabstrip input[type="radio"] + label
{
    display: inline-block;
}

.css3-tabstrip li > div,
.css3-tabstrip input[type="radio"]
{
    position: absolute;
}

.css3-tabstrip li > div,
.css3-tabstrip input[type="radio"] + label
{
    border: solid 1px #ccc;
}

.css3-tabstrip
{
    font: normal 11px Arial, Sans-serif;
    color: #404040;
}

.css3-tabstrip li
{
    vertical-align: top;
}

.css3-tabstrip li:first-child
{
    margin-left: 8px;
}

.css3-tabstrip li > div
{
    top: 33px;
    bottom: 0;
    left: 0;
    width: 100%;
    padding: 8px;
    overflow: auto;
    background: #fff;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

.css3-tabstrip input[type="radio"] + label
{
    margin: 0 2px 0 0;
    padding: 0 18px;
    line-height: 32px;
    background: #f1f1f1;
    text-align: center;
    border-radius: 5px 5px 0 0;
    cursor: pointer;
    -moz-user-select: none;
    -webkit-user-select: none;
    user-select: none;
}

.css3-tabstrip input[type="radio"]:checked + label
{
    z-index: 1;
    background: #fff;
    border-bottom-color: #fff;
    cursor: default;
}

.css3-tabstrip input[type="radio"]
{
    opacity: 0;
}

.css3-tabstrip input[type="radio"] ~ div
{
    display: none;
}

.css3-tabstrip input[type="radio"]:checked:not(:disabled) ~ div
{
    display: block;
}

.css3-tabstrip input[type="radio"]:disabled + label
{
    opacity: .5;
    cursor: no-drop;
}

The Markup

In the example below, the first tab is selected by default, because the radio button, associated with it has the checked attribute. You can set arbitrary tab to selected by default like that.

<div class="css3-tabstrip">
    <ul>
        <li>
            <input type="radio" name="css3-tabstrip-0" checked="checked" id="css3-tabstrip-0-0" /><label for="css3-tabstrip-0-0">Home</label>
            <div>
                <h3>What is Lorem Ipsum</h3>
                <p>Lorem Ipsum is simply dummy text of the printing and
                    typesetting industry. Lorem Ipsum has been the industry's
                    standard dummy text ever since the 1500s, when an unknown
                    printer took a galley of type and scrambled it to make a
                    type specimen book. It has survived not only five centuries,
                    but also the leap into electronic typesetting, remaining
                    essentially unchanged. It was popularised in the 1960s
                    with the release of Letraset sheets containing Lorem
                    Ipsum passages, and more recently with desktop publishing
                    software like Aldus PageMaker including
                    versions of Lorem Ipsum.</p>
            </div>
        </li><li>
            <input type="radio" name="css3-tabstrip-0" id="css3-tabstrip-0-1" /><label for="css3-tabstrip-0-1">About Us</label>
            <div>
                <h3>Why do we use it?</h3>
                <p>It is a long established fact that a reader will be distracted
                    by the readable content of a page when looking at its layout.
                    The point of using Lorem Ipsum is that it has a more-or-less
                    normal distribution of letters, as opposed to using 'Content
                    here, content here', making it look like readable English.
                    Many desktop publishing packages and web page editors
                    now use Lorem Ipsum as their default model text, and a
                    search for 'lorem ipsum' will uncover many web sites still
                    in their infancy. Various versions have evolved over the
                    years, sometimes by accident, sometimes on purpose
                    (injected humour and the like).</p>
            </div>
        </li><li>
            <input type="radio" name="css3-tabstrip-0" id="css3-tabstrip-0-2" /><label for="css3-tabstrip-0-2">Portfolio</label>
            <div>
                <h3>Where does it come from?</h3>
                <p>Contrary to popular belief, Lorem Ipsum is not simply
                    random text. It has roots in a piece of classical
                    Latin literature from 45 BC, making it over 2000 years
                    old. Richard McClintock, a Latin professor at Hampden-Sydney
                    College in Virginia, looked up one of the more obscure
                    Latin words, consectetur, from a Lorem Ipsum passage,
                    and going through the cites of the word in classical
                    literature, discovered the undoubtable source. Lorem
                    Ipsum comes from sections 1.10.32 and 1.10.33 of "de
                    Finibus Bonorum et Malorum" (The Extremes of Good and Evil)
                    by Cicero, written in 45 BC. This book is a treatise on
                    the theory of ethics, very popular during the Renaissance.
                    The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..",
                    comes from a line in section 1.10.32.</p>
            </div>
        </li><li>
            <input type="radio" name="css3-tabstrip-0" id="css3-tabstrip-0-3" disabled="disabled" /><label for="css3-tabstrip-0-3">Contact Us</label>
            <div>
                <h3>Where can I get some?</h3>
                <p>There are many variations of passages of Lorem Ipsum available,
                    but the majority have suffered alteration in some form,
                    by injected humour, or randomised words which don't look
                    even slightly believable. If you are going to use a
                    passage of Lorem Ipsum, you need to be sure there isn't
                    anything embarrassing hidden in the middle of text. All
                    the Lorem Ipsum generators on the Internet tend to
                    repeat predefined chunks as necessary, making this the
                    first true generator on the Internet. It uses a dictionary
                    of over 200 Latin words, combined with a handful of model
                    sentence structures, to generate Lorem Ipsum which looks
                    reasonable. The generated Lorem Ipsum is therefore
                    always free from repetition, injected humour, or
                    non-characteristic words etc.</p>
            </div>
        </li>
    </ul>
</div>

Finishing Touches

What’s left is to set width and height to the tabstrip. This should be done on the outermost element. If you have different tabstrips on the same page, you can use it’s, secondary classes, etc. In the example page, we have just one tabstrip, so we will use the base class:

.css3-tabstrip
{
    width: 440px;
    height: 220px;
}

Supported Browsers

  • Mozilla Firefox
  • Google Chrome
  • Apple Safari
  • Opera
  • Internet Explorer 9 and above

If you like this post, maybe you will follow me on Twitter or visit my personal website for other cool stuff. You can also try out the demo.

Enjoy the weekend!

Related Posts

About these ads

22 comments

  1. Hi.. What if we have forms with radio buttons in the tab content? will those get effected by this css or is there any class which can skip for content form with radio buttons?

  2. Hi,
    Firstly thank you for the tutorial!
    I have a small issue, lets say i have two tabs (communication and feedback)
    Ive set the font color to black and the background of the two tabs to a light shade of blue.
    How would i change the color of the text to white, according to the tab that ive clicked on.
    For example if i click on the communication tab i want the text to be white but the feedback text to remain black.

    Regards.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s