本文主要介紹了css常用的布局,居中等其他小技巧。
css布局
左右布局
利用float實(shí)現(xiàn)左右布局
左右模塊設(shè)置float:left;,并且設(shè)置寬度,保證左右模塊加起來的和等于100%,父級利用clear清除浮動(dòng)。
html代碼
<div class="wrap clearfix">
<div class="left-box">
這是左邊
</div>
<div class="right-box">
這是右邊
</div>
</div>
css代碼
.clearfix::after {
content: "";
display: block;
clear: both;
}
.wrap {
padding: 10px;
background: #ccc;
}
.left-box {
float: left;
background: #eaa;
font-size: 30px;
width: 40%;
text-align: center;
height: 300px;
}
.right-box {
float: right;
background: #6a8;
font-size: 30px;
width: 60%;
text-align: center;
height: 300px;
}
利用flex實(shí)現(xiàn)左右布局
利用css3的彈性布局盒子,實(shí)現(xiàn)左右布局。直接在父元素上設(shè)置display:flex;即可。
html代碼
<div class="wrap">
<div class="left-box">
這是左邊
</div>
<div class="right-box">
這是右邊
</div>
</div>
css代碼
.wrap {
display: flex;
padding: 10px;
background: #ccc;
}
.left-box {
background: #eaa;
font-size: 30px;
width: 40%;
text-align: center;
height: 300px;
}
.right-box {
background: #6a8;
font-size: 30px;
width: 60%;
text-align: center;
height: 300px;
}
若其中有一個(gè)模塊為寬度為定寬,那么只需要在另一個(gè)模塊上設(shè)置flex: 1;即可。
左中右布局
實(shí)現(xiàn)左中右布局,可以使用float,inline-blok,flex等多種方式來實(shí)現(xiàn)。
利用float實(shí)現(xiàn)
利用float實(shí)現(xiàn)左中右布局,原理跟上文的左右布局類似,只需要添加一個(gè)中間模塊即可。
html 代碼
<div class="wrap clearfix">
<div class="left-box">
這是左邊
</div>
<div class="middle-box">
這是中間
</div>
<div class="right-box">
這是右邊
</div>
</div>
css代碼
.clearfix::after {
content: "";
display: block;
clear: both;
}
.wrap {
display: block;
padding: 10px;
background: #ccc;
}
.left-box {
float: left;
width: 30%;
background: #eaa;
font-size: 30px;
text-align: center;
height: 300px;
}
.middle-box {
float: left;
width: 40%;
background: #a8a;
font-size: 30px;
text-align: center;
height: 300px;
}
.right-box {
float: left;
width: 30%;
background: #6a8;
font-size: 30px;
text-align: center;
height: 300px;
}
利用inline-block實(shí)現(xiàn)左中右居中
理由inline-block實(shí)現(xiàn)左中右布局,有一個(gè)細(xì)節(jié)要注意一下,需要在父節(jié)點(diǎn)上面設(shè)置font-size: 0;除去標(biāo)簽之間回車引起的空格。還有就是圣杯布局,兼容性好一點(diǎn)。
html帶代碼
<div class="wrap">
<div class="left-box">
這是左邊
</div>
<div class="middle-box">
這是中間
</div>
<div class="right-box">
這是右邊
</div>
</div>
css代碼
.wrap {
display: block;
padding: 10px;
background: #ccc;
font-size: 0;
}
.left-box {
display: inline-block;
width: 30%;
background: #eaa;
font-size: 30px;
text-align: center;
height: 300px;
}
.middle-box {
display: inline-block;
width: 40%;
background: #a8a;
font-size: 30px;
text-align: center;
height: 300px;
}
.right-box {
display: inline-block;
width: 30%;
background: #6a8;
font-size: 30px;
text-align: center;
height: 300px;
}
flex實(shí)現(xiàn)左中右布局
原理如上文提到的左右布局相似,只需要多添加一個(gè)模塊即可。
圣杯布局
圣杯布局是為了兼容老版本的ie瀏覽器使用的手段。功能是左右固定寬度,中間可變。相對來說,圣杯布局的復(fù)雜度高一點(diǎn),但只要理解了,也就很容易搞清楚。
htm代碼
<div class="wrap clearfix">
<div class="middle box">
<div class="margin-inner">
這是中間
</div>
</div>
<div class="left box">
這是左邊
</div>
<div class="right box">
這是右邊
</div>
</div>
css代碼
.clearfix::after {
content: "";
display: block;
clear: both;
}
.box {
float: left;
min-height: 300px;
font-size: 30px;
text-align :center;
}
.wrap {
padding: 10px;
display: block;
background: #ccc;
font-size: 0;
}
.left {
margin-left: -100%;
width: 200px;
background: #eaa;
}
.middle {
width: 100%;
}
.middle .margin-inner {
margin: 0 220px 0 200px;
background: #a8a;
min-height: 200px;
}
.right {
margin-left: -220px;
width: 220px;
background: #6a8;
}
居中
水平居中
margin實(shí)現(xiàn)塊級元素水平居中
給設(shè)置好寬度的塊級元素設(shè)置的margin: 0 auto;實(shí)現(xiàn)水平居中
inline-block實(shí)現(xiàn)水平居中
給元素設(shè)置display: inline-block;,設(shè)置父級元素的屬性width: 100%;text-align: center;
flex實(shí)現(xiàn)水平居中
父級元素設(shè)置display: flex;justify-content: center;即可
html代碼
<div class="wrap">
<div class="box">
第一行文字<br>
第一行文字<br>
第一行文字<br>
第一行文字
</div>
css代碼
.wrap {
display: flex;
justify-content: center;
height: 600px;
font-size: 30px;
background: #ccc;
font-size: 0;
}
.box {
display: inline-block;
padding: 10px;
vertical-align: middle;
font-size: 30px;
}
垂直居中
利用padding或line
- 可以設(shè)置上下
padding -
line-height和height相等實(shí)現(xiàn)。
利用display: table
父級元素設(shè)置display: table;,子元素設(shè)置display: table-cell;vertical-align: middle;
html代碼
<div class="wrap">
<div class="box">
第一行文字<br>
第一行文字<br>
第一行文字<br>
第一行文字
</div>
</div>
css代碼
.wrap {
display: table;
height: 600px;
font-size: 30px;
text-align:center;
}
.box {
padding: 10px;
display: table-cell;
vertical-align: middle;
background: #ccc;
font-size: 30px;
}
利用偽元素實(shí)現(xiàn)
設(shè)置偽元素高度100%,寬度為0,然后利用垂直對齊屬性vertical-align: middle;實(shí)現(xiàn)垂直居中。
html代碼
<div class="wrap">
<div class="box">
第一行文字<br>
第一行文字<br>
第一行文字<br>
第一行文字
</div>
</div>
css代碼
.wrap {
display: block;
height: 600px;
font-size: 30px;
background: #ccc;
font-size: 0;
}
.wrap::before {
content: '';
display: inline-block;
vertical-align: middle;
width: 0;
height: 100%;
}
.box {
padding: 10px;
display: inline-block;
vertical-align: middle;
font-size: 30px;
}
注意細(xì)節(jié),需要把父級元素font-size設(shè)置為0。如果想要兼容ie8及以下,需要把::before改為:before。
利用定位給已知高度的元素設(shè)置垂直居中
父級元素設(shè)置position: relative;,子元素設(shè)置positon:absolute;,在利用margin實(shí)現(xiàn)垂直居中
html代碼
<div class="wrap">
<div class="box"></div>
</div>
css代碼
.wrap {
position: relative;
height: 600px;
font-size: 30px;
background: #ccc;
font-size: 0;
}
.box {
position: absolute;
top: 50%;
margin-top: -90px;
width: 150px;
height: 180px;
font-size: 30px;
background: #987;
}
利用flex實(shí)現(xiàn)垂直居中
父級元素設(shè)置flex和align-items: center;即可
html代碼
<div class="wrap">
<div class="box"></div>
</div>
css代碼
.wrap {
display: flex;
align-items: center;
height: 600px;
font-size: 30px;
background: #ccc;
font-size: 0;
}
.box {
top: 50%;
width: 150px;
height: 180px;
font-size: 30px;
background: #987;
}
其他小技巧
使用margin實(shí)現(xiàn)水平和垂直同時(shí)居中。
給元素設(shè)置絕對定位,左右上下距離設(shè)置為0,再利用margin: auto;使得上下左右距離均勻分配。
html代碼
<div class="box"></div>
css代碼
.box {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
width: 150px;
height: 180px;
font-size: 30px;
background: #987;
}
文字間距
寫頁面時(shí),經(jīng)常遇到需要擴(kuò)大文字間間距的情況,尤其是標(biāo)題,如果用空格去填充那就太麻煩了,代碼也不夠優(yōu)雅。好在css提供了擴(kuò)大文字間距的屬性letter-spacing。

但是由于最后一個(gè)字符后也跟了一個(gè)間距,導(dǎo)致文字并沒有完全居中。那么現(xiàn)在要使用
css提供的第二個(gè)間距相關(guān)的屬性text-indent,可以控制首行文本前的間距。設(shè)置與letter-spacing的值相同即可。
代碼如下:
letter-spacing: 0.5em;
text-indent: 0.5em;