Leverage CSS variables in Firefox to customize your webpage

I am currently developing a comprehensive CSS tabs navigation system. Here is the link to what I'm trying to achieve: http://jsfiddle.net/7fZnn/1/

However, I want to avoid using inline CSS to position my tabs. In my search for alternatives, I came across the following options:

  • The attr() function, which is only supported for content attribute when selecting an element with :before or :after
  • CSS counters would be ideal, but like the attr() function, it only works in content attribute
  • CSS variables should work in Firefox 29; unfortunately, even though I am using version 30, it doesn't seem to work and I'm unsure why...

Here is my code utilizing CSS variables (with counters in comments): 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>Tab 1</h1>
                <p>Lorem Ipsum...</p>
            </div>
        </div>
        <div class="tab" id="bar">
            <a href="#bar">Bar</a>
            <div class="content">
                <h1>Tab 2</h1>
                <p>Lorem Ipsum...</p>
            </div>
        </div>
        <div class="tab" id="toto">
            <a href="#toto">Toto</a>
            <div class="content">
                <h1>Tab 3</h1>
                <p>Lorem Ipsum...</p>
            </div>
       </div>
       <div class="tab" id="tata">
           <a href="#tata">Tata</a>
           <div class="content">
               <h1>Tab 4</h1>
               <p>Lorem Ipsum...</p>
           </div>
       </div>
       <div class="tab default" id="titi">
           <a href="#titi">Titi</a>
           <div class="content">
               <h1>Tab 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;
}

If you have any insights into what I might be overlooking or suggestions for alternative methods to achieve my objective, your assistance would be greatly appreciated!

Thank you for your help!

Answer №1

You might want to consider making the following adjustments:

.tab {
    /*counter-increment: tab;*/
    /*var-tab: calc(var(tab) + 75px);*/
    width: 75px; //keeping a fixed width, similar to your example
    display: inline-block;
}

.tab .content {
    display: none;
    position: absolute;
    top: 50px;
    left: 0; //aligning all content to the left edge of the tab
    margin-top: -5px;
    border: solid 5px rgba(0,0,255, 0.1);
    padding: 10px 10px 10px 10px;
}

I've made some changes in your fiddle, take a look and see if it meets your needs.

Note: The slight spacing between the tabs could possibly be caused by hidden space characters in the HTML markup between the .tabs div's.

Answer №2

Regrettably, css variables are still in the process of becoming production ready (meaning they are not widely supported enough for use on public websites).

In my opinion, the most practical solution using pure css is to create a selector for each tab since there are only a limited number of tabs. While this method may not be the most elegant, it functions effectively.

.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);
}
/* repeat as necessary */

Check out this demo link for reference

Answer №3

Firefox version 31 introduced support for CSS variables without the need to set any flags. However, in versions 29 and 30, a flag had to be configured in order to enable this feature.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the proper way to ensure a pull right display appears correctly?

This is the code I am currently using .tag-container.bottom-border.col-middle h1.text-center {{ tag.name }} button.btn.btn-follow.pull-right(ng-hide="hasFollowed" ng-click="tagArticles.followTag()") Follow I am trying to align the tag name in th ...

Connections transition among associated HTML pages

I am working on my very first website using HTML and I'm encountering some challenges with maintaining consistency across pages. On the top of my page, I have a div containing some links. While they function correctly, I noticed that the spacing betw ...

The flex container cannot contain all of the content due to an

I am looking to implement a flexbox dropdown menu similar to this: https://i.sstatic.net/Cer9D.png After researching online, I found a simple dropdown menu example. However, I want to customize it using flexbox and its properties. The issue I encountered ...

"Struggling to position the bottom navbar at the center in Bootstrap?

Can someone help me center the icons in my second navigation bar? I've tried aligning them left and right, and that works, but when I try to center them, it doesn't work at all. Did I miss something in the code or make a mistake? Any assistance w ...

Half-extended Bootstrap 4 container reaching the edge of the page

