I’m trying to setup a tabbed pane (via jQuery) on one of my pages in a WordPress site. I have the tabs working and for the most part, it looks how I want it at this stage. There’s one issue though – setting the width of the tabs so that each tab’s span is long enough that all of them combined is equal to the space of the pane. Now this normally wouldn’t be too hard, but the thing is I don’t know how many tabs will be there in the end (my client is unsure).
Here’s the CSS I’m using via Jetpack’s Custom CSS functionality:
.ui-tabs-nav {
font-size: larger;
font-weight: bold;
background: none;
height: 30px;
/*To stop nav block scaling of tab size*/
}
.ui-tabs .ui-tabs-nav {
margin: 0;
padding: .2em .2em 0;
border: none;
background: none;
}
.ui-tabs .ui-tabs-nav li {
display: inline;
border: none;
margin: 0 0 -5px;
background: none;
}
.ui-tabs-anchor {
color: #000034;
}
ul.ui-widget-header, ul.ui-widget-content, ul.ui-state-default, ul.ui-state-hover {
background: none;
border: none;
}
.ui-tabs-active a {
text-decoration: none;
background: none;
color: #222222;
position: relative;
z-index: 5;
}
And some javascript to adjust the width of the tabs:
jQuery(document).ready(function($) {
var numTabs = $('#tabs li').length;
var tabWidth = 100 / numTabs;
var tabPercent = tabWidth + "%";
var liArray = $('#tabs li');
var i;
for (i = 0; i < liArray.length; i++) {
liArray[i].style.width = tabPercent;
}
});
And finally, some HTML:
<div id="tabs">
<ul class="ui-widget-header ui-corner-top">
<li><a href="#tabs1">First</a></li>
<li><a href="#tabs2">Second</a></li>
<li><a href="#tabs3">Third</a></li>
</ul>
<div id="tabs1">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<div id="tabs2">Phasellus mattis tincidunt nibh. Cras orci urna, blandit id, pretium vel, aliquet ornare, felis. Maecenas scelerisque sem non nisl. Fusce sed lorem in enim dictum bibendum.</div>
<div id="tabs3">Nam dui erat, auctor a, dignissim quis, sollicitudin eu, felis. Pellentesque nisi urna, interdum eget, sagittis et, consequat vestibulum, lacus. Mauris porttitor ullamcorper augue.</div>
</div>
So yeah, with these snippets in mind – how do I use Javascript to dynamically apply a style to my tabs so that the width of them all combined = 100% of the pane?
Thanks!
Change the li to a block element so you can set its width.
http://jsfiddle.net/bwzhLzco/1/