html元素水平居中、垂直居中、水平垂直居中于其父级元素的方法
2014-04-08 15:29
369 查看
html元素 水平居中 于 其父级元素的方法:
方法1:
方法2:
html元素 垂直居中 于 其父级元素的方法:
html元素 水平垂直居中 于 其父级元素的方法:
方法1:
方法2:
完整代码:
效果图:
方法1:
<div class="wrap"> <div class="left-right-middle1">左右居中方法1</div> </div>
html,body,div{ margin:0; padding:0; height:100%; position:relative; } .wrap{ width:400px; height:300px; margin:10px; border:1px solid #000; } .left-right-middle1{ width:200px; background-color:#69F; margin:0 auto; }
方法2:
<div class="wrap"> <div class="left-right-middle2">左右居中方法2</div> </div>
html,body,div{ margin:0; padding:0; height:100%; position:relative; } .wrap{ width:400px; height:300px; margin:10px; border:1px solid #000; } .left-right-middle2{ width:200px; background-color:#69F; left:50%; margin-left:-100px; }
html元素 垂直居中 于 其父级元素的方法:
<div class="wrap"> <div class="top-bottom-middle">上下居中</div> </div>
html,body,div{ margin:0; padding:0; height:100%; position:relative; } .wrap{ width:400px; height:300px; margin:10px; border:1px solid #000; } .top-bottom-middle{ height:200px; background-color:#69F; top:50%; margin-top:-100px; }
html元素 水平垂直居中 于 其父级元素的方法:
方法1:
<div class="wrap"> <div class="all-middle1">上下左右居中方法1</div> </div>
html,body,div{ margin:0; padding:0; height:100%; position:relative; } .wrap{ width:400px; height:300px; margin:10px; border:1px solid #000; } .all-middle1{ width:200px; height:200px; background-color:#69F; top:50%; margin:-100px auto 0; }
方法2:
<div class="wrap"> <div class="all-middle2">上下左右居中方法2</div> </div>
html,body,div{ margin:0; padding:0; height:100%; position:relative; } .wrap{ width:400px; height:300px; margin:10px; border:1px solid #000; } .all-middle2{ width:200px; height:200px; background-color:#69F; top:50%; left:50%; margin:-100px 0 0 -100px; }
完整代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
<title>Div Middle</title>
<style type="text/css">html,body,div{ margin:0; padding:0; height:100%; position:relative; } .wrap{ width:400px; height:300px; margin:10px; border:1px solid #000; } .left-right-middle1{ width:200px; background-color:#69F; margin:0 auto; }.left-right-middle2{
width:200px;
background-color:#69F;
left:50%;
margin-left:-100px;
}
.top-bottom-middle{
height:200px;
background-color:#69F;
top:50%;
margin-top:-100px;
}
.all-middle1{
width:200px;
height:200px;
background-color:#69F;
top:50%;
margin:-100px auto 0;
}
.all-middle2{
width:200px;
height:200px;
background-color:#69F;
top:50%;
left:50%;
margin:-100px 0 0 -100px;
}
</style>
</head>
<body><div class="wrap"> <div class="left-right-middle1">左右居中方法1</div> </div><div class="wrap">
<div class="left-right-middle2">左右居中方法2</div>
</div><div class="wrap"> <div class="top-bottom-middle">上下居中</div> </div><div class="wrap">
<div class="all-middle1">上下左右居中方法1</div>
</div><div class="wrap"> <div class="all-middle2">上下左右居中方法2</div> </div></body>
</html>
效果图:
相关文章推荐
- html元素水平居中、垂直居中、水平垂直居中于其父级元素的方法
- html元素水平居中、垂直居中、水平垂直居中于其父级元素的方法
- 元素的垂直水平居中的三种方法
- 利用css实现元素水平垂直居中的方法(分情况讨论)
- 关于元素浮动后垂直居中和水平居中的方法
- css中已知宽高的子元素水平垂直居中的三种方法
- css中已知宽高的子元素水平垂直居中的三种方法
- 几种可以让元素水平垂直居中的方法
- 元素水平垂直居中的几种方法
- CSS内联元素、块级元素的水平居中和垂直居中方法总结
- css3中通过设置box-pack和box-align让div里面的各种元素垂直和水平居中的方法
- css中已知宽高的子元素水平垂直居中的三种方法
- css元素水平垂直居中的十种方法
- 未知宽高元素水平垂直居中方法
- 块元素水平垂直居中方法汇总
- CSRF、AMD和水平垂直居中元素的方法
- 实现绝对定位元素水平垂直居中的两种方法
- CSS实现父级元素属性display为block的元素垂直和水平居中的三种方法
- css中已知宽高的子元素水平垂直居中的三种方法
- 元素,单行文字或多行文字,图片 之 水平垂直居中的方法大全及例子分析