当前位置: 首页> 教育> 就业 > css-伪类选择器

css-伪类选择器

时间:2025/9/14 19:06:47来源:https://blog.csdn.net/wch19960518/article/details/140803117 浏览次数:0次

css记录

  • 选择器
    • 伪类选择器
      • :is、:where
      • :has

选择器

伪类选择器

:is、:where

栗子完整代码

<template><section class="is-styling"><p>我是is的section</p></section>![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/b0d3967ff5c147ba91c6680f1d93250b.png)<aside class="is-styling"><p>我是is的aside</p></aside><footer class="is-styling"><p>我是is的footer</p></footer><br /><section class="where-styling"><p>我是where的section</p></section><aside class="where-styling"><p>我是where的aside</p></aside><footer class="where-styling"><p>我是where的footer</p></footer>
</template><script setup></script><style lang="scss" scoped>
:is(section.is-styling, aside.is-styling, footer.is-styling) p {color: red;
}:where(section.where-styling, aside.where-styling, footer.where-styling) p {color: green;
}
</style>

栗子实现的效果-:is和:where后跟选择器列表时-作用一样:

在这里插入图片描述

:where后跟条件的栗子完整代码

<template><section class="is-styling"><p>我是is的section</p></section><aside class="is-styling"><p>我是is的aside</p></aside><footer class="is-styling"><p>我是is的footer</p></footer><br /><section class="where-styling"><p>我是where的section</p></section><aside class="where-styling"><p>我是where的aside</p></aside><footer class="where-styling"><p>我是where的footer</p></footer></template><script setup></script><style lang="scss" scoped>
section:where([class^="where"]) {font-weight: bold;
}
</style>

上面栗子实现的效果:
在这里插入图片描述

相同点: :is、:where选择器都是根据条件来定位元素。
不同点: :is()用于根据选择器列表匹配元素,保持原有选择器的特定性。而:where()不仅可以根据选择器列表匹配元素,而且可根据条件匹配元素。同时降低选择器的优先级,避免因选择器过长而导致的优先级问题

:has

可以用于**基于后代元素**来定位元素,根据元素内部是否存在特定的子元素来选择该元素。

栗子完整代码

<template><section class="is-styling"><p>我是is的section</p></section><aside class="is-styling"><p>我是is的aside</p></aside><footer class="is-styling"><p>我是is的footer</p></footer><br /><section class="where-styling"><p>我是where的section</p><span>我是span元素</span></section><aside class="where-styling"><p>我是where的aside</p></aside><footer class="where-styling"><p>我是where的footer</p></footer></template><script setup></script><style lang="scss" scoped>
section:has(span) {background: orange;
}
</style>

栗子实现的效果:
在这里插入图片描述

关键字:css-伪类选择器

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: