AMDG
AMDG

Reputation: 975

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:

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!

Upvotes: 1

Views: 1092

Answers (3)

Jeff Clayton
Jeff Clayton

Reputation: 7291

CSS variables were enabled in Firefox version 31. You had to set a flag in earlier versions 29 and 30 to enable it.

Upvotes: 0

Matheus
Matheus

Reputation: 839

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.

Upvotes: 2

xec
xec

Reputation: 18024

Unfortunately css variables are pretty far from being production ready (i.e. supported widely enough to be used on public sites).

I believe the most viable (pure css) solution is the most obvious one, seeing as there are a limited amount of tabs, you could simply have a selector for each one. This isn't particularly elegant, but works just fine.

.tab:nth-child(1) > a {
    left: calc(74px * 0);
}
.tab:nth-child(2) > a {
    left: calc(74px * 1);
}
.tab:nth-child(3) > a {
    left: calc(74px * 2);
}
/* etc.. */

http://jsfiddle.net/7fZnn/2/

Upvotes: 0

Related Questions