這篇文章給大家介紹CSS中怎么實(shí)現(xiàn)水平、垂直居中,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

首先我先創(chuàng)建一個(gè)公共的模板樣式
<template>
<div class="two">
<div class="parent">
<div class="child">123</div>
</div>
</div>
</template>
<style lang="less" scoped>
.parent{
margin: 0 auto;
width: 300px;
height: 300px;
border: 1px solid red;
box-sizing: border-box;
.child {
height: 100px;
width: 100px;
background: #2f8df0;
}
}
</style>然后具體用到的樣式單獨(dú)寫在方法里面,首先先介紹4個(gè)平時(shí)布局的技巧。
1.水平居中div里面的div,設(shè)置子元素的寬度。
.parent{
width:300px;
margin:0 auto;
}注意:如果子元素設(shè)置了display:table-cell,那么margin:0 auto;將會(huì)失效。
2.設(shè)置文字垂直居中,設(shè)置包含文字div的高。
.child{
text-align:center
line-height:100px //知道子元素的高,設(shè)置和高一樣的高度
}3.兩個(gè)或者多個(gè)塊級(jí)元素垂直居中對(duì)齊,父元素設(shè)置height和line-height相等。
.parent{
line-height: 300px; //知道父元素的高,設(shè)置和高一樣的高度
}
.child1{
display: inline-block;
vertical-align: middle;
line-height: initial; //initial 關(guān)鍵字用于設(shè)置 CSS 屬性為它的默認(rèn)值。
}
.child2{
display: inline-block;
vertical-align: middle;
line-height: initial; //initial 關(guān)鍵字用于設(shè)置 CSS 屬性為它的默認(rèn)值。
}4.讓一個(gè)元素充滿當(dāng)前整個(gè)容器,設(shè)置absolute
.parent{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}OK,介紹完畢,下面開始介紹CSS實(shí)現(xiàn)水平垂直居中的方式。
1.不需要設(shè)置子元素的寬高,需要設(shè)置父元素的高度。使用absolute + transform (推薦)
.parent{
position: relative
}
.child{
position: absolute;
top:50%;
left:50%;
transform:translate(-50%,-50%)
}
// 備注一下,如果只需要上下居中那就只要保留top,只要左右居中的話就保留left,translate設(shè)置
translateY(-50%)或者translateX(-50%)2.不需要設(shè)置子元素的寬高,不需要設(shè)置父元素的寬高。 使用flex布局(建議移動(dòng)端直接使用flex
pc端看需要兼容的情況。)
.parent{
display:flex;
align-items:center;
justify-content:center;
}
.child{
}3.不需要設(shè)置子元素的寬高,需要設(shè)置父元素的高度。使用lineheight。
注意:這種方法需要通過text-align在子元素中將文字顯示重置為想要的效果
.parent{
line-height: 300px; //設(shè)置和父元素的高度一樣
text-align: center;
}
.child{
display: inline-block;
vertical-align: middle;
line-height: initial; //initial 關(guān)鍵字用于設(shè)置 CSS 屬性為它的默認(rèn)值。
text-align: left; //將文字顯示重置為想要的效果
}4.不需要設(shè)置子元素的寬高,需要設(shè)置父元素的高度。使用css-table (使用之后此元素的margin:0 auto會(huì)失效)
.parent{
display: table-cell;
vertical-align: middle;
text-align: center;
}
.child{
display: inline-block;
}5.設(shè)置子元素的寬高,設(shè)置父元素的高度。 使用absolute + 負(fù)margin
.parent{
position: relative
}
.child{
position: absolute;
top: 50%;
left: 50%;
margin-left: -50px; //知道子元素的寬高
margin-top: -50px; //知道子元素的寬高
}6.設(shè)置子元素的寬高,設(shè)置父元素的高度。使用absolute + margin auto
.parent{
position: relative
}
.child{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}7.設(shè)置子元素的寬高,設(shè)置父元素的高度。 使用absolute + calc(這種方法兼容性依賴calc的兼容性)
.parent{
position: relative
}
.child{
position: absolute;
top: calc(50% - 50px);
left: calc(50% - 50px);
}8.使用writing-mode(使用起來比較復(fù)雜,不推薦)
//公共的樣式在最上面 <div class="parent"> <div class="box-child"> <div class="child">123</div> </div> </div>
.parent{
writing-mode: vertical-lr; //改變文字顯示的方向
text-align: center;
}
.box-child{
writing-mode: horizontal-tb;
display: inline-block;
text-align: center;
width: 100%;
}
.child{
text-align: left; //將文字顯示重置為想要的效果
margin: 0 auto;
}9.不需要設(shè)置子元素的寬高,不需要設(shè)置父元素的寬高。 使用grid布局(不建議使用,目前兼容性不是很好)
.parent{
display: grid;
}
.child{
align-self: center;
justify-self: center;
}10.使用table布局(純粹湊方法,這年頭,誰(shuí)還用table布局呀,哈哈哈哈)
<table> <tbody> <tr> <td class="parent"> <div class="child">123</div> </td> </tr> </tbody> </table>
.parent{
text-align: center;
}
.child{
display: inline-block;
}關(guān)于CSS中怎么實(shí)現(xiàn)水平、垂直居中就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)站名稱:CSS中怎么實(shí)現(xiàn)水平、垂直居中-創(chuàng)新互聯(lián)
分享URL:http://www.chinadenli.net/article10/dedogo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、做網(wǎng)站、小程序開發(fā)、微信小程序、網(wǎng)站收錄、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容
移動(dòng)網(wǎng)站建設(shè)知識(shí)