CSS新特性

CSS容器查询

容器查询允许开发者基于父容器尺寸而非视口尺寸应用样式。通过@container规则实现,需先使用container-type定义查询基准:

css 复制代码
.parent {
  container-type: inline-size;
}
@container (min-width: 600px) {
  .child { font-size: 1.2rem; }
}

层叠层(Cascade Layers)

通过@layer控制样式优先级,解决特异性冲突。可创建明确的分层结构:

css 复制代码
@layer base, components, utilities;
@layer base {
  button { background: red; }
}
@layer utilities {
  .btn { background: blue; } /* 最终生效 */
}

子网格(Subgrid)

扩展CSS Grid功能,允许嵌套网格继承父网格轨道。在grid布局中声明:

css 复制代码
.parent {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}
.child {
  grid-column: span 2;
  display: grid;
  grid-template-columns: subgrid; /* 继承父级列轨道 */
}

颜色函数更新

新增oklch()oklab()等感知均匀的颜色空间函数:

css 复制代码
.element {
  background: oklch(70% 0.3 150); /* 亮度 色度 色相 */
  color: oklab(0.6 -0.1 -0.2);
}

视口单位扩展

新增动态视口单位适应移动端浏览器UI变化:

  • dvw/dvh:考虑动态工具栏的视口尺寸
  • svw/svh:最小可见视口尺寸
  • lvw/lvh:最大可见视口尺寸

文本换行控制

text-wrap属性提供更精细的排版控制:

css 复制代码
p {
  text-wrap: pretty; /* 自动优化换行避免孤行 */
  text-wrap: balance; /* 平衡多行文本宽度 */
}

滚动驱动动画

通过scroll-timeline关联滚动位置与动画:

css 复制代码
@keyframes fade {
  to { opacity: 0 }
}
.scroller {
  scroll-timeline: --scroll-track;
}
.target {
  animation: fade auto linear;
  animation-timeline: --scroll-track;
}

嵌套语法

原生CSS支持类似预处理器嵌套写法:

css 复制代码
.parent {
  & .child { color: red; }
  @media (width >= 600px) {
    padding: 2rem;
  }
}
相关推荐
西洼工作室1 天前
CSS响应式布局全攻略
前端·css·响应式布局·栅格系统
鹤归时起雾.1 天前
CSS属性继承与元素隐藏全解析
前端·css
合作小小程序员小小店1 天前
web网页开发,在线%物流配送管理%系统,基于Idea,html,css,jQuery,java,ssh,mysql。
java·前端·css·数据库·jdk·html·intellij-idea
华仔啊1 天前
20个CSS实用技巧,10分钟从小白变大神!
前端·css
昔人'1 天前
grid: auto-fit 和 auto-fill区别
css·grid
小白每天学一点1 天前
微信小程序开发学习-8
javascript·css·微信小程序·小程序·html
合作小小程序员小小店2 天前
web网页开发,在线%人力资源管理%系统,基于Idea,html,css,jQuery,java,jsp,ssh,mysql。
java·前端·css·数据库·mysql·html·intellij-idea
这个一个非常哈2 天前
VUE篇之推送+瀑布流
css·vue.js·css3
顾安r2 天前
11.19 脚本 最小web控制linux/termux
linux·服务器·css·flask
Dontla2 天前
React Tailwind CSS div布局demo
前端·css·react.js