一、自適應內部元素 利用width的新特性min-content實現 width新特性值介紹: fill-available,自動填充盒子模型中剩餘的寬度,包含margin、padding、border尺寸,他讓非block元素也可實現寬度100%; max-content:假設容器有足夠的寬度,足 ...
目錄
一、自適應內部元素
- 利用width的新特性min-content實現
- width新特性值介紹:
- fill-available,自動填充盒子模型中剩餘的寬度,包含margin、padding、border尺寸,他讓非block元素也可實現寬度100%;
- max-content:假設容器有足夠的寬度,足夠的空間,此時,所占據的寬度就是max-content,與display為inline不一樣,max-content就像white-space:nowrap一樣,不會換行的。
- min-content:採用內部元素最小寬度值最大元素的寬度作為最終容器的寬度,最小寬度:替換元素,如:圖片的最小寬度就是圖片呈現的寬度,文本元素,如果全是中文就是一個中文的寬度,如果包含英文,預設為英文單詞不換行的寬度。
- fit-content:實現浮動元素的水平居中,因預設情況下浮動的元素元素是不能通過margin:auto實現水平居中的,這時就需要fit-content輔助實現。
- width新特性值介紹:
- min-content與max-content示例代碼:
<head> <meta charset="UTF-8"> <title>Document</title> <style> .figure01{ width: min-content; margin: auto; } .figure01 img{ width: 210px; } .figure02{ width: max-content; margin: auto; } .figure02 img{ width: 210px; } </style> </head> <body> <figure class="figure01" > <img src="../img/cat.png" alt=""> <p>標簽規定獨立的流內容(圖像、圖表、照片、代碼等等)。figure 元素的內容應該與主內容相關,但如果被刪除,則不應對文檔流產生影響。</p> </figure> <figure class="figure02" > <img src="../img/cat.png" alt=""> <p>標簽規定獨立的流內容(圖像、圖表、照片、代碼等等)。figure 元素的內容應該與主內容相關,但如果被刪除,則不應對文檔流產生影響。</p> </figure> </body>
- fit-content示例代碼:
<head> <meta charset="UTF-8"> <title>float的fit-conent的實用</title> <style> .wrap{ } .wrap ul{ margin: auto; list-style: none; width: fit-content; } .wrap ul li{ float: left; margin: 0px 10px; background: greenyellow; padding: 4px; } </style> </head> <body> <div class="wrap"> <ul> <li>one</li> <li>two</li> <li>thr</li> <li>fou</li> </ul> </div> </body>
二、精確控製表格的列寬
- 利用table的table-layout屬性設置為fixed值來實現,但需要width設置為100%
- 代碼如下
<head> <meta charset="UTF-8"> <title>Document</title> <style> .divWrap{ width: 400px; border: 1px solid red; } .wrap{ table-layout: fixed; width: 100%; } .wrap td.one{ width: 20%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .wrap td.two{ width: 80%; } </style> </head> <body> <div class='divWrap' > <table class="wrap" > <tr> <td class='one' >one</td> <td class='two' >這個中文假文產生器每次會產生由五個段落、每段六至八個假句組成的假文。所以,假句其實是假文的基礎。為了讓假句在視覺質地上接近真句,我準備了一個由 128 個真句組成的語料庫。每次要產生假句時就從中隨機挑選一個,然後逐字替換為隨機選出的筆畫數相同、但一般人不可能認得的低頻字。因為一般人不可能認得,實際上也算是假字了這個中文假文產生器每次會產生由五個段落、每段六至八個假句組成的假文。所以,假句</td> </tr> <tr> <td class='one' >這個中文假文產生器每次會產生由五個段落、每段六至八個假句組成的假文。所以,假句</td> <td class='two' > <img src="../img/cat.png" alt=""> </td> </tr> </table> </div> </body>
三、根據兄弟元素的數量來設置樣式
- nth-last-child和only-child、nth-child的應用
- only-child:選擇只有一個子元素的元素
- nth-child:選擇第幾個元素
- nth-last-child:選擇第幾個元素,從最後一個開始計數
- odd和even:表示奇數和偶數元素
- (an+b):n表示下標從0開始,b是偏移量,(3n+0):表示3的倍數元素
- nth-of-type和nth-last-of-type的應用
- odd和even:表示奇數和偶數元素
- (an+b):n表示下標從0開始,(3n+0):表示3的倍數元素
- (-n+b):表示選擇小於等於b的元素
- nth-child與nth-of-type的區別
- nth-child是以同級第一個元素開始計數
- nth-of-type是以同級指定類型的第一個元素開始計數
- 代碼:
<head> <meta charset="UTF-8"> <title>Document</title> <style> .wrap{ width: 200px; } .wrap p:nth-child(2), .wrap p:nth-child(4){ /*從第一個元素開始計數*/ background: red; } .wrap p:nth-of-type(1), .wrap p:nth-of-type(2){ /*從第一個p元素開始計數*/ color: lightyellow; } </style> </head> <body> <div class="wrap"> <h1>標題數據</h1> <p>第一行內容(第一個P標簽)</p> <span>這是一個span</span> <p>第二行內容(第二個P標簽)</p> </div> </body>
- nth-last-child選擇指定兄弟元素
li:first-child:nth-last-child(n+6):nth-last-child(-n+8)
分析: - first-child:選擇第一個元素
- nth-last-child(n+6):選擇從最後一個開始計數的,下標大於6的元素
- nth-last-child(-n+8):選擇從最後一個開始計數的,下標小於8的元素
- 選擇有6 - 8個兄弟元素的li。
- 示例代碼:
<head> <meta charset="UTF-8"> <title>Document</title> <style> ul{ list-style: none; overflow: hidden; } ul li{ float: left; padding: 4px 10px; border-radius: 4px; background: greenyellow; margin: 0px 2px; } li:first-child:nth-last-child(4), li:first-child:nth-last-child(4)~li{ background: indianred; } li:first-child:nth-last-child(n+6):nth-last-child(-n+8), li:first-child:nth-last-child(n+6):nth-last-child(-n+8)~li{ /*匹配li有6,7,8個的ul*/ background: red; } </style> </head> <body> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> </ul> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> </ul> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> </ul> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> </ul> </body>
四、一幅背景,定寬內容居中
- 可以利用margin:auto實現,但需要多一層html結構
- 利用calc可以少一層html結構,且Css更簡潔
- calc(50% - 300px): -之間必須要有空格,否則會解析出錯
用容器一半的寬度 - 內容塊一半的寬度
- 容器的寬度可以不設置為100%,因為容器的最小寬度都為內容的寬度
- 示例代碼:
<head> <meta charset="UTF-8"> <title>Document</title> <style> footer{ background: url("../img/cat.png") repeat-x; height: 200px; color: gainsboro; padding: 1em calc(50% - 300px); } .wrap{ width: 600px; } </style> </head> <body> <footer> <div class="wrap"> 這個中文假文產生器每次會產生由五個段落、每段六至八個假句組成的假文。所以,假句其實是假文的基礎。為了讓假句在視覺質地上接近真句,我準備了一個由 128 個真句組成的語料庫。每次要產生假句時就從中隨機挑選一個,然後逐字替換為隨機選出的筆畫數相同、但一般人不可能認得的低頻字。因為一般人不可能認得,實際上也算是假字了這個中文假文產生器每次會產生由五個段落、每段六至八個假句組成的假文。所以,假句 </div> </footer> </body>
五、垂直居中
5.1、絕對定位
- 以下兩種技巧都需要使用絕對定位
- calc實現
- 內容部分必須固定寬和高
- translate實現
- 內容部分可以自適應寬和高
- 某些瀏覽器會導致元素模糊,可用transform-style:preserve-3d來修複,因為元素可能被放置在半個像素上
- 示例代碼:
<head> <meta charset="UTF-8"> <title>Document</title> <style> .wrap{ position: relative; width: 400px; height: 150px; border: 1px solid red; } .wrap .cont{ position: absolute; top: calc(50% - 30px); left: calc(50% - 50px); width: 100px; height: 60px; background: gray; } .wrap02{ position: relative; width: 400px; height: 150px; border: 1px solid red; } .wrap02 .cont{ position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); background: gray; } </style> </head> <body> <div class="wrap"> <div class="cont">這個內容部分需要定寬和定高</div> </div> <div class="wrap02"> <div class="cont">這個內容部分可以實現自適應</div> </div> </body>
5.2、視口垂直居中 + translate
- 1vh表示視口高度的1%, 1vw表示視口的寬度的1%
- 當寬度 小於 < 高度時,1vmin = 1vm, 否則 1vmin = 1vh
- 當寬度 大於 > 高度時, 1vmax = 1vm,否則 1vmax = 1vh;
- 內容部分必須要固定寬和高
- 示例代碼:
<head> <meta charset="UTF-8"> <title>Document</title> <style> .wrap{ width: 18em; background: lightgreen; padding: 1em 1.5em; margin: 50vh auto 0; transform: translateY(-50%); } </style> </head> <body> <div class="wrap"> 這個只能做到視口居中 </div> </body>
5.3、flexbox + margin:auto
- 在flexbox時,用margin:auto可以實現水平和垂直居中,可以用 margin:0 auto設置水平居中;margin: auto 0設置垂直居中
- 被居中元素的寬度和高度可以自適應
- 也可以通過flex的align-items和justify-content來實現水平垂直居中
- 示例代碼:
<head> <meta charset="UTF-8"> <title>Document</title> <style> .wrap01{ display: flex; min-height: 10vh; border: 1px solid gray; width: 30vw; } .wrap01 .main{ margin: auto; padding