Reputation: 131
i've been struggling with something: centering my CSS menu!! i can't figure out how to do it. What am i doing wrong?
Upvotes: 0
Views: 239
Reputation: 7256
You used position fixed. So the margin auto doesn't work with fixed position. You can do the following:
ul
. If you want you ul
to increase as li
increases, you can set ul
width to inherit. Upvotes: 1
Reputation: 1406
Try this
.menu {
height: 80px;
border: 1px solid rgba(255,255,255,0.3);
position: fixed;
top: -8px;
left: 50%;
margin-left: -200px; /* half of the .menu width if you don't know width delete this line */
}
And if you're not going to know the width of this element you can dynamically calculate it with javascript/jquery.
$(document).ready(function() {
var wdth = $('.menu').width();
wdth = wdth/2;
$('.menu').css('margin-left', -wdth);
});
Here is the working example http://jsfiddle.net/yfVs2/11/
Upvotes: 2