淺談CSS3 Feature Query: @supports功能

來源:互聯網
上載者:User
我們已經知道使用媒體查詢(Media Query)來檢測螢幕尺寸,從而實現響應式介面設計。而特性查詢則用來查詢使用者代理程式(如案頭瀏覽器)是否支援某個CSS3的特性,這個功能除了IE之外,已被其他瀏覽器所支援。本文我們主要和大家分享CSS3 Feature Query: @supports功能,這是2017年不能不瞭解和學習的一個CSS新特性,非常實用,考慮到現實世界瀏覽器的複雜性,該特性本應該先於其他新特性出來。

文法格式


@supports <supports-condition> {  <group-rule-body>}

特性查詢使用@supports規則(和媒體查詢@media類似,都是使用一個@符號首碼:at-rule),該CSS規則允許我們把CSS樣式寫在條件塊中,這樣只有在目前使用者代理支援一個特定的CSS屬性值對(property-value pair)的時候才被應用。

舉個簡單的例子,如果我們要為支援彈性方塊(flexbox)特性的瀏覽器定義樣式,可以像下面這樣寫:


@supports ( display: flex ) {    .foo { display: flex; }}

同樣的,和媒體查詢規則類似,可以使用一些邏輯操作符(如and、or和not),並支援串接在一起:


@supports (display: table-cell) and (display: list-item) {    … /* your styles */} @supports not ((text-align-last:justify) or (-moz-text-align-last:justify) ){    … /* 不支援justify時,用這裡的代碼來類比text-align-last:justify */}

使用執行個體

檢測動畫特性:


@supports (animation-name: test) {    … /* 當UA支援無首碼animations特性時的特定樣式 */    @keyframes { /* @supports 作為一個CSS條件組at-rule,可以包含其他at-rules */      …        }}

檢測自訂屬性:


@supports (--foo: green) {  body {    color: green;  }}

規範狀態

尚處於候選推薦CR(Candidate Recommendation)狀態,規範連結:CSS Conditional Rules Module Level 3.

瀏覽器安全色性

案頭系統:

行動裝置:

相關文章

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.