如何让DIV相对于body水平和垂直居中
1个回答
展开全部
主要的样式定义如下:
1
2
body {<a href="https://www.baidu.com/s?wd=TEXT-ALIGN&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLmWm4rAf1ujm1uW7hP1fd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHDknj01nWDvrHbsnHc4nW04rf" target="_blank" class="baidu-highlight">TEXT-ALIGN</a>: center;}
#center { <a href="https://www.baidu.com/s?wd=MARGIN-RIGHT&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLmWm4rAf1ujm1uW7hP1fd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHDknj01nWDvrHbsnHc4nW04rf" target="_blank" class="baidu-highlight">MARGIN-RIGHT</a>: auto; <a href="https://www.baidu.com/s?wd=MARGIN-LEFT&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLmWm4rAf1ujm1uW7hP1fd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHDknj01nWDvrHbsnHc4nW04rf" target="_blank" class="baidu-highlight">MARGIN-LEFT</a>: auto; }
说明:
首先在父级元素定义TEXT-ALIGN: center;这个的意思就是在父级元素内的内容居中;对于IE这样设定就已经可以了。但在mozilla中不能居中。解决办法就是在子元素定义时候设定时再加上“MARGIN-RIGHT: auto;MARGIN-LEFT: auto; ”
需要说明的是,如果你想用这个方法使整个页面要居中,建议不要套在一个DIV里,你可以依次拆出多个div,只要在每个拆出的div里定义MARGIN-RIGHT: auto;MARGIN-LEFT: auto; 就可以了。
如何使图片在DIV 中垂直居中
用背景的方法。举例:
1
body{BACKGROUND: url(/uploadfiles/2006/05181435734.gif) #FFF no-repeat center;}
关键就是最后的center,这个参数定义图片的位置。还可以写成“top left”(左上角)或者"bottom right"等,也可以直接写数值"50 30"。
如果是文字,便不能用背景方法,可以用增高行距的办法变通实现垂直居中,完整代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<html>
<head>
<style>
body{<a href="https://www.baidu.com/s?wd=TEXT-ALIGN&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLmWm4rAf1ujm1uW7hP1fd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHDknj01nWDvrHbsnHc4nW04rf" target="_blank" class="baidu-highlight">TEXT-ALIGN</a>: center;}
#center{ MARGIN-RIGHT: auto;
MARGIN-LEFT: auto;
height:200px;
background:#F00;
width:400px;
<a href="https://www.baidu.com/s?wd=vertical-align&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLmWm4rAf1ujm1uW7hP1fd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHDknj01nWDvrHbsnHc4nW04rf" target="_blank" class="baidu-highlight">vertical-align</a>:middle;
<a href="https://www.baidu.com/s?wd=line-height&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLmWm4rAf1ujm1uW7hP1fd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHDknj01nWDvrHbsnHc4nW04rf" target="_blank" class="baidu-highlight">line-height</a>:200px;
}
</style>
</head>
<body>
<div id="center"><p>test content</p></div>
</body>
</html>
说明:
vertical-align:middle;表示行内垂直居中,我们将行距增加到和整个DIV一样高line-height:200px;然后插入文字,就垂直居中了。
1
2
body {<a href="https://www.baidu.com/s?wd=TEXT-ALIGN&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLmWm4rAf1ujm1uW7hP1fd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHDknj01nWDvrHbsnHc4nW04rf" target="_blank" class="baidu-highlight">TEXT-ALIGN</a>: center;}
#center { <a href="https://www.baidu.com/s?wd=MARGIN-RIGHT&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLmWm4rAf1ujm1uW7hP1fd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHDknj01nWDvrHbsnHc4nW04rf" target="_blank" class="baidu-highlight">MARGIN-RIGHT</a>: auto; <a href="https://www.baidu.com/s?wd=MARGIN-LEFT&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLmWm4rAf1ujm1uW7hP1fd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHDknj01nWDvrHbsnHc4nW04rf" target="_blank" class="baidu-highlight">MARGIN-LEFT</a>: auto; }
说明:
首先在父级元素定义TEXT-ALIGN: center;这个的意思就是在父级元素内的内容居中;对于IE这样设定就已经可以了。但在mozilla中不能居中。解决办法就是在子元素定义时候设定时再加上“MARGIN-RIGHT: auto;MARGIN-LEFT: auto; ”
需要说明的是,如果你想用这个方法使整个页面要居中,建议不要套在一个DIV里,你可以依次拆出多个div,只要在每个拆出的div里定义MARGIN-RIGHT: auto;MARGIN-LEFT: auto; 就可以了。
如何使图片在DIV 中垂直居中
用背景的方法。举例:
1
body{BACKGROUND: url(/uploadfiles/2006/05181435734.gif) #FFF no-repeat center;}
关键就是最后的center,这个参数定义图片的位置。还可以写成“top left”(左上角)或者"bottom right"等,也可以直接写数值"50 30"。
如果是文字,便不能用背景方法,可以用增高行距的办法变通实现垂直居中,完整代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<html>
<head>
<style>
body{<a href="https://www.baidu.com/s?wd=TEXT-ALIGN&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLmWm4rAf1ujm1uW7hP1fd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHDknj01nWDvrHbsnHc4nW04rf" target="_blank" class="baidu-highlight">TEXT-ALIGN</a>: center;}
#center{ MARGIN-RIGHT: auto;
MARGIN-LEFT: auto;
height:200px;
background:#F00;
width:400px;
<a href="https://www.baidu.com/s?wd=vertical-align&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLmWm4rAf1ujm1uW7hP1fd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHDknj01nWDvrHbsnHc4nW04rf" target="_blank" class="baidu-highlight">vertical-align</a>:middle;
<a href="https://www.baidu.com/s?wd=line-height&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLmWm4rAf1ujm1uW7hP1fd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHDknj01nWDvrHbsnHc4nW04rf" target="_blank" class="baidu-highlight">line-height</a>:200px;
}
</style>
</head>
<body>
<div id="center"><p>test content</p></div>
</body>
</html>
说明:
vertical-align:middle;表示行内垂直居中,我们将行距增加到和整个DIV一样高line-height:200px;然后插入文字,就垂直居中了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询