thomaSmith thomaSmith - 4 months ago 11
CSS Question

How to change the active color of the tabs title background using css

I am trying to change the background color of the active title tab in my tab menu.

tab

<div class="row">
<div class="medium-12 columns">
<ul class="tabs" data-tabs id="authentication_tab">
<li class="tabs-title is-active"><a href="#panel1" aria-selected="true">Tab 1</a></li>
<li class="tabs-title"><a href="#panel2">Tab 2</a></li>
</ul>
<div class="tabs-content" data-tabs-content="authentication_tab">
<div class="tabs-panel is-active" id="panel1">
<p>Vivamus hendrerit arcu sed erat molestie vehicula. Sed auctor neque eu tellus rhoncus ut eleifend nibh porttitor. Ut in nulla enim. Phasellus molestie magna non est bibendum non venenatis nisl tempor. Suspendisse dictum feugiat nisl ut dapibus.</p>
</div>
<div class="tabs-panel" id="panel2">
<p>Suspendisse dictum feugiat nisl ut dapibus. Vivamus hendrerit arcu sed erat molestie vehicula. Ut in nulla enim. Phasellus molestie magna non est bibendum non venenatis nisl tempor. Sed auctor neque eu tellus rhoncus ut eleifend nibh porttitor.</p>
</div>
</div>
</div>
</div>


css

li.is-active {

background-color:blue;

}

Answer

Using jQuery (lots of comments in case the syntax of jQuery is new to you):

// Bind a function to the `click` event of elements matching the selector `.tabs-title`
// Note that within a jQuery function, `$(this)` is the current element represented as a jQuery element object.
$(".tabs-title > a").click(function() {
    // For each element with the class selector `.tabs-title`
    $(".tabs-title").each(function() {
        // Remove the class "is-active" from the current element in the loop
        $(this).removeClass("is-active")
    })

    // Get the parent of the clicked `a` element (i.e., the `.tabs-title` element), then add the class.
    $(this).parent().addClass("is-active");
})

jsFiddle.

EDIT:

To reference jQuery, add this within the <head> of your document:

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>