行內水平居中設置通過給父元素設置 text-align:center 來實現的,塊狀元素分定寬和不定寬塊狀元素實現水平居中有3種方法:1.加入 table 標簽2.設置 display: inline 3.設置 position:relative 和 left:50%:利用 相對定位 的方式,將元素... ...
行內元素水平居中如何設置?如果被設置元素為文本、圖片等行內元素時,水平居中是通過給父元素設置 text-align:center 來實現的。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>行內水平居中設置</title> 6 <style type="text/css"> 7 div.box1,h5{ 8 border: 1px solid red;/*為了顯示居中效果明顯為 div 設置了邊框*/ 9 text-align: center; 10 } 11 </style> 12 </head> 13 <body> 14 <h5>行內元素水平居中如何設置?如果被設置元素為文本、圖片等行內元素時,水平居中是通過給父元素設置 text-align:center 來實現的。</h5> 15 <div class="box1"><img src="https://www.cnblogs.com/images/logo_small.gif" ></div> 16 </body> 17 </html>
當被設置元素為 塊狀元素 時用 text-align:center 就不起作用了,這時也分兩種情況:定寬塊狀元素和不定寬塊狀元素。
A.定寬塊狀元素(塊狀元素的寬度width為固定值)如何設置水平居中?滿足定寬和塊狀兩個條件的元素是可以通過設置“左右margin”值為“auto”來實現居中的。同時margin-left:auto;margin-right:auto;元素的margin是可以隨意設置的。
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>定寬塊狀元素設置</title> 6 <style type="text/css"> 7 div.box1{ 8 border: 1px solid red; 9 width: 600px; 10 /*text-align: center;*//*當被設置元素為 塊狀元素 時用 text-align:center 就不起作用了*/ 11 margin: 0 auto; 12 /* margin-left: auto; 13 margin-right: auto; */ 14 } 15 </style> 16 </head> 17 <body> 18 <div class="box1">明日復明日,明日何其多,我生待明日,萬事成蹉跎</div> 19 </body> 20 </html>
B.不定寬度的塊狀元素(塊狀元素的寬度width不固定)設置居中,比如網頁上的分頁導航,因為分頁的數量是不確定的,所以我們不能通過設置寬度來限制它的彈性。不定寬度的塊狀元素有三種方法居中1.加入 table 標簽2.設置 display: inline 方法:與第一種類似,顯示類型設為 行內元素,進行不定寬元素的屬性設置3.設置 position:relative 和 left:50%:利用 相對定位 的方式,將元素向左偏移 50% ,即達到居中的目的
B-1:為什麼選擇方法一加入table標簽? 是利用table標簽的長度自適應性---即不定義其長度也不預設父元素body的長度(table其長度根據其內文本長度決定),因此可以看做一個定寬度塊元素,然後再利用定寬度塊狀居中的margin的方法,使其水平居中。第一步:為需要設置的居中的元素外面加入一個 table 標簽 ( 包括 <tbody>、<tr>、<td> )。第二步:為這個 table 設置“左右 margin 居中”(這個和定寬塊狀元素的方法一樣)。
B-2:改變元素的display類型為行內元素,利用其屬性直接設置(改變塊級元素的 display 為 inline 類型(設置為 行內元素 顯示),然後使用 text-align:center 來實現居中效果)
B-3:通過給父元素設置 float,然後給父元素設置 position:relative 和 left:50%,子元素設置 position:relative 和 left: -50% 來實現水平居中。
針對B-1與B-2總結:這種方法相比第一種方法的優勢是不用增加無語義標簽,但也存在著一些問題:它將塊狀元素的 display 類型改為 inline,變成了行內元素,所以少了一些功能,比如設定長度值。
B-3:/*假想ul層的父層(即下麵例子中的div層)中間有條平分線將ul層的父層(div層)平均分為兩份,ul層的css代碼是將ul層的最左端與ul層的父層(div層)的平分線對齊;而li層的css代碼則是將li層的平分線與ul層的最左端(也是div層的平分線)對齊,從而實現li層的居中。*/
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>不定寬塊狀元素設置</title> 6 <style type="text/css"> 7 table{ 8 border: 1px solid red; 9 margin: 0 auto; 10 } 11 .container{ 12 text-align: center; 13 border: 1px solid #0000FF; 14 15 } 16 .container ul{ 17 list-style: none; 18 margin: 0; 19 padding: 0; 20 /* margin:0;padding:0(消除文本與div邊框之間的間隙)*/ 21 } 22 .container ul li{ 23 display: inline; 24 } 25 .box,.box ul{ 26 position: relative; 27 } 28 .box{ 29 float: left; 30 left: 50%; 31 } 32 .box ul{ 33 list-style: none; 34 margin: 0; 35 padding: 0; 36 left: -50%; 37 } 38 /* .box ul li{ 39 float: left; 40 display: inline; 41 } */ 42 </style> 43 </head> 44 <body> 45 <!-- 加入 table 標簽 --> 46 <table> 47 <tbody> 48 <tr> 49 <td> 50 <ul> 51 <li>明日復明日,明日何其多,我生待明日,萬事成蹉跎1</li> 52 <li>明日復明日,明日何其多,我生待明日,萬事成蹉跎2</li> 53 <li>明日復明日,明日何其多,我生待明日,萬事成蹉跎3</li> 54 <li>明日復明日,明日何其多,我生待明日,萬事成蹉跎4</li> 55 <li>明日復明日,明日何其多,我生待明日,萬事成蹉跎5</li> 56 </ul> 57 </td> 58 </tr> 59 </tbody> 60 </table> 61 <!-- 設置 display: inline 方法:與第一種類似,顯示類型設為 行內元素,進行不定寬元素的屬性設置 --> 62 <div class="container"> 63 <ul> 64 <li>明日復明日,明日何其多,我生待明日,萬事成蹉跎1</li> 65 <li>明日復明日,明日何其多,我生待明日,萬事成蹉跎2</li> 66 <li>明日復明日,明日何其多,我生待明日,萬事成蹉跎3</li> 67 <li>明日復明日,明日何其多,我生待明日,萬事成蹉跎4</li> 68 <li>明日復明日,明日何其多,我生待明日,萬事成蹉跎5</li> 69 </ul> 70 </div> 71 <!-- 設置浮動和相對定位來實現 --> 72 <div class="box"> 73 <ul> 74 <li>明日復明日,明日何其多,我生待明日,萬事成蹉跎1</li> 75 <li>明日復明日,明日何其多,我生待明日,萬事成蹉跎2</li> 76 <li>明日復明日,明日何其多,我生待明日,萬事成蹉跎3</li> 77 <li>明日復明日,明日何其多,我生待明日,萬事成蹉跎4</li> 78 <li>明日復明日,明日何其多,我生待明日,萬事成蹉跎5</li> 79 </ul> 80 </div> 81 </body> 82 </html>
為了用戶體驗性好,除了水平居中,我們還有垂直居中,這裡我們又得分兩種情況:父元素高度確定的單行文本,以及父元素高度確定的多行文本。
C-1:父元素高度確定的單行文本的豎直居中的方法是通過設置父元素的 height 和 line-height 高度一致來實現的。line-height 與 font-size 的計算值之差,在 CSS 中成為“行間距”。分為兩半,分別加到一個文本行內容的頂部和底部。這種文字行高與塊高一致帶來了一個弊端:當文字內容的長度大於塊的寬時,就有內容脫離了塊。
C2父元素高度確定的多行文本、圖片等的豎直居中的方法有兩種:
C2-1:使用插入 table (包括tbody、tr、td)標簽,同時設置 vertical-align:middle。css 中有一個用於豎直居中的屬性 vertical-align,在父元素設置此樣式時,會對inline-block類型的子元素都有用。/*因為 td 標簽預設情況下就預設設置了 vertical-align 為 middle,所以我們不需要顯式地設置了。*/
C2-2:在 chrome、firefox 及 IE8 以上的瀏覽器下可以設置塊級元素的 display 為 table-cell(設置為表格單元顯示),激活 vertical-align 屬性,但註意 IE6、7 並不支持這個樣式, 相容性比較差。(不推薦)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>垂直居中</title> 6 <style type="text/css"> 7 .box1{ 8 width: 600px; 9 height: 50px; 10 line-height: 50px; 11 border: 1px solid red; 12 background-color: #f90; 13 }/*當文字內容的長度大於塊的寬時,就有內容脫離了塊。*/ 14 table td{ 15 /* vertical-align: middle; */ 16 background-color: #008000; 17 height: 100px; 18 color: #fff; 19 } 20 .box2{ 21 height: 200px; 22 display: table-cell; 23 vertical-align: middle; 24 background-color: #00FFFF; 25 padding: 0 20px; 26 }/*在 chrome、firefox 及 IE8 以上的瀏覽器下可以設置塊級元素的 display 為 table-cell(設置為表格單元顯示),激活 vertical-align 屬性,但註意 IE6、7 並不支持這個樣式, 相容性比較差。*/ 27 .box2 ul li::selection{ 28 background-color: blueviolet; 29 color: lightblue; 30 } 31 .box2 ul{ 32 list-style: none; 33 margin: 0; 34 padding: 0; 35 } 36 </style> 37 </head> 38 <body> 39 <!-- 父元素高度確定的單行文本 --> 40 <div class="box1"> 41 Tomorrow is coming, tomorrow is so many, I will be born tomorrow, everything will happenTomorrow is coming, tomorrow is so many, 42 </div> 43 <!-- C2-1使用插入 table (包括tbody、tr、td)標簽,同時設置 vertical-align:middle。 --> 44 <table> 45 <tbody> 46 <tr> 47 <td> 48 Tomorrow is coming, tomorrow is so many, I will be born tomorrow, everything will happenTomorrow is coming, tomorrow is so many, I will be born tomorrow, everything will happenTomorrow is coming, tomorrow is so many, I will be born tomorrow, everything will happen 49 </td> 50 </tr> 51 </tbody> 52 </table> 53 <!-- C2-2:在 chrome、firefox 及 IE8 以上的瀏覽器下可以設置塊級元素的 display 為 table-cell(設置為表格單元顯示),激活 vertical-align 屬性,但註意 IE6、7 並不支持這個樣式, 相容性比較差。(不推薦) --> 54 <div class="box2"> 55 <ul> 56 <li>Hello saturday, i'm friday yesterday, buddy has not been very good this week, can you treat him tenderly</li> 57 <li>Hello saturday, i'm friday yesterday, buddy has not been very good this week, can you treat him tenderly</li> 58 <li>Hello saturday, i'm friday yesterday, buddy has not been very good this week, can you treat him tenderly</li> 59 <li>Hello saturday, i'm friday yesterday, buddy has not been very good this week, can you treat him tenderly</li> 60 <li>Hello saturday, i'm friday yesterday, buddy has not been very good this week, can you treat him tenderly</li> 61 </ul> 62 </div> 63 </body> 64 </html>
最後有一個有趣的現象就是當為元素(不論之前是什麼類型元素,display:none 除外)設置以下 2 個句之一:1. position : absolute 2. float : left 或 float:right (簡單來說,只要html代碼中出現以上兩句之一,元素的display顯示類型就會自動變為以 display:inline-block(塊狀元素)的方式顯示,當然就可以設置元素的 width 和 height 了,且預設寬度不占滿父元素)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>隱性改變display類型</title> 6 <style type="text/css"> 7 .box1,.box2{ 8 background: #00FF00; 9 /* display: none; */ 10 width: 400px; 11 height: 300px; 12 color: #fff; 13 text-align: justify; 14 } 15 .box1{ 16 float: left; 17 border: 2px solid red; 18 } 19 .box2{ 20 position: absolute; 21 left: 50px; 22 top: 150px; 23 border: 2px solid #0000FF; 24 } 25 .box3{ 26 background-color: #f90; 27 color: #fff; 28 font-weight: lighter; 29 width: 400px; 30 position: relative; 31 left: 20px; 32 top: 50px; 33 } 34 </style> 35 </head> 36 <body> 37 <span class="box1"> 38 box1簡單來說,只要html代碼中出現2者之一 1. position : absolute 2. float : left 或 float:right ,元素的display顯示類型就會自動變為以 display:inline-block(塊狀元素)的方式顯示,當然就可以設置元素的 width 和 height 了,且預設寬度不占滿父元素。 39 </span> 40 <em class="box2"> 41 box2簡單來說,只要html代碼中出現2者之一 1. position : absolute 2. float : left 或 float:right ,元素的display顯示類型就會自動變為以 display:inline-block(塊狀元素)的方式顯示,當然就可以設置元素的 width 和 height 了,且預設寬度不占滿父元素。 42 </em> 43 <strong class="box3">box3簡單來說,只要html代碼中出現2者之一 1. position : absolute 2. float : left 或 float:right ,元素的display顯示類型就會自動變為以 display:inline-block(塊狀元素)的方式顯示,當然就可以設置元素的 width 和 height 了,且預設寬度不占滿父元素。</strong> 44 </body> 45 </html>