|
導(dǎo)讀網(wǎng)頁的本質(zhì)就是超級(jí)文本標(biāo)記語言,通過結(jié)合使用其他的Web技術(shù)(如:腳本語言、公共網(wǎng)關(guān)接口、組件等),可以創(chuàng)造出功能強(qiáng)大的網(wǎng)頁。因而,超級(jí)文本標(biāo)記語言是萬維網(wǎng)(Web)編程的基礎(chǔ),也就是說萬維網(wǎng)是建立... 網(wǎng)頁的本質(zhì)就是超級(jí)文本標(biāo)記語言,通過結(jié)合使用其他的Web技術(shù)(如:腳本語言、公共網(wǎng)關(guān)接口、組件等),可以創(chuàng)造出功能強(qiáng)大的網(wǎng)頁。因而,超級(jí)文本標(biāo)記語言是萬維網(wǎng)(Web)編程的基礎(chǔ),也就是說萬維網(wǎng)是建立在超文本基礎(chǔ)之上的。超級(jí)文本標(biāo)記語言之所以稱為超文本標(biāo)記語言,是因?yàn)槲谋局邪怂^“超級(jí)鏈接”點(diǎn)。 網(wǎng)頁CSS的垂直居中需求始終沒有停過,而其困難度也始終沒有讓人輕松過,經(jīng)過了每位開發(fā)先烈的研究后,據(jù)說CSS的垂直居中技巧已達(dá)到近十種之多,但始終鮮為人知,部分公司甚至將CSS的垂直居中技巧當(dāng)成面試題,其重要性可見一斑,今天就帶著大家了解一下CSS的垂直居中的多種方式吧。1、Line-height 適用情景:單行文字垂直居中技巧 這個(gè)方式應(yīng)該是最多人知道的了,常見于單行文字的應(yīng)用,像是按鈕這一類對(duì)象,或者是下拉框、導(dǎo)航此類元素最常見到的方式了。此方式的原理是在于將單行文字的行高設(shè)定后,文字會(huì)位于行高的垂直中間位置,利用此原理就能輕松達(dá)成垂直居中的需求了。 <div class="content">Lorem ipsam.</div>
.content{
width: 400px;
background: #ccc;
line-height:100px;
margin: auto;
}2、Line-height + inline-block 適用情景:多對(duì)象的垂直居中技巧 既然可以使用第一種方式對(duì)行元素達(dá)成垂直居中的話,當(dāng)然沒有理由不能做到多行啊~但是你需要將多個(gè)元素或多行元素當(dāng)成一個(gè)行元素來看待,所以我們必須要將這些數(shù)據(jù)多包一層,并將其設(shè)定為inline-block,并在該inline-block對(duì)象的外層對(duì)象使用inline-block來代替height的設(shè)置,如此便可以達(dá)到垂直居中的目的了,從使你的數(shù)據(jù)是包含了標(biāo)題跟內(nèi)容在內(nèi)也可以正常的垂直居中了。 <div class="box box2">
<div class="content">
立馬來看Amos實(shí)際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
CSS3精美相冊(cè)效果 </a>
效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div>
</div>
h2{
text-align: center;
}
.box{
width: 500px;
border: 1px solid #f00;
margin: auto;
line-height: 200px;
text-align: center;
}
.box2 .content{
display: inline-block;
height: auto;
line-height:1;
width: 400px;
background: #ccc;
}3、:before + inline-block 適用情景:多對(duì)象的CSS垂直居中技巧 :before 偽類元素搭配 inline-block 屬性的寫法應(yīng)該是很傳統(tǒng)的垂直居中的技巧了,此方式的好處在于子元素居中可以不需要特別設(shè)定高度,我們將利用:before偽類元素設(shè)定為100%高的inline-block,再搭配上將需要居中的子元素同樣設(shè)置成inline-block性質(zhì)后,就能使用vertical-align:middle來達(dá)到垂直居中的目的了,此方式在以往其實(shí)是個(gè)非常棒的垂直居中解決方案,唯獨(dú)需要特別處理掉inline-block元素之間的4-5px空間這個(gè)小缺陷,但也很實(shí)用了。 <h2>3.:before + inline-block</h2>
<div class="box box3">
<div class="content">
立馬來看Amos實(shí)際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
CSS3精美相冊(cè)效果 </a>
效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div>
</div>
h2{
text-align: center;
}
.box{
width: 500px;
height: 250px;
border: 1px solid #f00;
margin: auto;
text-align: center;
}
.box::before{
content:'';
display: inline-block;
height: 100%;
width: 0;
vertical-align: middle;
}
.box .content{
width: 400px;
background: #ccc;
display: inline-block;
vertical-align: middle;
}4、absolute + margin 負(fù)值 適用情景:多行文字的垂直居中技巧 誰說絕對(duì)定位要少用?Amos認(rèn)為沒有少用多用的問題,重點(diǎn)在于你是否有妥善運(yùn)用才是重點(diǎn),絕對(duì)定位在這個(gè)例子中會(huì)設(shè)置top:50%來抓取空間高度的50%,接著在將居中元素的margin-top設(shè)定為負(fù)一半的高度,這樣就能讓元素居中了,此方法可是自古以來流傳多年的居中方式呢? <h2>4.absolute + margin 負(fù)值</h2>
<div class="box box4">
<div class="content">
立馬來看Amos實(shí)際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
CSS3精美相冊(cè)效果 </a>
效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div>
</div>
h2{
text-align: center;
}
.box{
width: 500px;
height: 250px;
border: 1px solid #f00;
margin: auto;
position: relative;
}
.box4 .content{
width: 400px;
background: #ccc;
height: 70px;
position: absolute;
top:50%;
left: 50%;
margin-left: -200px;
margin-top: -35px;
}5、absolute + margin auto 適用情景:多行文字的垂直居中技巧 又一個(gè)絕對(duì)定位的垂直居中的方案,這個(gè)方式比較特別一點(diǎn),當(dāng)元素設(shè)置為絕對(duì)定位后,假設(shè)它是抓不到整體可運(yùn)用的空間范圍,所以margin:auto會(huì)失效,但當(dāng)你設(shè)置了top:0;bottom:0;時(shí),絕對(duì)定位元素就抓到了可運(yùn)用的空間了,這時(shí)你的margin:auto就生效了(神奇吧),如果你的絕對(duì)定位元素需要水平居中于父層,那你同樣可以設(shè)定left:0;right:0;來讓絕對(duì)定位元素取得空間可運(yùn)用范圍,再讓marign-left與margin-right設(shè)定為auto即可居中。但此方式的缺點(diǎn)是你的定位元素必須有固定的寬高(百分比也算)才能正常居中。 <h2>5.absolute + translate(-50%, -50%)</h2>
<div class="box box5">
<div class="content">
立馬來看Amos實(shí)際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
CSS3精美相冊(cè)效果 </a>
效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div>
</div>
h2{
text-align: center;
}
.box{
width: 500px;
height: 250px;
border: 1px solid #f00;
margin: auto;
position: relative;
}
.content{
width: 400px;
background: #ccc;
height: 70px;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}6、Display:table-cell 適用情景:多行文字的垂直居中技巧 這一招我想有點(diǎn)年紀(jì)的開發(fā)者應(yīng)該都有看過,當(dāng)然像我這么嫩的開發(fā)者當(dāng)然是第一次看到啦,這一招的原理在于使用 CSS display屬性將div設(shè)置成表格的單元格,這樣就能利用支持存儲(chǔ)單元格對(duì)齊的vertical-align屬性來將信息垂直居中 <h2>19.display: table-cell</h2>
<div class="box box19">
<div class="content">
立馬來看Amos實(shí)際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
CSS3精美相冊(cè)效果 </a>
效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div>
</div>
h2{
text-align: center;
}
.box{
width: 500px;
height: 250px;
border: 1px solid #f00;
margin: auto;
text-align: center;
display: table-cell;
vertical-align: middle;
}
.content{
width: 400px;
background: #ccc;
margin: auto;
}7、padding 適用情景:多行文字的垂直居中技巧 什么!這也算垂直居中技巧,連我奶奶都知道這方式吧 對(duì)的,這的確也算是一種垂直居中的方式,不可諱言的這方式真的是簡單過頭了,以至于有些開發(fā)者認(rèn)為這種方式都不能算是一種垂直居中的技巧,但同樣的你無法反駁的是,我的數(shù)據(jù)的確垂直居中啦,好啦,就當(dāng)我硬凹吧,你說的對(duì),好吧 <h2>22.padding</h2>
<div class="box box22">
<div class="content">
立馬來看Amos實(shí)際完成的 <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
CSS3精美相冊(cè)效果 </a>
效果吧!別忘了拖拉一下窗口看看 RWD 效果喔! </div>
</div>
h2{
text-align: center;
}
.box{
width: 500px;
border: 1px solid #f00;
margin: auto;
height: auto;
padding: 50px 0;
}
.content{
width: 400px;
background: #ccc;
margin: auto;
}總結(jié):以上給大家介紹了7種CSS垂直居中技巧,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)PHP中文網(wǎng)的支持! 以上就是分享7種實(shí)現(xiàn)CSS垂直居中的技巧(附代碼)的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章! 網(wǎng)站建設(shè)是一個(gè)廣義的術(shù)語,涵蓋了許多不同的技能和學(xué)科中所使用的生產(chǎn)和維護(hù)的網(wǎng)站。 |
溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!