標籤:play title orm for nbsp item type char flex
一、在需要置中的元素加上如下C3屬性即可:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.div{
height: 330px;
width: 330px;
background-color: mediumspringgreen;
border: 6px solid lightcoral;
text-align: center;
line-height: 330px;
position: fixed;
top: 50%;
left: 50%;
-webkit-transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-ms-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
}
</style>
</head>
<body>
<div class="div">上下左右置中</div>
</body>
</html>
二、只要在父級元素上面加上這三句話就可以實現不定寬高水平垂直置中:
justify-content:center;//子項目水平置中align-items:center;//子項目垂直置中display:-webkit-flex;
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.father{
width: 900px;
height: 900px;
background-color: aqua;
justify-content: center;
align-items: center;
display: -webkit-flex;
}
.div{
height: 330px;
width: 330px;
background-color: mediumspringgreen;
border: 6px solid lightcoral;
text-align: center;
line-height: 330px;
/*position: fixed;*/
/*top: 50%;*/
/*left: 50%;*/
/*-webkit-transform: translateX(-50%) translateY(-50%);*/
/*-moz-transform: translateX(-50%) translateY(-50%);*/
/*-ms-transform: translateX(-50%) translateY(-50%);*/
/*transform: translateX(-50%) translateY(-50%);*/
}
</style>
</head>
<body>
<div class="father">
<div class="div">上下左右置中</div>
</div>
</body>
</html>
水平垂直置中div(css3)