css-伪类选择器

选择器

伪类选择器

: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>

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

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值