Feeds:
Posts
Comments

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. If you don’t feel like reading the explanations that follow, you can download the example straight away.

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 or download the example from this link.

Enjoy the weekend!

Related Posts

Here’s a quick, dirty, reliable and library-independent client-side HTML template engine with support for prefetched templates (in a JavaScript object) and a few handy and extensible methods for compiling, adding and removing templates. Please, check the documentation in the source code and the examples at the bottom of this page for usage and more insight. You can also download the example from this link. Or you can check the demo first.

The Renderer JavaScript Class

/*
 * Renderer
 * HTML Template Engine
 * developer's website: http://wemakesites.net/#!/web-dev
 * developer's twitter: https://twitter.com/#!/wemakesitesnet
 * developer's blog http://acidmartin.wordpress.com/
 **/

(function() {
    "use strict";

    /*
     * @namespace window.AcidJs
     **/
    if(undefined === window.AcidJs) {
        window.AcidJs = {};
    }

    /*
     * @class Renderer
     * @constructor
     **/
    function Renderer() {}

    Renderer.prototype = {
        /*
         * @member TEMPLATES
         * @public
         * @info key/value storage of templates that will be used
         **/
        TEMPLATES: {
            "list": '<ul>{{items}}</ul>',
            "list-item": '<li>{{contents}}</li>',
            "names": '<span>first name: {{firstName}}</span><span>last name: {{lastName}}</span>'
        },

        /*
         * @method compile
         * @public
         * @param name (String) name of the template that will be used
         * @param data (Object) key/value pairs of template placeholders and values that should be replaced against these placeholders
         **/
        compile: function(name, data) {
            var
                html = this.TEMPLATES[name] || "";

            data = data || {};

            for (var key in data) {
                if (data.hasOwnProperty(key)) {
                    var
                        value = data[key],
                        regexp = new RegExp("{{" + key + "}}", "g");

                    html = html.replace(regexp, value);
                }
            }

            return html;
        },

        /*
         * @method add
         * @public
         * @param name (String)
         * @param html (String)
         * @param callback (Function) [optional]
         **/
        add: function(name, html, callback) {
            if(name && html) {
                this.TEMPLATES[name] = html;
                if(callback) {
                    callback.call(this);
                }
            }
        },

        /*
         * @method remove
         * @public
         * @param name (String)
         **/
        remove: function(name) {
            if(this.TEMPLATES[name]) {
                delete this.TEMPLATES[name];
            }
        }
    };

    window.AcidJs.Renderer = Renderer;
})();

Use Cases and Examples

(function() {
    // initialize Renderer
    window.templates = new AcidJs.Renderer();

    // compile and render some test template
    window.console.log(window.templates.compile("names", {firstName: "Martin", lastName: "Ivanov"}));

    // add template on the fly
    window.templates.add('date', '<div>{{month}} {{day}} {{year}}</div>');

    // compile and render the newly added template
    window.console.log(window.templates.compile("date", {day: 21, month: "June", year: 1976}));

    // add template on the fly and compile it immediately (callback is optional)
    window.templates.add('test-2', '<span><em>{{testValue}}</em> <strong>{{anotherTestValue}}</strong></span>', function() {
        window.console.log( window.templates.compile("test-2", {testValue: "Some value", anotherTestValue: "Another test value"}) );
    });

    // compile and render templates contents as function
    window.console.log(window.templates.compile("list", {items: function() {
            var
                guitars = ["B.C. Rich", "Jackson", "ESP", "Ibanez", "Gibson", "Fender"],
                html = [];

            for(var i = 0, len = guitars.length; i < len; i++) {
                var
                    guitar = guitars[i];

                html.push(window.templates.compile("list-item", {contents: guitar}));
            }

            html = html.join("");

            return html;
    }}));

    // remove the test template
    window.templates.remove("names");
})();

Supported Browsers

  • Mozilla Firefox
  • Google Chrome
  • Apple Safari
  • Opera
  • Internet Explorer 6-10

Please, follow @wemakesitesnet or check my website for more cool stuff. If you like this piece of code, you can also download it.

Related Posts

Acid.JS Validator is a free bookmarklet that uses the W3C SGML parser’s API to validate the markup of the page on which it is called. To start using the bookmarklet, go to this page and follow the instructions.

