Search code examples
cssfirefoxcss-variables

Use CSS variables Firefox


I'm working on a full CSS tabs navigation. Here it is what I want to do: http://jsfiddle.net/7fZnn/1/

But I don't want to use inline CSS to place my tabs. So I investigate what can replace this inline CSS. I find those:

  • the attr() function, only supported for content attribute when you select and element with :before of :after
  • CSS counters. That would be great, but as the attr() function, this just work in content attribute
  • CSS variables, this should work in Firefox 29, actually I use 30. But it don't, and I don't know why...

Here is my code with CSS variables (and counters in comment): http://jsfiddle.net/j8wxQ/2/

The HTML

<div class="container">
    <div class="tabs">
        <div class="tab" id="foo">
            <a href="#foo">Foo</a>
            <div class="content">
                <h1>Onglet 1</h1>
                <p>Lorem Ipsum...</p>
            </div>
        </div>
        <div class="tab" id="bar">
            <a href="#bar">Bar</a>
            <div class="content">
                <h1>Onglet 2</h1>
                <p>Lorem Ipsum...</p>
            </div>
        </div>
        <div class="tab" id="toto">
            <a href="#toto">Toto</a>
            <div class="content">
                <h1>Onglet 3</h1>
                <p>Lorem Ipsum...</p>
            </div>
       </div>
       <div class="tab" id="tata">
           <a href="#tata">Tata</a>
           <div class="content">
               <h1>Onglet 4</h1>
               <p>Lorem Ipsum...</p>
           </div>
       </div>
       <div class="tab default" id="titi">
           <a href="#titi">Titi</a>
           <div class="content">
               <h1>Onglet 5</h1>
               <p>Lorem Ipsum...</p>
           </div>
       </div>
    </div>
</div>

The CSS

:root {
    var-tab: 0px;
}

@media screen and (min-width: 1025px) {
    .container {
        width: 1000px;
        margin: auto;
    }
}

@media screen and (max-width: 1024px) and (min-width: 641px) {
    .container {
        width: 99%;
        margin: auto;
    }
}

@media screen and (max-width: 640px) {
    .container {
        width: 100%;
        margin: 0;
        padding: 0;
    }

    body, html {
        margin: 0;
        padding: 0;
    }
}

.tabs {
    position: relative;
    width: 100%;
    /*counter-reset: tab 0;*/
}

.tab {
    /*counter-increment: tab;*/
    var-tab: calc(var(tab) + 75px);
}

.tab .content {
    display: none;
    position: absolute;
    top: 50px;
    margin-top: -5px;
    border: solid 5px rgba(0,0,255, 0.1);
    padding: 10px 10px 10px 10px;
}

.tab:target .content {
    display: block;
    position: absolute;
    top: 50px;
}

.tab.default .content{
    display: block;
    position: absolute;
    top: 50px;
}

.tab:target ~ .tab.default .content {
    display: none;
}

.tabs .tab a {
    position: absolute;
    top: 0;
    left: var(tab);
    /*left: calc(75px * counter(tab));*/
    display: inline-block;
    height: 45px;
    line-height: 45px;
    min-width: 75px;
    margin: 0;
    padding: 0;
    text-decoration: none;
    color: #000;
    vertical-align: middle;
    border-bottom: solid 5px rgba(0,0,255, 0.1);
    text-align: center;
}

.tabs .tab a:hover {
    border-bottom: solid 5px rgba(0,0,255,0.5);
}

.tabs .tab:target a {
    border-bottom: solid 5px rgba(0,0,255,1);
}

p {
    text-align: justify;
}

So, what did I miss? Do you think I can use something else to achieve my goal?

Thanks for your help!


Solution

  • You could try to change these:

    .tab {
        /*counter-increment: tab;*/
        /*var-tab: calc(var(tab) + 75px);*/
        width: 75px; //fixed width, like you did on your example
        display: inline-block;
    }
    
    .tab .content {
        display: none;
        position: absolute;
        top: 50px;
        left: 0; //push all content to the left edge of tab
        margin-top: -5px;
        border: solid 5px rgba(0,0,255, 0.1);
        padding: 10px 10px 10px 10px;
    }
    

    I've update you fiddle, see if this suits you.

    Observation: That little spacing beetwen the tabs might be the html markup, hidden space characters beetwen the .tabs div's.