SS 是前端里面的基礎(chǔ)之一,也是非常重要的一部分,它往往決定了你所做出來的網(wǎng)頁頁面是否美觀。在設(shè)計(jì)網(wǎng)頁頁面的過程中,總會(huì)有將元素或者文字進(jìn)行水平垂直居中的要求。下面w3cschool編程獅就為大家介紹 CSS 中幾種常用到的水平垂直居中的方法。
當(dāng)元素有給定的高度以及寬度的時(shí)候,使用 margin: auto; 元素僅會(huì)水平居中,并不會(huì)進(jìn)行垂直居中。此時(shí)就需要設(shè)置元素的 position 為 absolute,父級元素的 position 為 relative,同時(shí)元素的上下左右都需要設(shè)置為 0。
HTML 代碼
<div class="box">
<div class="center1"></div>
</div>
CSS 代碼
.box{
width: 200px;
height: 200px;
background-color: #eee;
position: relative;
margin-top: 20px;
}
.center1{
width: 50px;
height: 50px;
background-color: #00ACED;
margin: auto;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
效果展示:
當(dāng)已經(jīng)知道了要進(jìn)行水平垂直居中的元素的寬高時(shí),就可以通過設(shè)置 position: absolute 來實(shí)現(xiàn)。但是,使用的同時(shí)還需要結(jié)合其他屬性才完整實(shí)現(xiàn)。因?yàn)椋瑔问窃O(shè)置 absolute,上左距離均為一半,就會(huì)出現(xiàn)下面這種情況。很顯然可以看到,元素并不是完全居中,僅只有左上角的位置在中心點(diǎn)
概念圖:
因此想要實(shí)現(xiàn)元素完全水平垂直居中,在設(shè)置了 absolute 定位后,可以設(shè)置 margin 值為負(fù),或者使用 calc 來計(jì)算,上左距離在 50% 的基礎(chǔ)上還要減去元素本身一半的寬高。
margin 值為負(fù)或者 calc 計(jì)算均是在已知元素寬高的情況下,假設(shè)不知道元素的寬高,那么怎么實(shí)現(xiàn)水平垂直居中呢?這里就可以使用 transform 屬性,通過坐標(biāo)位移來實(shí)現(xiàn)居中。
CSS 代碼
/* 結(jié)合 margin */
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: 50%;
top: 50%;
margin-left: -25px;
margin-top: -25px;
}
/* 結(jié)合 calc 計(jì)算*/
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: calc(50% - 25px)
top: calc(50% - 25px);
}
/* 結(jié)合 transform */
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
效果展示
03
PART
可以通過彈性布局來設(shè)置水平垂直居中,這里需要設(shè)置父級元素 display:flex; 還需要設(shè)置兩個(gè)屬性,水平布局 justify-content 以及垂直布局 align-items。
HTML代碼
<div class="box2">
<div class="center4"></div>
</div>
CSS代碼:
.box2{
background-color: #eee;
width: 200px;
height: 200px;
position: relative;
margin-top: 20px ;
display: flex;
justify-content: center;
align-items: center;
}
.center4{
width: 50px;
height: 50px;
background-color: #B39873;
}
效果展示:
前面介紹的是元素如何實(shí)現(xiàn)水平垂直居中,下面介紹的是如何將文字進(jìn)行水平垂直居中。這第一個(gè)方法也是最經(jīng)常用的,使用文本水平對齊 text-align 和行高 line-height 來實(shí)現(xiàn)的。
HTML 代碼
<div class="box3">
<div class="center5">文字居中</div>
</div>
CSS 代碼
.box3{
background-color: #eee;
width: 200px;
height: 200px;
margin-top: 20px;
}
.center5{
text-align: center;
line-height: 200px;
}
效果展示
05
PART
第二個(gè)方法可以通過網(wǎng)格布局 grid 來實(shí)現(xiàn)。而這里通過 grid 有兩種方式實(shí)現(xiàn),一種對元素本身屬性進(jìn)行設(shè)置,另一種在元素的父級元素中設(shè)置。兩者看上去內(nèi)容似乎差不多,不同的是在元素中設(shè)置的是 align-self 還要多了一個(gè) margin,父級元素中是 align-items。
相關(guān)代碼:
/* grid 元素中設(shè)置 */
.box4{
background-color: #eee;
width: 200px;
height: 200px;
margin-top: 20px;
display: grid;
}
.center6{
align-self: center;
justify-content: center;
margin: auto;
}
/* grid 父級元素中設(shè)置 */
.box5{
background-color: #eee;
width: 200px;
height: 200px;
margin-top: 20px;
display: grid;
align-items: center;
justify-content: center;
}
效果展示:
以上就是關(guān)于 CSS 如何將元素或者文字進(jìn)行水平垂直居中的幾種常用方法,大家還其他關(guān)于 CSS 實(shí)現(xiàn)水平垂直居中的方法嗎?請?jiān)谠u論區(qū)留下你的想法。
關(guān)注w3cschool編程獅訂閱更多IT資訊、技術(shù)干貨~
中,是我們編碼過程中最常見的,那么,我們平時(shí)常見的居中方式,下面一一羅列出來,有錯(cuò)誤的地方,望碼友多多包涵并加以矯正。
水平居中
1、多塊級元素,設(shè)置display:inline-block;使之在一行排列,在父級樣式里,設(shè)置text-align:center;就可以實(shí)現(xiàn)水平居中的效果
body {
text-align: center;
}
div{
width: 100px;
height: 100px;
border: 1px solid;
display: inline-block;
}
2、內(nèi)聯(lián)元素,利用text-align:center;可以實(shí)現(xiàn)塊級元素內(nèi)部的內(nèi)聯(lián)元素的水平居中
div {
border: 1px solid red;
width: 100px;
height: 100px;
text-align: center;
}
<div>
<span>塊級元素中的行內(nèi)元素的水平居中</span>
</div>
3、塊級元素,通過把固定寬高的塊級元素的margin-left和margin-right設(shè)置為auto,方可實(shí)現(xiàn)
div{
width: 100px;
height: 100px;
border: 1px solid;
margin: 0 auto;
}
<div></div>
4、利用彈性盒子(display: flex;)
給父級定寬定高,然后設(shè)置display: flex;以及justify-content: center;方可達(dá)到水平居中效果
body {
width: 500px;
height: 500px;
display: flex;
justify-content: center;
border: 1px solid red;
}
div {
width: 100px;
height: 100px;
border: 1px solid;
}
<body>
<div></div>
</body>
垂直居中
1、內(nèi)聯(lián)元素(單行)
通過設(shè)置元素的height和line-height,方可達(dá)到居中效果
2、多行元素,利用表布局(table)
通過給想要居中的元素的父級設(shè)置display: talbe-cell;以及vertical-align:enter;方可居中
3、彈性盒子(flex)
給父級設(shè)置display: flex;變成彈性盒子。
分兩種,
(1),主軸方向?yàn)樗剑苯釉O(shè)置 align-items: center;
(2),主軸方向?yàn)榇怪保O(shè)置flex-direction: column;改變主軸方向,然后設(shè)置justify-content: center;
彈性盒模型主軸不同,居中的方式也不同,靈活應(yīng)用。
4、固定寬高的塊級元素
利用父相子絕的定位原理,實(shí)現(xiàn)垂直居中
position: absolute;
left: 50%;
top: 50%;
margin-left: (自身高度的一半);
5,未知寬高的塊級元素
利用transform: translateY(-50%);方可實(shí)現(xiàn)
position: absolute;
top: 50%;
transform: translateY(-50%);
水平垂直方向的居中
1、固定寬高
通過margin平移整體寬高的一半,實(shí)現(xiàn)水平垂直居中
{
position: absolute;
width: 100px;
height: 100px;
border: 1px solid;
left: 50%;
top: 50%;
margin-top: -50px;
margin-left: -50px;
}
2、未知寬高
利用transform中的translate()屬性實(shí)現(xiàn)
{
position: absolute;
border: 1px solid;
left: 50%;
top: 50%;
transform: translateY(-50%);
transform: translateX(-50%);
}
3、彈性盒子(flex)
通過display:flex,把父級變成彈性盒模型,利用align-items: center;justify-content: center;方可實(shí)現(xiàn)居中。
注意:彈性盒子容器中,多行項(xiàng)目的居中方式另加計(jì)算。
body {
border: 1px solid;
width: 300px;
height: 300px;
position: relative;
display: flex;
align-items: center;
justify-content: center;
}
div {
border: 1px solid;
width: 100px;
height: 100px;
}
隨著學(xué)習(xí)的不斷深入,居中方式可以有很多種,我們要善于利用,更加明確什么情況下用怎樣的居中方式。
今天的內(nèi)容中,廊坊網(wǎng)站建設(shè)公司的技術(shù)人員將分享下網(wǎng)站導(dǎo)航水平居中設(shè)置的方法,其實(shí)辦法有很多,以CSS代碼為例,這種代碼操作很方便,后期修改的時(shí)候也簡單,以下是詳細(xì)的實(shí)例操作。
方法一:使用display:inline-block控制
這個(gè)方法比較簡單,是將容器轉(zhuǎn)成display:inline-block行內(nèi)塊級元素,然后就可以直接用text-align:center使其達(dá)到水平居中效果。HTML代碼中需要一個(gè)div來包圍這個(gè)導(dǎo)航菜單。
方法二:使用position:relative解決
position:relative定位方法來讓元素水平居中,一般來說小編推薦這方法,因?yàn)榇a多了個(gè)div去包住,當(dāng)然這些是根據(jù)情況來使用的。將定位div設(shè)為浮動(dòng),再定位left:50%,然后導(dǎo)航定位至left:-50%。
方法三:使用display:table解決
HTML代碼實(shí)例
<ul class="navbar">
<li><a href="/">Home</a></li>
</ul>
CSS代碼實(shí)例
.navbar {
display:table;
margin:0 auto;
}
.navbar li {
display:table-cell;
}
.navbar li + li {
padding-left:20px;
}
方法四:使用display:inline-flex解決
HTML代碼實(shí)例操作
<div class="navbar">
<ul>
<li><a href="/">Home</a></li>
</ul>
</div>
CSS代碼代碼編寫
.navbar {
text-align:center;
}
.navbar > ul {
display:-webkit-inline-box;
display:-moz-inline-box;
display:-ms-inline-flexbox;
display:-webkit-inline-flex;
display:inline-flex;
}
.navbar li + li {
margin-left:20px;
}
提示:瀏覽器兼容問題,目前這個(gè)代碼不支持IE7及以下版本的IE瀏覽器。
其實(shí)這些方法很好的解決了網(wǎng)站導(dǎo)航菜單居中的問題,使用CSS編寫有助于后期的修改,極大的方便了我們的操作和節(jié)省了寶貴的時(shí)間。在以后的內(nèi)容中,小編還將會(huì)繼續(xù)為朋友們分享更多更有價(jià)值的知識(shí)。
原創(chuàng)文章出自暢想網(wǎng)絡(luò),轉(zhuǎn)載地址:http://www.e-wkj.cn/xw/2316.html
*請認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。