2019-07-11div居中

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
/常见的居中/
.father{
width: 200px;
height: 200px;
background-color: red;
margin-bottom: 100px;
position: relative;
}
.child{
width: 100px;
height: 100px;
background-color: blue;
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
margin: auto;
}
/负边距居中/
.father2{
width: 200px;
height: 200px;
background-color: red;
margin-bottom: 100px;
position: relative;
}
.child2{
width: 100px;
height: 100px;
background-color: blue;
text-align: center;
position: absolute;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
}
/弹性布局居中(未知宽高也可以)/
.father3{
width: 200px;
height: 200px;
background-color: red;
margin-bottom: 100px;
position: relative;
display: flex;
align-items: center;
justify-content: center;
}
.child3{
width: 100px;
height: 100px;
background-color: blue;
}
/左右居中/
.father4{
width: 200px;
height: 200px;
background-color: red;
margin-bottom: 100px;
position: relative;
text-align: center;
}
.child4{
width: 100px;
height: 100px;
background-color: blue;
display: inline-block;
}
/很巧妙的方法/
.father5{
width: 200px;
height: 200px;
background-color: red;
margin-bottom: 100px;
position: relative;
text-align: center;
}
.child5{
width: 100px;
height: 100px;
background-color: blue;
display: inline-block;
top:50%;
left:50%;
transform:translate(-50%,-50%);
position: absolute;
}

</style>
<body>
<div class="father">
<div class="child"></div>
</div>
<div class="father2">
<div class="child2">
</div>
</div>
<div class="father3">
<div class="child3">
</div>
</div>
<div class="father4">
<div class="child4">
</div>
</div>
<div class="father5">
<div class="child5">
</div>
</div>

</body>
</html>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 各种纯css图标 CSS3可以实现很多漂亮的图形,我收集了32种图形,在下面列出。直接用CSS3画出这些图形,要比...
    剑残阅读 13,241评论 0 8
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,981评论 1 45
  • 1. tab列表折叠效果 html: 能源系统事业部 岗位名称: 工作地点 岗位名...
    lilyping阅读 5,882评论 0 1
  • Square #square{ width: 100px; height: 100px; background: ...
    情话_2ee5阅读 2,656评论 0 1
  • 词:三月寒 我们都忘了当初的所想 谁又在乎他是不是在发光 躲在黑暗角落的彷徨 抹杀了岁月对它的向往 我也不会跟在你...
    子栖三月寒阅读 1,202评论 0 2