:target pseudo selector and tabs

前端 未结 1 2051
没有蜡笔的小新
没有蜡笔的小新 2021-01-14 06:10

So I want to create a tab system using only CSS.

what I have so far works, but I don\'t know how to make one tab visible by default.

The tabs:



        
相关标签:
1条回答
  • 2021-01-14 06:52

    Well, if you make your default your last tab (section:last-child), then I think this could work:

    .tabs section,
    .tabs section:target ~ section {
       display: none;
    }
    

    Using the general sibling selector ~ requires that the element precede the siblings it targets, so hence the reason for the last-child rather than first-child requirement.

    EDIT: 11-12-2011, I did find a way for you to highlight your a tags as active! Assuming it meets your particular application. Here is some simple modified code for proof of concept (only tested in FF):

    HTML

    <section class="tabs">
      <ul class="nav">
       <li><a href="#tab1">1</a></li>
       <li><a href="#tab2">2</a></li>
       <li><a href="#tab3">3</a></li>
      </ul>
    
    
      <section id="tab2"><div class="tabActive"></div> content for 2... </section>
      <section id="tab3"><div class="tabActive"></div> content for 3... </section>
      <section id="tab1"><div class="tabActive"></div> content for 1... </section>
    
    </section>
    

    CSS

    .nav {
        position: relative;
        z-index: 2;
        margin: 10px .5em 0;
    }
    .nav li {
        padding: .5em;
        width: 2em;
        text-align: center;
        float: left;
    }
    
    .tabs section,
    .tabs section:target ~ section {
       display: none;
    }
    
    .tabs section:target,
    .tabs section:last-child {
        display: block;
        clear: left;
        margin: 0 .5em;
        min-width: 300px;  /* for show only */
        min-height: 200px; /* for show only */
        border: 1px solid black;   
        position: relative;
        z-index: 1;
        padding: 10px;
    }
    
    .tabActive { /* set for tab 1 */
        width: 2em;
        height: 2em;
        position: absolute;
        top: -2em;
        left: .5em;
        border: 1px solid black;
        border-bottom: transparent;
        background-color: inherit;
        margin-top: -1px; /* top border height */
        margin-left: -1px; /* left border width */
    }
    
    #tab1 {background-color: cyan;}
    #tab2 {background-color: yellow;}
    #tab3 {background-color: pink;}
    
    #tab2 .tabActive {left: 3.5em;}
    #tab3 .tabActive {left: 6.5em;}
    
    0 讨论(0)
提交回复
热议问题