To validate a page, simply click the bookmark and you will be presented with a detailed validation report. In you are a chrome user, you can get and install the bookmarklet from Google Web Store.

Screenshots

Validation Success

validator-valid

Validation Failure

validator-invalid

Site Cannot be Checked

validator-uncheckable

Supported Browsers *

  • Mozilla Firefox
  • Google Chrome

* More browsers will be supported soon

To get your HTML markup validation bookmarklet, go to this page and follow the instructions.

Related

Ribbonbar UI is by far the best recreation of the “real” desktop ribbonbar for the web. It is skinnable, fully AJAX driven component that is ready to use on any website. What you can you do with a MS Office like ribbon bar in web environment? Whether use it for navigation, associate it with your WYSIWYG editor, use in an email application, display links to your favorite social websites or simply build your entire webpage around it – this depends only on you.

ribbon-bar-xml

Ribbonbar UI has everything you would expect from a ribbonbar – cool looks, comprehensive and easy API, amazing command handling, different sets of inbuilt tools, cool Windows7 skin. And at last, but not least – deploying and configuring it is a matter of minutes.

Key Features

  • By far, the most successful recreation of the “real” desktop ribbonbar as a web component.
  • Comprehensive documentation and fun to use API.
  • Easy to modify JSON structures for tabs, ribbons and commands.
  • Imageless CSS3 skin working with all modern browsers and falling back nicely for older browsers.
  • 4 types of inbuilt tools – “toolbar”, “office menu”, “split-button” and “external”.
  • Ability to load external tools by loading them via AJAX and passing get/post parameters.
  • Small footprint.
  • Easy command handling.
  • Can be used with 3-rd party WYSIWYG editors, as a navigation component, in email systems, etc.
  • The retrieval for the JSON files can be done with any server runtime (however Ribbonbar is shipped with PHP routine only).

Supported Browsers

  • Firefox
  • Chrome
  • Safari
  • Opera
  • Internet Explorer

Screenshots

ribbonbar-ui-4

ribbonbar-ui-3

ribbonbar-ui-2

ribbonbar-ui-1

Related

CSS still does not offer a solution for background image cropping out of the box, so here’s a technique that makes use of pseudo elements that I use extensively for this purpose. What I basically did – I created a square 16×16 pixels pseudo element box ::before the content of the element, assigned background image to it, and then, based on features of the href atribute of each link, I defined background coordinates, so the correct icon is displayed depending on the file type (extension):

The Markup:

<ul class="css3-background-image-cropping">
    <li><a href="some-file.md">some-file.md</a></li>
    <li><a href="some-file.pdf">some-file.pdf</a></li>
    <li><a href="some-file.txt">some-file.txt</a></li>
</ul>

The CSS:

:root .css3-background-image-cropping a::before
{
    content: "";
    display: inline-block;
    vertical-align: middle;
    margin: 0 4px 0 0;
    width: 16px;
    height: 16px;
    background: url("icons.png") no-repeat;
}

:root .css3-background-image-cropping a[href$=".md"]::before
{
    background-position: 0 -64px;
}

:root .css3-background-image-cropping a[href$=".pdf"]::before
{
    background-position: 0 -80px;
}

:root .css3-background-image-cropping a[href$=".txt"]::before
{
    background-position: 0 -96px;
}

The Icons Sprite

icons

And voilà! That’s the result:

css3-background-image-cropping

The Advantages

  • The texts in the boxes wrap safely and nicely without revealing unwanted regions of the sprite.
  • The images in the sprite are order- and size-independent.

The Disadvantages

  • Works only on CSS3-capable browsers.

If you like this post, you can try the demo or download the example, follow me on Twitter or check my website for more cool stuff! Happy new year!

Relates Posts

2012 in Review

The WordPress.com stats helper monkeys prepared a 2012 annual report for my blog.

Here’s an excerpt:

About 55,000 tourists visit Liechtenstein every year. This blog was viewed about 230,000 times in 2012. If it were Liechtenstein, it would take about 4 years for that many people to see it. Your blog had more visits than a small country in Europe!

Click here to see the complete report.

