【www.gdgbn.com--jquery】
通过css教程实现水平居中:
.classname{
margin:0 auto;
width:200px;
height:200px;
}
2、通过css实现水平居中和垂直居中
通过css创建一个水平居中和垂直居中的div是一件比较麻烦的事情,您必须事先知道另外一个div的尺寸:
.classname{
width:300px;
height:200px;
position:absolute;
left:50%;
top:50%;
margin:-100px 0 0 -150px;
}
3、通过jquery实现水平居中和垂直居中前面已经提到过了,css的方法只适用于有固定尺寸的div,所以到jquery发挥作用了:
$(window).resize(function(){
$(".classname").css({
position:"absolute",
left: ($(window).width() - $(".classname").outerwidth())/2,
top: ($(window).height() - $(".classname").outerheight())/2
});
});
//初始化函数
$(window).resize();