Skip to content

Instantly share code, notes, and snippets.

@iansvo
Created February 15, 2015 15:03
Show Gist options
  • Save iansvo/6cfda954e7afe0abd8ac to your computer and use it in GitHub Desktop.
Save iansvo/6cfda954e7afe0abd8ac to your computer and use it in GitHub Desktop.
Nav Equalizer Demo
<div class="container">
<h1>Nav Equalizer Demo</h1>
<hr>
<p>The below navigation will automatically size itself according to the length/content of the links added to it. The demo below demonstrates the effect when using variable amounts of links and/or variable link text length.</p>
<p>The trick is to use a display table/table-cell properties on the menu and it's list items respectively. This insures that the browser handles the menu items correctly and doesn't rely on awkward margin-calulations, especially with variable amounts of menu items.</p>
<p>Go ahead and try adding/removing menu items to see the effect. You can also adjust your browser window to see how it performs in a responsive scenario or with variable browser widths.</p>
<div class="notice"></div>
<p class="message text-center add-bottom"></p>
<p class="alert text-center" style="display:none;"></p>
<nav role="navigation">
<ul class="flex-menu">
<li><a href="#">Link 1 Text is Long</a></li>
<li><a href="#">Link 2 Text Is longer</a></li>
<li><a href="#">Link 3</a></li>
<li><a href="#">Link 4</a></li>
<li><a href="#">Link 5 Is the longest yet</a></li>
<li><a href="#">Link 6</a></li>
</ul>
</nav>
<div class="text-center add-bottom">
<label for="link_label"><input type="text" id="link_label" value="Another Link"></label>
<button class="add btn">Add Menu Item</button>
</div>
<div class="border-top text-center add-bottom">
<button class="remove btn">Remove Last Menu Item</button> <button class="reset btn">Reset Demo</button>
</div>
<div class="add-bottom text-center">Note: Container width = 90% max viewport width</div>
</div><!--end container-->
<div class="author text-center">Made by <a href="https://twitter.com/iansvo" target="_blank">@iansvo</a></div>

Nav Equalizer Demo

The below navigation will automatically size itself according to the length/content of the links added to it. The demo below demonstrates the effect when using variable amounts of links and/or variable link text length.

The trick is to use a display table/table-cell properties on the menu and it's list items respectively. This insures that the browser handles the menu items correctly and doesn't rely on awkward margin-calulations, especially with variable amounts of menu items.

Go ahead and try adding/removing menu items to see the effect. You can also adjust your browser window to see how it performs in a responsive scenario or with variable browser widths.

A Pen by Ian Svoboda on CodePen.

License.

// Setup variables
var $menuList,
$menuListItems,
$menuListItemAnchors,
menuListWidth,
menuListItemsWidth,
menuListItemCount,
container_width,
defaultDemo,
linkText;
//Sets Variable Values
$menuList = $('.flex-menu');
$menuListItems = $('.flex-menu > li');
$menuListItemAnchors = $('.flex-menu li a');
function navEqualizer() {
$menuList = $('.flex-menu');
$menuListItems = $('.flex-menu > li');
$menuListItemAnchors = $('.flex-menu li a');
menuListWidth = $menuList.width();
menuListItemCount = $menuListItems.length;
menuListItemsWidth = menuListWidth / menuListItemCount;
// Actions
if( $(window).width() >= 768 )
{
$menuListItems.css('width', menuListItemsWidth)
$('.container .message').html('The list items are approx. ' + Math.round(menuListItemsWidth) + 'px wide.')
if(menuListItemsWidth < 100) {
$('.alert').show().html('At this point you may be trying to fit too many list items. Try using fewer list items for best results.')
}
if(menuListItemsWidth >= 100) {
$('.alert').hide();
}
}
if( $(window).width() < 768 ) {
$menuListItems.css('width', '100%');
menuListItemsWidth = $menuListItems.width();
$('.container p').html('The list items are the width of the container.')
}
} // end function
function demoFunctions() {
$('.reset.btn').click(function() {
$menuList.html(defaultDemo);
navEqualizer();
})
$('.add.btn').click(function() {
linkText = $('#link_label').val();
$menuList.append('<li><a href=#">'+linkText+'</a></li>');
navEqualizer();
})
$('#link_label').keypress(function(event) {
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13') {
linkText = $('#link_label').val();
$menuList.append('<li><a href=#">'+linkText+'</a></li>');
navEqualizer();
}
});
$('.remove.btn').click(function() {
$('.flex-menu > li:last').remove();
navEqualizer();
});
}
function containerWidth() {
container_width = $('.container').width();
$('.notice').html('The container is ' + container_width + 'px wide.' );
}
// call on resize
$(window).resize(function() {
containerWidth();
$('.notice').html('The container is ' + container_width + 'px wide.' );
navEqualizer();
});
// Doc Ready
$(function() {
containerWidth()
navEqualizer();
defaultDemo = $menuList.html();
demoFunctions();
});
/* Adds Open Sans Font */
@import url(http://fonts.googleapis.com/css?family=Open+Sans);
/* Basic Demo Page Styles */
html {height: 100%;}
body {
background: #3498DB;
font-size: 16px;
line-height: 20px;
color: #FFFFFF;
font-family: 'Open Sans', sans-serif;
height: 100%;
}
.container {
max-width: 1200px;
width: 90%;
margin: 0 auto;
margin-top: 10%;
background: none;
padding-bottom: 20px;
}
nav {margin: 40px 0}
.notice {
font-size: 2em;
color: #FFFFFF;
line-height: 30px;
background: none;
text-align: center;
margin: 40px 0;
}
.alert {
font-weight: bold;
color: #000;
padding: 1em;
line-height: 20px;
font-size: 14px;
}
/* Controls Styling */
.btn {
padding: 10px;
cursor: pointer;
background: none;
text-transform: uppercase;
border: 1px solid #FFF;
color: #FFF;
font-weight: bold;
}
.add.btn {
width: 150px;
margin-left: 10px;
}
.reset.btn {
width: 150px;
margin-left: 10px;
}
.border-top {
border-top: 1px solid #DEDEDE;
padding-top: 20px;
}
.remove.btn {width: 225px;}
input {width: 205px; padding: 12px 10px;}
.author {margin: 60px auto 20px;}
.author a {color: #FFF;}
.author a:hover {color: #E3263A;}
/* Menu Styles */
.flex-menu {
list-style: none;
padding: 0;
margin: 0;
width: 100%;
max-width: 100%;
}
/* Solid fallback styling to insure mostly equal separation even without javascript*/
@media (min-width: 768px) {
.flex-menu { display: table;}
.flex-menu li {
display: table-cell;
}
}
/* Menu Look-n-Feel */
.flex-menu li {
background: #E3263A;
color: #EFEFEf;
border-right: 1px solid #DEDEDE;
vertical-align: middle;
text-align: center;
}
.flex-menu li:hover {background: #2c3e50;}
.flex-menu li:last-child {border-right: none;}
.flex-menu li a {
padding: 20px 5px;
display: block;
text-align: center;
color: inherit;
text-transform: uppercase;
text-decoration: none;
}
.flex-menu li:hover a {color: #E3263A;}
/* Helper Classes */
.text-center {text-align: center;}
.add-bottom {margin-bottom: 20px;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment