您的位置:首页 > Web前端 > HTML

html中div内容居中的方法

2017-03-27 09:46 495 查看

一、div内容 居中的方法:

方法1:table-cell

div中的内容居中:不改变盒子尺寸。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
background-color: red;
width: 300px;
height: 300px;

/*此元素会作为一个表格单元格显示
(类似 <td> 和 <th>)*/
display: table-cell;
/*垂直居中 */
vertical-align: middle;
/*水平居中*/
text-align: center;
}
</style>
</head>

<body>
<div>
<!--<span>垂直居中</span>-->
<img src="../share_icon_sina_weibo.png" width="150" height="100"/>
</div>
</body>
</html>


方法2:display:flex

div中的内容居中:不改变盒子尺寸

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
background-color: red;
width: 300px;
height: 300px;

display: flex; /**/
justify-content: center; /*水平居中*/
align-items: Center; /*垂直居中*/
}
</style>
</head>

<body>
<div>
<img src="../share_icon_sina_weibo.png" width="150" height="100"/>
<!--<span>垂直居中</span>-->
</div>
</body>
</html>


方法3:绝对定位和负边距

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
background-color: red;
width: 300px;
height: 300px;

position: relative;
}

img {
position: absolute;
width: 150px;
height: 100px;
/*img左上角移动到html的中心*/
top: 50%;
left: 50%;
/*img左上角向左上角分别移动自身的一半距离*/
margin-left: -75px;
margin-top: -50px;
/*text-align: center;*/
}
</style>
</head>
<body>
<div>
<img src="../share_icon_sina_weibo.png"/>
<!--<span>垂直居中</span>-->
</div>
</body>
</html>


方法4:绝对定位和0

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
background-color: red;
width: 300px;
height: 300px;

position: relative;
}

img {
/*width: 50%;*/
/*height: 50%;*/
overflow: auto;
margin: auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
</style>
</head>
<body>
<div>
<img src="../share_icon_sina_weibo.png" width="150" height="100"/>
<!--<span>垂直居中</span>-->
</div>
</body>
</html>


这种方法跟上面的有些类似,但是这里是通过margin:auto和top,left,right,bottom都设置为0实现居中,很神奇吧。不过这里得确定内部元素的高度,可以用百分比,比较适合移动端。

方法5:translate

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
background-color: red;
width: 300px;
height: 300px;

position: relative;
}

img {
position: absolute;
width: 150px;
height: 100px;
/*img左上角移动到html的中心*/
top: 50%;
left: 50%;
/*img左上角向左上角分别移动自身的一半距离*/
transform: translate(-50%, -50%);
/*text-align: center;*/
}
</style>
</head>
<body>
<div>
<img src="../share_icon_sina_weibo.png"/>
<!--<span>垂直居中</span>-->
</div>
</body>
</html>


这实际上是方法3的变形,移位是通过translate来实现的。

方法六:display:inline-block

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
background-color: red;
width: 300px;
height: 300px;

text-align: center;
font-size: 0;
}

div:after {
content: '';
width: 0;
height: 100%;
display: inline-block;
vertical-align: middle;
}

img {
vertical-align: middle;
display: inline-block;
font-size: 16px;
}
</style>
</head>
<body>
<div>
<img src="../share_icon_sina_weibo.png" width="150" height="100"/>
<!--<span>垂直居中</span>-->
</div>
</body>
</html>


这种方法确实巧妙…通过:after来占位。

方法七:display:flex和margin:auto

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
background-color: red;
width: 300px;
height: 300px;

display: flex;
/*text-align: center;*/
}

img {
margin: auto;
}
</style>
</head>
<body>
<div>
<img src="../share_icon_sina_weibo.png" width="150" height="100"/>
<!--<span>垂直居中</span>-->
</div>
</body>
</html>


方法八:display:-webkit-box

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
background-color: red;
width: 300px;
height: 300px;

display: -webkit-box;
-webkit-box-pack: center;
-webkit-box-align: center;
-webkit-box-orient: vertical;
text-align: center
}

</style>
</head>
<body>
<div>
<img src="../share_icon_sina_weibo.png" width="150" height="100"/>
<!--<span>垂直居中</span>-->
</div>
</body>
</html>


效果图:



参考:

纯CSS实现垂直居中的几种方法

如何让一个div居于页面正中间【实现方法】

注意:

以上不会改变div的尺寸:

以下使用padding会改变div的尺寸:

方法一

利用text-align属性将图片水平居中,然后设置padding-top的值使其垂直居中。

<!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 name="author" type="Nancle from CAU CS101"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>如何让img标签在div里左右上下居中</title>
<script type="text/javascript">
</script>
<style type="text/css">
#container1 {
/*  300*150*/
width: 300px;
height: 150px;
background-color: cyan;
/*水平居中*/
text-align: center;
}

#container2 {
/*  300*150*/
width: 300px;
height: 150px;
background-color: cyan;
/*水平居中*/
text-align: center;
/*垂直居中*/
padding-top: 50px; /*div高度-img高度*/
}
</style>
</head>
<body>
<!--300*150-->
<div id="container1"><!--150*100-->
<img src="../share_icon_sina_weibo.png" width="150" height="100"/>
</div>
<br>
<br>
<br>
<!--300*150-->
<div id="container2"><!--150*100-->
<img src="../share_icon_sina_weibo.png" width="150" height="100"/>
</div>
</body>
</html>


方法二:

只用padding属性,通过计算求得居中

<!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 name="author" type="Nancle from CAU CS101"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>如何让img标签在div里左右上下居中</title>
<script type="text/javascript">
</script>
<style type="text/css">
#container {
/*  300*150*/
width: 300px;
height: 150px;
background-color: cyan;
}

#container1 {
/*  300*150*/
width: 300px;
height: 150px;
background-color: cyan;
/*水平居中*/
padding-left: 150px; /*div宽度-img宽度*/
}

#container2 {
/*  300*150*/
width: 300px;
height: 150px;
background-color: cyan;
/*水平居中*/
padding-left: 150px; /*div宽度-img宽度*/
/*垂直居中*/
padding-top: 50px; /*div高度-img高度*/
}
</style>
</head>
<body>
<div id="container"><!--150*100-->
<img src="../share_icon_sina_weibo.png" width="150" height="100"/>
</div>
<br/>
<br/>
<br/>
<div id="container1"><!--150*100-->
<img src="../share_icon_sina_weibo.png" width="150" height="100"/>
</div>
<br/>
<br/>
<br/>
<div id="container2"><!--150*100-->
<img src="../share_icon_sina_weibo.png" width="150" height="100"/>
</div>
<p>
</p>
</body>
</html>


方法三:

利用图片的margin属性将图片水平居中,利用DIV的padding属性将图片垂直居中。

Img是内联元素,要设置其margin属性使其居中,就要将其转换为块元素display:block;然后利用margin:0 auto;实现图片的水平居中。

<!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 name="author" type="Nancle from CAU CS101"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>如何让img标签在div里左右上下居中</title>
<script type="text/javascript">
</script>
<style type="text/css">
.div1 {
/*  300*150*/
width: 300px;
height: 150px;
background-color: cyan;
/*垂直居中*/
padding-top: 50px;
}

/* Img是内联元素,要设置其margin属性使其居中,
就要将其转换为块元素display:block;然后利用margin:0 auto;实现图片的水平居中*/
img {
/*background-color: red;*/
display: block;
/*水平居中*/
margin: 0 auto;
}
</style>
</head>

<body>
<div class="div1">
<img src="../share_icon_sina_weibo.png" width="150" height="100"/>
</div>
</body>
</html>


参考:

如何让图片在div中居中显示?

二、div盒子 居中的方法:

方法一:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>div居于页面正中间</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
background-color: #EAEAEA;
}

div {
width: 200px;
height: 200px;
background-color: #1E90FF;
/*--------------------------文字居中方法--------------------------*/
text-align: center; /*水平居中*/
line-height: 200px; /*垂直居中:div的全高度*/

/*--------------------------div居中方法--------------------------*/
/*使div脱离文档流*/
position: absolute;
/*左上角位置*/
top: 50%; /*div下移父容器的一半高度*/
left: 50%; /*div右移父容器的一半宽度*/
/*中心矫正:上移div自身一半高度,左移div自身一半宽度*/
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
</style>

</head>

<body>
<div>
我在中间
<!--<img src="../share_icon_sina_weibo.png" width="150" height="100"/>-->
</div>
<!--
使文字处于div中心方法:
先设置一个div的宽高,使用text-align水平居中;
再使用line-height上下居中即可(line-height设置的高度需跟div的高度一致)
-->
</body>

</html>


方法二:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>div居中方法</title>
<style>
/*先要设置div元素的祖先元素html和body的高度为100%(因为他们默认是为0的),
并且清除默认样式,即把margin和padding设置为0(如果不清除默认样式的话,浏览器就会出现滚动条*/
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}

div {
width: 300px;
height: 300px;
background: orange;
position: relative; /*相对父容器*/
/*-----------------------div居中-----------------------*/
/*水平居中*/
margin: 0 auto; /*水平居中*/
/*垂直居中*/
top: 50%; /*垂直偏移:父元素的一半高度*/
margin-top: -150px; /*垂直矫正:上移自身一半高度*/
/*---------------------文字居div中间----------------------*/
text-align: center;
line-height: 300px;
}
</style>
</head>

<body>
<div>我在中间</div>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: