首頁 > web前端 > css教學 > CSS 中的容器查詢

CSS 中的容器查詢

Mary-Kate Olsen
發布: 2024-09-28 16:08:30
原創
1069 人瀏覽過

Container Queries in CSS

First a container must be registered, and it can be queried.

Register a Container

Use a selector to register a container.

.parent {
    container-name: myname;
    container-type: inline-size;
    ... other code
}
登入後複製

or, use the shorthand option

.parent {
    container: myname / inline-size;
    ... other code
}
登入後複製

At the time of registration, two details - type and name - have to specified.

Container Type

container-type: ...

  • size
  • inline-size
  • normal

Container Name

container-name: ;

identifies the container especially useful if you may a scenario of multiple containers.

Query a Container

The container query starts with the @container at-rule followed by the name of the container and the feature query.

div {
    font-size: 2em;
}

@container myname (width: > 30ch)
{
    div {
        font-size: 3em;
    }
}
登入後複製

container query to set font-size for the div inside the myname container to 3em if the feature width is greater than 30ch.

Features to Query

size-query...

  • width
  • height
  • inline-size
  • block-size
  • aspect-ratio
  • orientation

style-query...

  • style(property: value)

property to be checked for value.

for eg

@container contname style('background-color: blue') {
    ... 
    styles 
    ...
}
登入後複製

The container query to apply styles if the background-color of the container contname is blue

Compound Queries

and, or and not can be used to create compound queries

for eg

    @container myname (width>30ch) and (height>100px) {
        ...
    }

    @container myname not (color: blue) {
        ...
    }
登入後複製

Nested Container Queries

Container queries can be nested within other container queries.

for eg

    @container myname (width>30ch) {
        ...
        @container myname (background-color: blue) {
            ...
        }
        @container myname (background-color: red) {
            ...
        }
    }
登入後複製

以上是CSS 中的容器查詢的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板