I'm looking to create a layout with a .container that consists of two distinct halves. The left half should function as a standard .col-6, while the right half should extend all the way to the edge of the screen, similar to a .col-6 within a .contain ...

Achieving a stacked arrangement of divs upon click-through using only pure JavaScript

http://jsfiddle.net/Zgxv9/ In the example provided in my fiddle, the div elements return to their original positions when clicked. However, I am looking for a solution where the last clicked div always remains on top. This is my current code: function di ...

What is causing the resistance in changing the color of my current navigation items?

I've encountered a small challenge with my header section. Despite multiple attempts, I'm struggling to change the color of the 'current' menu item. It seems that overriding Bootstrap's default color is proving to be more difficult ...

Can the font-weight of FontAwesome icons be altered?

Is there a method to decrease the weight of one of the FontAwesome icons on my website? It appears much heavier than the font style I am currently utilizing. Here is how it currently displays: : Not very appealing, right? I attempted adjusting the font-w ...

A straightforward jQuery conditional statement for dynamically generated content

If the div '#ajax' contains content, I want to make the div '.container' clickable. Here is the basic jQuery script I have implemented: if ('#ajax:empty') { $('.container').css('pointer-events', ' ...

Enhance the appearance of 6 image URLs by wrapping them in dynamic divs with the

Can someone assist me in displaying 6 images retrieved from a MySQL database on an MVC Razor view? Images 1 and 6 should be placed in separate divs labeled "item", while images 2, 3, 4, and 5 should be grouped together in a div called "item-small". Below i ...

Conceal navigation options on smaller screens and enable drop-down menu functionality with Bootstrap 3

With the usage of Bootstrap 3, I have successfully hidden two menu items when the screen is resized. However, these items still appear in the drop down menu that appears on the top left (with three bars) when the screen size is reduced. Is there a way to k ...

Prolong the ultimate position of the initial animation for the translated object

I understand that this question has been asked multiple times before, but I am seeking a fresh approach to these animations. Issue: The second box contains multiple animations, and I am trying to achieve the same effect as the first one. However, it seem ...

Creating a border with tabs and a triangle using CSS

I'm having trouble getting my HTML/CSS tabs to look like the ones in the image. I've experimented with border-radius but haven't been able to achieve the desired result. Can anyone provide guidance on how to replicate these tabs using only ...

Position the label and the select dropdown side by side within the sweetalert 2 component

Can anyone provide an example of aligning a label and dropdown in the same row using sweetalert2? I attempted to customize the label placement, but it appears on a separate line. I would like to log the selected dropdown item upon clicking the OK button. ...

Can someone assist me in successfully incorporating a web font into a template?

After downloading a Wordpress template, I found the font used for h1 and h2 text to be difficult to read due to its slim design - Century Gothic. However, I prefer using Noteworthy, a font available on my Mac. I converted Noteworthy from OTF to web-font st ...

Troubleshooting issue arising from transferring code from CodePen to a text editor

I've attempted to recreate a Codepen example in my HTML files on my computer, but it's not displaying correctly. While the static layout resembles what appears in Codepen, the background with the moving squares isn't functioning as expected ...

Displaying URLs stylishly with Pills Bootstrap

When a pill is selected from the list, I need to display a specific URL that can be used elsewhere. However, there is an href="#pills-something" attribute that directs to an ID below. I am looking for something like: mysite.com/myspecificpill or ...

Printing Apex Oracle reports using Internet Explorer

I am facing a challenge with printing my page that contains two interactive reports. To enable printing, I have utilized a JavaScript function that triggers window.print(). I have incorporated CSS to adjust the layout of my tables when printed. @media pr ...

Aligning the Twitter and Facebook buttons

Issue with Social Media Button Alignment I'm encountering a problem with aligning Twitter and Facebook buttons on my website. The alignment seems to be off and I need some help fixing it. Code Snippet html: <span id="social"> ...

Is it possible for a website administrator to view the modifications I have made to the CSS and HTML code?

Is it possible for a website to detect any temporary changes made to their CSS or Html through developer tools, even though these changes are not permanent? ...