I’ve always been interested in techniques for adding custom look and feel to form elements, and through the years I’ve offered a number of such solutions. In today’s post I will share my latest insight in check box and radio button styling, which is an improved version of another technique I’ve offered sometime ago, but this time with a fallback for non-CSS3 browsers and more elegant looks and code.

css-pages-imageless-css3-custom-checkboxes-and-radio-buttons

The CSS

As you can see, I am using the CSS3 :root element, which is ignored by non-CSS3 browsers, so they will display the normal check boxes and radios. The code listing below contains only official CSS properties, so maybe it’s a good idea to download the cross-browser solution from this link.

:root .css3-radios label,
:root .css3-radios input[type="radio"] + span,
:root .css3-radios input[type="radio"] + span::before,
:root .css3-checkboxes label,
:root .css3-checkboxes input[type="checkbox"] + span,
:root .css3-checkboxes input[type="checkbox"] + span::before
{
    display: inline-block;
    vertical-align: middle;
}

:root .css3-radios,
:root .css3-checkboxes
{
    position: relative;
}

:root .css3-radios label *,
:root .css3-checkboxes label *
{
    cursor: pointer;
}

:root .css3-radios input[type="radio"],
:root .css3-checkboxes input[type="checkbox"]
{
    opacity: 0;
    position: absolute;
}

:root .css3-radios input[type="radio"] + span,
:root .css3-checkboxes input[type="checkbox"] + span
{
    font: normal 11px/14px Arial, Sans-serif;
    color: #333;
}

:root .css3-radios label:hover span::before,
:root .css3-checkboxes label:hover span::before
{
    box-shadow: 0 0 2px #ccc;
}

:root .css3-radios label:hover span,
:root .css3-checkboxes label:hover span
{
    color: #000;
}

:root .css3-radios input[type="radio"] + span::before,
:root .css3-checkboxes input[type="checkbox"] + span::before
{
    content: "";
    width: 12px;
    height: 12px;
    margin: 0 4px 0 0;
    border: solid 1px #a8a8a8;
    line-height: 14px;
    text-align: center;
    border-radius: 100%;
    background: #f6f6f6;
    background: radial-gradient(#f6f6f6, #dfdfdf);
}

:root .css3-radios input[type="radio"]:checked + span::before,
:root .css3-checkboxes input[type="checkbox"]:checked + span::before
{
    color: #666;
}

:root .css3-radios input[type="radio"]:disabled + span,
:root .css3-checkboxes input[type="checkbox"]:disabled + span
{
    cursor: default;
    opacity: .4;
}

:root .css3-checkboxes input[type="checkbox"] + span::before
{
    border-radius: 2px;
}

:root .css3-radios input[type="radio"]:checked + span::before
{
    content: "\2022";
    font-size: 24px;
}

:root .css3-checkboxes input[type="checkbox"]:checked + span::before
{
    content: "\2714";
    font-size: 12px;
}

The Markup

Nothing fancy here, just a regular semantic form without any additional markup, supporting natively keyboard navigation and accessibility intact.

<!-- markup for custom radio buttons -->
<ul class="css3-radios">
    <li><label><input type="radio" checked="checked" name="radios-01" /><span>checked radio button</span></label></li>
    <li><label><input type="radio" name="radios-01" /><span>unchecked radio button</span></label></li>
    <li><label><input type="radio" name="radios-01" disabled="disabled" /><span>disabled radio button</span></label></li>
</ul>
<!-- / markup for custom radio buttons -->
<!-- markup for custom check boxes -->
<ul class="css3-checkboxes">
    <li><label><input type="checkbox" checked="checked" name="checkbox-01" /><span>selected checkbox</span></label></li>
    <li><label><input type="checkbox" name="checkbox-02" /><span>unselected checkbox</span></label></li>
    <li><label><input type="checkbox" name="checkbox-03" disabled="disabled" /><span>disabled checkbox</span></label></li>
</ul>
<!-- / markup for custom check boxes -->

What do you Think?

If you like the solution, you can try the demo or get it from this link. In case you are interested in other stuff I do, you can follow me on Twitter, visit my personal website or check my Web UI components.

Have a great weekend!

Related Posts

Follow

Get every new post delivered to your Inbox.

Join 474 other followers

%d bloggers like this: