”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > CSS 中的容器查询

CSS 中的容器查询

发布于2024-11-12
浏览:111

Container Queries in CSS

首先必须注册一个容器,并且可以查询它。

注册一个容器

使用选择器注册容器。

.parent {
    container-name: myname;
    container-type: inline-size;
    ... other code
}

或者,使用简写选项

.parent {
    container: myname / inline-size;
    ... other code
}

注册时,必须指定两个详细信息 - 类型和名称。

集装箱类型

容器类型:...

  • 尺寸
  • 内联大小
  • 普通的

集装箱名称

容器名称:;

标识容器,如果您可能有多个容器,则特别有用。

查询容器

容器查询以 @container at-rule 开头,后跟容器名称和功能查询。

div {
    font-size: 2em;
}

@container myname (width: > 30ch)
{
    div {
        font-size: 3em;
    }
}

容器查询,如果特征宽度大于 30ch,则将 myname 容器内的 div 的字体大小设置为 3em。

查询功能

尺寸查询...

  • 宽度
  • 高度
  • 内联大小
  • 块大小
  • 纵横比
  • 方向

样式查询...

  • 样式(属性:值)

要检查值的属性。

例如

@container contname style('background-color: blue') {
    ... 
    styles 
    ...
}

如果容器名称的背景颜色为蓝色,则容器查询应用样式

复合查询

and、or 和 not 可用于创建复合查询

例如

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

    @container myname not (color: blue) {
        ...
    }

嵌套容器查询

容器查询可以嵌套在其他容器查询中。

例如

    @container myname (width>30ch) {
        ...
        @container myname (background-color: blue) {
            ...
        }
        @container myname (background-color: red) {
            ...
        }
    }
版本声明 本文转载于:https://dev.to/gajendra/container-queries-in-css-3o6a?1如有侵犯,请联系[email protected]删除
最新教程 更多>

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3