首先編寫一個簡單的html代碼,設置一個父div類名爲boxFather,再設置一個子div類名爲box1。html代碼以下:html
<div class="boxFather"> <div class="box1"></div> </div>
下面使用div盒子裏面要有內容撐開,html代碼以下:web
<div class="boxFather">
<div class="box1">
測試內容
</div>
</div>
1.div居中的方法測試
(1)父div不定寬高,子div定寬高flex
第一種方式:spa
<style> .box1{ background-color: red; width: 300px; height: 200px; margin: 0 auto; } </style>
第二種方式:code
<style> .boxFather{ position: relative; } .box1{ width: 300px; height: 200px; background-color: red; position: absolute; left: 50%; -webkit-transform: translate(-50%); -moz-transform: translate(-50%); -ms-transform: translate(-50%); -o-transform:translate(-50%) ; transform:translate(-50%); } </style>
第三種方式:orm
<style> .boxFather{ display: flex; justify-content: center; } .box1{ background-color: red; width: 300px; height: 200px; } </style>
第四種方法:htm
<style> .boxFather{ text-align: center; } .box1{ display: inline-block; background-color: red; width: 300px; height: 200px; } </style>
第五種方法:blog
<style> .boxFather{ position: relative; } .box1{ position: absolute; left:0; top:0; right:0; bottom:0; margin:auto; background-color: red; width: 300px; height: 200px; } </style>
第六種:it
<style> .boxFather{ position: relative; } .box1{ position: absolute; left:50%; top:50%; width: 300px; height: 200px; margin-left: -150px; margin-top: -100px; background-color: red; } </style>
第七種:
<style> .boxFather{ display: flex; justify-content:center; } .box1{ width: 300px; height: 200px; background-color: red; } </style>
(2)父div不定寬高,子div不定寬高 。(注意div盒子裏面要有內容撐開)
第一種方法:
<style> .boxFather{ position: relative; } .box1{ background-color: red; position: absolute; left: 50%; -webkit-transform: translate(-50%); -moz-transform: translate(-50%); -ms-transform: translate(-50%); -o-transform:translate(-50%) ; transform:translate(-50%); } </style>
第二種方法:
<style> .boxFather{ display: flex; justify-content:center; } .box1{ background-color: red; } </style>
2.div垂直居中
(1)父div不定寬高,子div不定寬高
第一種方法:
<style> .boxFather{ display: flex; justify-content:center; align-items: center; height: 200px; border:1px solid #ccc; } .box1{ background-color: red; } </style>
(2)父div定高,子div定寬高
第一種方法:
<style> .boxFather{ position: relative; height: 500px; border:1px solid #ccc; } .box1{ width: 300px; height: 200px; background-color: red; position: absolute; top: 50%; left: 50%; -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>
第二種方法:
<style> .boxFather{ } .box1{ position: fixed; left:0; top:0; right:0; bottom:0; margin:auto; background-color: red; width: 300px; height: 200px; } </style>
(3)父div定寬高,子div不定寬高(須要內容撐開)
第一種方法:
<style> .boxFather{ width: 300px; height: 200px; background-color: red; display: table-cell; vertical-align: middle; text-align: center; } .box1{ display: inline-block; } </style>
第二種方法:
<style> .boxFather{ display: flex; justify-content:center; align-items: center; height: 200px; border:1px solid #ccc; } .box1{ background-color: red; } </style>
3.div垂直
文字垂直,html代碼以下:
<div class="boxFather">
測試內容
</div>
第一種方法:
.boxFather{ height: 100px; line-height: 100px; background-color: red; border: 1px solid #ccc; }
第二種方法:
.boxFather{ height: 100px; background-color: red; display:table-cell; vertical-align:middle; }
div垂直的內容可複用‘div垂直居中’的內容是內容垂直
記錄一下,之後還會陸續添加。