我试图居中一个元素。该元素可能会改变宽度,因此我不能使用margin: 0 auto
的CSS方法。用Jquery居中元素
这里是我的代码:
$.fn.center = function() {
$(this).css({
'margin-left': ($(this).parent().innerWidth() - $(this).outerWidth())/2 + "px",
'margin-right': ($(this).parent().innerWidth() - $(this).outerWidth()) + ($(this).outerWidth()/2) + "px"
});
};
$('#nav').center();
不幸的是,这是行不通的。你能帮我弄清楚为什么?我应该使用绝对定位吗?
谢谢。
显示[JS小提琴](http://jsfiddle.net/)或类似演示可能? –
_这不是工作_是什么意思? – Andreas
@davidthomas它的生活在一个tumblr网站即时通讯制作:http://dev-indiehaz.tumblr.com/应该集中在标题下的导航链接! – Harry