How do I make my HTML5 nav li tab stay active on click with CSS3?

被刻印的时光 ゝ 提交于 2019-12-04 22:24:27

:active only applies to anchors (<a>) and buttons (<button>, <input type="button/>...) and only while they are pressed.

You need to take a look at :target

http://jsfiddle.net/coma/ED6cH/6/

HTML

<div class="tabbed">
    <a href="#dog" id="dog">
        Dog
        <div>
            <p>This is a dog...</p>
        </div>
    </a>
    <a href="#cat" id="cat">
        Cat
        <div>
            <p>This is a cat...</p>
        </div>
    </a>
    <a href="#foo" id="foo">
        Foo
        <div>
            <p>This is a foo...</p>
        </div>
    </a>
</div>

CSS

div.tabbed {
    position: relative;
    font-size: 0;
}

div.tabbed > a {
    display: inline-block;
    padding: .5em;
    font-size: 16px;
    border-radius: 3px 3px 0 0;
    background-color: #333;
    color: #eee;
    text-decoration: none;
    line-height: 1em;
}

div.tabbed > a + a {
    margin-left: .5em;
}

div.tabbed > a:target {
    color: #333;
    background-color: #eee;
}

div.tabbed > a > div {
    position: absolute;
    top: 100%;
    left: 0;
    width: 300px;
    padding: .5em;
    border-radius: 0 3px 3px 3px;
    display: none;
    color: #333;
    background-color: #eee;
}

div.tabbed > a:target > div {
    display: block;
}

The :target pseudo selector matches what's in the URL hash (http://foo.com#this_is_the_hash) with the element having that hash string as id.

There is another hackish method using transition with very long time:

http://joelb.me/blog/2012/maintaining-css-style-states-using-infinite-transition-delays/

Insane:

http://dabblet.com/gist/2076449

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!