2、css效能最佳化專題————呱呱二號,css效能最佳化

來源:互聯網
上載者:User

2、css效能最佳化專題————呱呱二號,css效能最佳化

css選取器最佳化

首先,css選取器的解析順序是由右到左的


for example:

1.(1)box > span

瀏覽器渲染網頁的時候是先搜尋span,接著一直向上搜尋,直到搜尋到span的父級元素為.box,如果搜尋不到,就會尋找下一個span。

1.(2).box span

瀏覽器渲染網頁的時候是先搜尋span,接著一直向上搜尋,直到搜尋到span的父輩元素為.box,如果搜尋不到,就會尋找下一個span。

相比之下,1.(1)的效率會比1.(2)的效率要高。

 

 

2.(1)#btn

在一個頁面上每一個id都是唯一的,瀏覽器會直接尋找到#btn的元素,並進行渲染。

2.(2)button#btn

這個例子相對於上面的選取器多了一個button,瀏覽器尋找到#btn的元素之後,還會再尋找button元素。

 

 

3.[name="submit"]

盡量不要使用全域選取器

 

4.多個規則還不如直接使用一個特殊的class

for example:

4.(1)div .box ul li a

4.(2).div-a

 

5.巧用css繼承屬性,減少代碼使用。

不可繼承的:display、margin、border、padding、background、height、min-height、max-height、width、min-width、max-width、overflow、position、left、right、top、bottom、z-index、float、clear、table-layout、vertical-align、page-break-after、page-bread-before和unicode-bidi。所有元素可繼承:visibility和cursor。內嵌元素可繼承:letter-spacing、word-spacing、white-space、line-height、color、font、font-family、font-size、font-style、font-variant、font-weight、text-decoration、text-transform、direction。終端塊狀元素可繼承:text-indent和text-align。列表元素可繼承:list-style、list-style-type、list-style-position、list-style-image。表格元素可繼承:border-collapse。 font屬性書寫格式(僅css1)
font : font-style font-variant font-weight font-size line-height font-family;

font屬性預設值
font: normal normal normal medium normal "Times New Roman" ; 5.css放在head位置css放在head位置的好處就是,在載入css檔案的時候,瀏覽器不會渲染頁面,只有css檔案載入好了之後才會進行渲染頁面。而在此之前,頁面都以空白顯示。  6.css縮寫,可以減少代碼的使用,以達到檔案更小的目的。   

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.