CSS: phương thức tĩnh supports()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.

Phương thức tĩnh CSS.supports() trả về một giá trị boolean cho biết trình duyệt có hỗ trợ một tính năng CSS nhất định hay không.

Cú pháp

js
CSS.supports(propertyName, value)
CSS.supports(supportCondition)

Tham số

Có hai nhóm tham số riêng biệt. Nhóm thứ nhất cho phép kiểm tra khả năng hỗ trợ của một cặp thuộc tính-giá trị:

propertyName

Một chuỗi chứa tên của thuộc tính CSS cần kiểm tra.

value

Một chuỗi chứa giá trị của thuộc tính CSS cần kiểm tra.

Cú pháp thứ hai nhận một tham số khớp với điều kiện của @supports:

supportCondition

Một chuỗi chứa điều kiện cần kiểm tra.

Giá trị trả về

true nếu trình duyệt hỗ trợ quy tắc, ngược lại là false.

Ví dụ

Trong các ví dụ sau, result là một giá trị boolean cho biết trình duyệt có hỗ trợ tính năng CSS đã cho hay không.

js
result = CSS.supports("text-decoration-style", "blink");
result = CSS.supports("display: flex");
result = CSS.supports("(--foo: red)");
result = CSS.supports("selector(:has(a))");
result = CSS.supports(
  "(transform-style: preserve) or (-moz-transform-style: preserve) or (-webkit-transform-style: preserve)",
);

Để xem thêm ví dụ và các đặc tính cú pháp, hãy xem at-rule @supports.

Thông số kỹ thuật

Specification
CSS Conditional Rules Module Level 3
# dom-css-supports

Tương thích trình duyệt

Xem thêm