自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

CSS水平居中和垂直居中多種解決方案

開發(fā) 前端
你對CSS中居中的概念和方法是否熟悉,這里向大家描述一下CSS中水平居中和垂直居中的多種解決方案,主要包括單行垂直居中,層水平居中,div層垂直居中等內(nèi)容。

這里和大家重點描述一下CSS水平居中和垂直居中的多種解決方案,主要包括單行垂直居中,層水平居中,div層垂直居中等內(nèi)容,相信通過本文的學(xué)習(xí)你對CSS居中問題會有更加深刻的認(rèn)識。

CSS實例:水平居中和垂直居中的多種解決方案

在定義網(wǎng)頁的CSS樣式的時候,我們或許并不怎么用到垂直居中。在51cto.com中,大量的文章介紹了這些知識。在和別人合作的時候,你的代碼的自適應(yīng)性就需要做到最大程度的好。你自己在做自己的網(wǎng)頁的時候或許可以保證在視覺上是居中的,但是如果讓這個元素或者他的父元素變大了,你還能保證居中嗎?

  vertical-align是個不錯的屬性,但是這個屬性起初是針對表格的cell的。當(dāng)然這個屬性雖然對塊級元素不起作用,但是對行內(nèi)元素比如span還是有作用的(這時候,這個屬性設(shè)置為負(fù)值元素內(nèi)容就下移,正值則上移)。

  下面是比較專業(yè)的垂直居中的集中解決方案。記錄于此,以便查閱。

1、單行垂直居中

  文字在層中垂直居中vertical-align屬性是做不到的.我們這里有個比較巧妙的方法就是:設(shè)置height的高度與line-height的高度相同!

ExampleSourceCode

  1. <divstyledivstyle="line-height:500px;height:500;"> 
  2.  

2、層水平居中

  設(shè)置div的寬度小于父div的寬度,設(shè)置margin:0auto;,即可讓div居中。

ExampleSourceCode 

  1. #parentdiv  
  2. {  
  3. width:500px;  
  4. }  
  5. #childdiv{  
  6. width:200px;  
  7. margin:0auto;  
  8. }  
  9.  

3、層中的文字水平居中

  在childdiv的css加上text-align:center

ExampleSourceCode

  1. #parentdiv  
  2. {  
  3. width:500px;  
  4. }  
  5. #childdiv{  
  6. width:200px;  
  7. margin:0auto;  
  8. text-align:center;  
  9. }  
  10.  

 #p#4、div層垂直居中

ExampleSourceCode

  1. <divstyledivstyle="width:275px;height:375px;border:solidred;"> 
  2. <divstyledivstyle="background:green;height:375px;width:275px;  
  3. position:relative;display:table-cell;vertical-align:middle;"> 
  4. <div 
  5. style="background:red;position:static;position:absolute\9;top:50%;"> 
  6. <div 
  7. style="background:blue;position:relative;top:-50%;"> 
  8. div> 
  9. div> 
  10. div> 
  11. div> 
  12.  

5、div層垂直水平居中,英文超長換行

ExampleSourceCode

  1. <divstyledivstyle="float:left;width:275px;height:375px;border:solidred;"> 
  2. <div 
  3. style="height:375px;width:275px;position:relative;  
  4. display:table-cell;vertical-align:middle;"> 
  5. <div 
  6. style="position:static;position:absolute\9;top:50%;"> 
  7. <divstyledivstyle="position:relative;top:-50%;text-align:center;"> 
  8. <divstyledivstyle="width:85px;WORD-WRAP:break-word;TABLE-LAYOUT:fixed;  
  9. word-break:break-all;margin:0auto;"> 
  10. div> 
  11.  
  12. div> 
  13. div> 
  14. div> 
  15. div> 
  16.  

 6、div垂直滾動

ExampleSourceCode

  1. <div 
  2. style="width:160px;height:260px;  
  3. overflow-y:scroll;border:1pxsolid;"> 
  4. div> 
  5.  

#p#

7、垂直居中和使用text-align水平居中

ExampleSourceCode

  1. <divstyledivstyle="float:left;width:275px;height:375px;border:solidred;"> 
  2. <div 
  3. style="height:375px;width:275px;position:relative;  
  4. display:table-cell;vertical-align:middle;"> 
  5. <div 
  6. style="position:static;position:absolute\9;top:50%;"> 
  7. <div 
  8. style="position:relative;top:-50%;text-align:center;"> 
  9. <divstyledivstyle="width:275px;"> 
  10. <divstyledivstyle="width:160px;WORD-WRAP:break-word;TABLE-LAYOUT:fixed;  
  11. word-break:break-all;text-align:left;"> 
  12. div> 
  13. div> 
  14.  
  15. div> 
  16. div> 
  17. div> 
  18. div> 
  19.  

 8、垂直居中和使用margin水平居中

ExampleSourceCode

  1. <divstyledivstyle="float:left;width:275px;
  2. height:375px;border:solidred;"> 
  3. <div 
  4. style="height:375px;width:275px;position:relative;  
  5. display:table-cell;vertical-align:middle;"> 
  6. <div 
  7. style="position:static;position:absolute\9;top:50%;"> 
  8. <div 
  9. style="position:relative;top:-50%;"> 
  10.  
  11. <divstyledivstyle="margin:0auto;width:160px;WORD-WRAP:break-word;  
  12. TABLE-LAYOUT:fixed;word-break:break-all;"> 
  13. div> 
  14. div> 
  15. div> 
  16. div> 
  17. div> 

【編輯推薦】

  1. 解析DIV+CSS網(wǎng)頁布局的意義與副作用
  2. 深入剖析CSS層疊與繼承的使用
  3. 專家推薦三大“頂級”CSS技巧!
  4. CSS中hack和注釋書寫規(guī)范
  5. DIV CSS初學(xué)者必須掌握的10個問題與技巧

 

責(zé)任編輯:佚名 來源: 52css.com
相關(guān)推薦

2010-08-24 14:23:39

DIV居中

2021-07-26 08:31:17

算法

2018-09-18 11:20:07

css html5javascript

2010-08-27 10:30:16

CSS垂直居中

2010-08-23 14:06:57

DIV+CSS

2010-09-09 10:23:23

DIVCSS垂直居中

2010-08-30 12:46:42

DIV+CSS

2010-09-10 09:31:08

CSSDIV

2010-09-09 10:15:35

DIVCSS

2010-08-31 14:49:57

CSS居中

2010-08-24 14:47:48

CSS居中

2010-09-02 13:03:38

CSS垂直居中

2010-08-26 08:45:32

margin:0pxa

2019-04-30 10:00:59

CSS居中前端

2010-08-26 09:27:07

CSS居中

2013-10-29 10:32:59

IECSS

2010-08-24 13:25:16

DIV+CSS

2010-08-16 16:39:48

DIV內(nèi)容居中

2010-08-26 11:27:35

CSS居中

2021-09-26 13:42:28

開發(fā)CSS代碼
點贊
收藏

51CTO技術(shù)棧公眾號