【css技能提升】css進階技巧,css技能提升

來源:互聯網
上載者:User

【css技能提升】css進階技巧,css技能提升
使用CSS複位

CSS複位可以在不同的瀏覽器上保持一致的樣式風格。您可以使用CSS reset 庫Normalize等,也可以使用一個更簡化的複位方法:

* {  box-sizing: border-box;  margin: 0;  padding: 0;}

現在元素的 margin 和 padding 已為0,box-sizing可以管理您的CSS盒模型布局。

注意:如果你遵循接下來繼承 box-sizing講解的這個技巧, 你不需要在以上代碼中添加 box-sizing 屬性。

 

繼承  box-sizing

從 html 元素繼承 box-sizing :

html {  box-sizing: border-box;}*, *::before, *::after {  box-sizing: inherit;}

如此在外掛程式或其它組件裡改變 box-sizing 變得簡單。

使用  :not() 選取器來決定表單是否顯示邊框

先為元素添加邊框

/* 添加邊框 */.nav li {  border-right: 1px solid #666;}

 

為最後一個元素去除邊框

/* 去掉邊框 */.nav li:last-child {  border-right: none;}

不過不要這麼做,使用 :not() 偽類來達到同樣的效果:

.nav li:not(:last-child) {  border-right: 1px solid #666;}

當然,你也可以使用 .nav li + li 或者 .nav li:first-child ~ li ,但是 :not() 更加清晰,具有可讀性。

為  body 元素添加行高

不必為每一個 <p><h*> 元素逐一添加 line-height,直接添加到 body 元素:

body {  line-height: 1.5;}

文本元素可以很容易地繼承 body 的樣式。

 

垂直置中任何元素

不!這絕不是黑魔法,真的可以垂直置中任何元素:

html, body {  height: 100%;  margin: 0;}body {  -webkit-align-items: center;    -ms-flex-align: center;    align-items: center;  display: -webkit-flex;  display: flex;}

注意: IE11 對 flexbox 的支援有點 bug。

逗號分隔列表

使列表的每項都由逗號分隔:

ul > li:not(:last-child)::after {  content: ",";}

因最後一項不加逗號,可以使用 :not() 偽類。

注意:這一技巧對於無障礙,特別是螢幕助讀程式而言並不理想。而且複製粘貼並不會帶走CSS產生的內容,需要注意。

使用負的  nth-child 來選擇元素

使用負的 nth-child 可以選擇 1 至 n 個元素。

li {  display: none;}/* 選擇第 1 至第 3 個元素並顯示出來 */li:nth-child(-n+3) {  display: block;}

 

或許你已經掌握了如何使用 :not()這個技巧,試下這個:

/* 選擇第 1 至第 3 個元素並顯示出來 */li:not(:nth-child(-n+3)) {  display: none;}

如此簡單!

使用 SVG 表徵圖

沒有理由不使用 SVG 表徵圖:

.logo {  background: url("logo.svg");}

 

SVG 在所有解析度下都可以良好縮放,並且支援所有 IE9 以後的瀏覽器,丟掉你的 .png, .jpg, 或 .gif-jif-whatev 檔案吧。

注意: 針對僅有表徵圖的按鈕,如果 SVG 沒有載入成功的話,以下樣式對無障礙有所協助:

.no-svg .icon-only:after {  content: attr(aria-label);}
使用 “形似貓頭鷹” 的選取器

這個名字可能比較陌生,不過通用選取器 (*) 和 相鄰兄弟選取器 (+) 一起使用,效果非凡:

* + * {  margin-top: 1.5em;}

更多 “形似貓頭鷹” 的選取器,可參考 A List Apart 上面 Heydon Pickering 的文章

使用  max-height 來建立純 CSS 的滑塊

max-height 與 overflow hidden 一起來建立純 CSS 的滑塊:

.slider {  max-height: 200px;  overflow-y: hidden;  width: 300px;}.slider:hover {  max-height: 600px;  overflow-y: scroll;}

滑鼠移入滑塊元素時增大它的 max-height 值,便可以顯示溢出部分。

創造格子等寬的表格

table-layout: fixed 可以讓每個格子保持等寬:

.calendar {  table-layout: fixed;}

無痛的 table 布局。

利用 Flexbox 去除多餘的外邊距

與其使用 nth-, first-, 和 last-child 去除列之間多餘的間隙,不如使用 flexbox 的 space-between 屬性:

.list {  display: flex;  justify-content: space-between;}.list .person {  flex-basis: 23%;}

列之間的間隙總是均勻相等。

 

利用屬性選取器來選擇空連結

當 <a> 元素沒有常值內容,但有 href 屬性的時候,顯示它的 href 屬性:

a[href^="http"]:empty::before {  content: attr(href);}

相當簡便。

給 “預設” 連結定義樣式

給 “預設” 連結定義樣式:

a[href]:not([class]) {  color: #008000;  text-decoration: underline;}

通過 CMS 系統插入的連結,通常沒有 class 屬性,以上樣式可以甄別它們,而且不會影響其它樣式。

一致垂直節奏

通用選取器 (*) 跟元素一起使用,可以保持一致的垂直節奏:

.intro > * {  margin-bottom: 1.25rem;}

 

一致的垂直節奏可以提供視覺美感,增強內容的可讀性。

 

固定比例盒子

要建立具有固定比例的一個盒子,所有你需要做的就是給 div 設定一個 padding:

.container {  height: 0;  padding-bottom: 20%;  position: relative;}.container div {  border: 2px dashed #ddd;      height: 100%;  left: 0;  position: absolute;  top: 0;  width: 100%;}

使用20%的padding-bottom使得框等於其寬度的20%的高度。與視口寬度無關,子項目的div將保持其寬高比(100%/ 20%= 5:1)。

 

為破碎圖象定義樣式

只要一點CSS就可以美化破碎的圖象:

img {    display: block;  font-family: Helvetica, Arial, sans-serif;  font-weight: 300;  height: auto;  line-height: 2;  position: relative;  text-align: center;  width: 100%;}

 

以添加虛擬元素的法則來顯示使用者資訊和URL的引用:

img:before {    content: "We're sorry, the image below is broken :(";  display: block;  margin-bottom: 10px;}img:after {    content: "(url: " attr(src) ")";  display: block;  font-size: 12px;}

瞭解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.