用css怎么样让DIV块垂直居中,方法越多越好!

html-css019

用css怎么样让DIV块垂直居中,方法越多越好!,第1张

绝对定位:

<style>

body{margin:0padding:0position:relative}

#box{width:500pxheight:500pxposition: absolutetop:50%left:50%margin-top:-250pxmargin-left:-250pxbackground:#eeeeee}

</style>

<div id="box">这个层是垂直,水平居中的,说说原理.box,对body来定位,然后距离顶部50%,左边50%,再用margin-top,用个负的box高度一半,margin-left:负的box宽的一半,这样就居中了。

</div>

有四种形式:1.链入外部样式表,就是把样式表保存为一个样式表文件,然后在页面中用<link rel="stylesheet" type="text/css" href="*.css"链接这个样式表文件.2.内部样式表,就是把样式表放到页面的<head区里.3.导入外部样式表,用@import,在<head与</head<style type="text/css"<!--@import "*.css"--</style4.内嵌样式,就是在标签内写入style="",比如: