CSS/JQuery navigation with menu items enlarging on hover without shifting adjacent elements
May 29th, 2010 in Web Design, Web Development, Code
Synopsis: Menu (navigation items) are popping out (font-size increased) on hover without pushing/shifting adjacent menu items.
Bonus: How to center navigation of undefined width.

I strongly encourage all web developers (designers, front-end developers or whatever) to solve all layout problems with simple CSS. Only after several hours of lamentation, declaring an anathema on IE6 (and even IE7), you guys are allowed to use a lovely JQuery.
Here is a nice JQuery menu idea, which I quickly made for one of ours recent projects and would like to share with The Web Designing / Developing World :)
Works for all popular browsers (even for our IE6 & IE7).
Description is much longer than describing subject, but everything is pretty simple:
1. assigning each list-item element the width of a nested anchor (including padding);
2. adding new class (increasing font size and removing padding) to current a
3. on mouse enter: assign current anchor and parent list-item width property
4. on mouse leave: remove class with font size enlargement, and remove padding.
<script type="text/javascript">
$(document).ready(function() {
$(".main-menu .wrapper-ul ul li").each( function () {
$(this).width($(this).children().innerWidth());
});
$(".main-menu .wrapper-ul ul li a").mouseenter(function() {
$(this).width($(this).parent().innerWidth());
$(this).addClass("active");
});
$(".main-menu .wrapper-ul ul li a").mouseleave(function() {
$(this).removeClass("active");
$(this).addClass("no-padding");
});
});
</script>
Be careful with paddings: don't make them to small. Otherwise, your enlarged text simply won't fit into its' parent list-item.
<div class="main-menu">
<div class="wrapper-ul">
<ul>
<li><a href="#">home</a></li>
<li><a href="#">success stories</a></li>
<li><a href="#">about us</a></li>
<li><a href="#">contact</a></li>
</ul>
</div>
</div>
Bonus: a CSS technic to center our menu elements with undefined width.
Here comes the CSS:
.no-padding { padding:0 !important; }
.main-menu { width: 600px; margin: 0 auto; height: 39px; border: solid 1px #ccc; }
.wrapper-ul {
float: left;
width: 100%;
overflow: hidden;
position:relative;
height: 39px; }
.wrapper-ul ul{
clear: left;
height: 39px;
float: left;
left: 50%;
margin: 0;
padding: 0;
position: relative;
text-align: center; }
.wrapper-ul ul li{
display: block;
float: left;
list-style: none;
padding: 0;
margin: 0;
height: 39px;
position: relative;
right: 50%; }
.wrapper-ul ul li a{
display: block;
line-height: 39px;
font-size: 12px;
white-space: nowrap;
color: #21949e;
padding: 0 32px;
text-transform: uppercase;
text-decoration: none;
font-weight: bold; }
.wrapper-ul ul li .active {
font-size: 18px;
font-weight: bold;
margin: 0;
padding: 0 !important;
text-decoration: none; }
Don't forget to "clear" all floated elements.
Menu text is popping, navigation elements and navigation itself is centered (you don't even need to assign their width by yourself!), everything is W3C valid and cross-browser tested!Ôªø
![]() |
Masha Front-End Developer & Email Marketing |
Comments (7)
Really good sharing this.
by Christian Louboutin - 06/03/2010 @ 09:09pm
Hey, nice site you got here! Keep up the good work!
by Buy Zenerx - 06/11/2010 @ 04:29am
Thanks. Using this technique on my home page. Works great!
by Conner - 10/12/2010 @ 09:45pm
Link building is a timely and stressfull task.
You can get backlinks extremely quickly using some great
<a href=http://www.tlsubmit.com>link building</a> programs.
you can submit <a href=http://www.tlsubmit.com>edu backlinks</a> which are the highest
priority backlinks you can possibly get in a link building campaign.
there are lots of things to consider when try to get links to your site.
relevant backlinks are the highest priority when it comes to getting a great
page rank.
get backlinks fast using good seo software such as tlsubmit.
how to get links, you can get great results using backlink seo programs but you must
be carefull you dont target the wrong sites make sure the content and sites are 80%
relevant to your own!
by enelligiome - 02/26/2011 @ 05:33am
Hi - I am really happy to discover this. cool job!
by AswadKannaw - 04/20/2011 @ 09:13am
thanks for sharing .
I really very like here doejo.com . I will come here often .
by Pargaicscyday - 05/19/2011 @ 12:38am
Thanks for this wonderful community. I love it! <a href="http://www.seomarketingservicesonline.com">Get Backlinks</a>
by yestedax - 05/23/2011 @ 07:17am