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

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

開發(fā) 前端
本文向大家介紹一下DIV水平居中和垂直居中的多種解決方案,主要包括單行垂直居中,層水平居中以及層中的文字水平居中等內(nèi)容。

你對(duì)DIV水平居中和垂直居中的解決方法是否了解,這里和大家分享一下,相信本文介紹一定會(huì)讓你有所收獲。

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

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

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

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

1、單行垂直居中

  文字在層中垂直居中vertical-align屬性是做不到的.我們這里有個(gè)比較巧妙的方法就是:設(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.  
  4. position:relative;display:table-cell;vertical-align:middle;"> 
  5. <div 
  6. style="background:red;position:static;position:absolute\9;top:50%;"> 
  7. <div 
  8. style="background:blue;position:relative;top:-50%;"> 
  9.  
  10. div> 
  11. div> 
  12. div> 
  13. div> 
  14.  

 5、DIV層垂直水平居中,英文超長(zhǎng)換行

ExampleSourceCode[] 

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

6、div垂直滾動(dòng)

ExampleSourceCode[] 

  1. <div 
  2. style="width:160px;height:260px;overflow-y:scroll;border:1pxsolid;"> 
  3.  
  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.  
  13. div> 
  14. div> 
  15.  
  16. div> 
  17. div> 
  18. div> 
  19. div> 
  20.  

 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.  
  14. div> 
  15.  
  16. div> 
  17. div> 
  18. div> 
  19. div> 
  20.  

【編輯推薦】

  1. DIV CSS隱藏內(nèi)容樣式方法詳解
  2. IE6.0對(duì)padding的解讀分析
  3. 揭露CSS中margins折疊現(xiàn)象內(nèi)幕
  4. 七大CSS選擇符用法說明
  5. 技術(shù)分享 如何使用CSS控制超鏈接文字樣式

 

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

2010-09-01 10:49:57

CSS水平居中垂直居中

2010-08-16 16:39:48

DIV內(nèi)容居中

2010-08-16 16:07:30

DIV垂直居中

2021-07-26 08:31:17

算法

2010-09-10 09:31:08

CSSDIV

2018-09-18 11:20:07

css html5javascript

2010-09-15 12:32:23

DIV頁(yè)面

2010-09-09 10:15:35

DIVCSS

2010-09-02 13:03:38

CSS垂直居中

2010-08-23 14:06:57

DIV+CSS

2010-09-10 13:58:38

DIV圖片居中

2010-08-27 10:30:16

CSS垂直居中

2010-09-09 10:23:23

DIVCSS垂直居中

2010-08-24 13:25:16

DIV+CSS

2010-09-02 13:16:44

CSS水平居中

2010-08-17 13:08:31

DIV+CSS布局

2010-08-30 12:46:42

DIV+CSS

2010-08-16 15:46:16

DIV居中

2010-08-31 14:49:57

CSS居中

2019-04-30 10:00:59

CSS居中前端
點(diǎn)贊
收藏

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