首页 > 其他分享 >[CSS] @scope

[CSS] @scope

时间:2024-07-01 19:22:55浏览次数:1  
标签:body like img inside article scope CSS

Video: https://www.youtube.com/watch?v=PkFuytYVqI8&list=WL&index=67

 

body
└─ article.feature
   ├─ section.article-hero
   │  ├─ h2
   │  └─ img
   │
   ├─ section.article-body
   │  ├─ h3
   │  ├─ p
   │  ├─ img
   │  ├─ p
   │  └─ figure
   │     ├─ img
   │     └─ figcaption
   │
   └─ footer
      ├─ p
      └─ img

 

If you wanted to select the <img> element inside the <section> with a class of article-body, you could do the following:

  • Write a selector like .feature > .article-body > img. However, that has high specificity so is hard to override, and is also tightly coupled to the DOM structure. If your markup structure changes in the future, you might need to rewrite your CSS.
  • Write something less specific like .article-body img. However, that will select all images inside the section.

This is where @scope is useful. It allows you to define a precise scope inside which your selectors are allowed to target elements. For example, you could solve the above problem using a standalone @scope block like the following:

@scope (.article-body) to (figure) {
  img {
    border: 5px solid black;
    background-color: goldenrod;
  }
}

The .article-body scope root selector defines the upper bound of the DOM tree scope in which the ruleset will be applied, and the figure scope limit selector defines the lower bound. As a result, only <img> elements inside a <section> with a class of article-body, but not inside <figure> elements, will be selected.

标签:body,like,img,inside,article,scope,CSS
From: https://www.cnblogs.com/Answer1215/p/18278659

相关文章

  • [CSS] Scroll animation: scroll-snap
    Video:https://www.youtube.com/watch?v=zqjKE_zcWzE&list=WL&index=68&t=14scode:https://github.com/Alliemack77/scroll-animations-with-css-only/*Customprops*/:root{--transition-250-ease-in:250msease-in;}/*Reset*/*::after,*:......
  • HTML5+CSS3集训(16)
    图片布局案例实践 <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>p155</title></head><body><ul><li><imgsrc="巧克力.jpg......
  • HTML5+CSS3集训(15)
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title></title><style>*{margin:0;padding:0;}.q1{border:1pxsolidblack;bac......
  • HTML5+CSS3集训(14)
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>p180</title></head><body><divclass="container"><divclass="top"......
  • (相关内容分享)精通CSS:高级Web标准解决方案(第3版) ( etc.)
    书:pan.baidu.com/s/1hNegko58yFJU01fPQ9PBnQ?pwd=rz68我的阅读笔记:Flexbox和Grid布局: 对于现代的布局技术,包括Flexbox和Grid,以实现更灵活和复杂的页面布局。响应式设计: 如何创建能够适应不同设备和屏幕尺寸的网页,以提供更好的用户体验。CSS动画和过渡: 利用CSS实现页面......
  • css3动画实现数字动态增加
    要实现数字的动态增加效果,可以使用CSS3的@keyframes规则来创建动画,并使用JavaScript来更新数字。以下是一个简单的实现示例:HTML:<divid="counter"class="counter">0</div>CSS:.counter{/*初始样式*/}@keyframesincreaseNumber{from{opacity:0;transfo......
  • html+css+js文章模板
    图片  源代码在图片后面,点赞加关注,谢谢......
  • 详细分析css float 属性以及position:absolute 的区别
    CSS中的float属性和position:absolute属性都可以用来定位元素,但它们在布局和行为上有着根本的区别。下面是对这两个属性的详细分析:float属性float属性主要用于让元素围绕文本流动,通常用于图像或文本块的布局。它有四个可能的值:left、right、none(默认值)、以及inherit。布局......
  • HTML5+CSS3+JS小实例:图片九宫格
    实例:图片九宫格技术栈:HTML+CSS+JS效果:源码:【HTML】<!DOCTYPEhtml><htmllang="zh-CN"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0">......
  • 探索Flex布局:CSS的现代布局解决方案
    CSS的Flexbox布局模式是一种强大的工具,它提供了一种更有效的方式来在页面上排列元素。在本文中,我们将深入探讨Flex布局的一些关键特性和实用技巧。1.一行多列布局Flexbox布局允许我们轻松地将元素排列成一行,并且可以控制当行空间不足时元素的对齐方式。代码示例首先,我们创......