简体   繁体   English

HTML 元素样式在作用域样式中不起作用

[英]HTML Element styling not working in scoped style

For some reason my element styling isn't working when using scoped.出于某种原因,我的元素样式在使用作用域时不起作用。 When inspecting the element the styling is not applied when using scoped.在检查元素时,使用作用域时不应用样式。 I need to use scoped because I want the styling only be applied within this component.我需要使用作用域,因为我希望样式只应用在这个组件中。 I'm using nuxt.js, no idea if this has anything to do with the problem.我正在使用 nuxt.js,不知道这是否与问题有关。

Not working:不工作:

<style scoped>

    a {
        color: red !important;
        text-decoration: underline !important;
    }

</style>

Working:在职的:

<style>

    a {
        color: red !important;
        text-decoration: underline !important;
    }

</style>

Any ideas?有任何想法吗?

As explained in the relevant documentation , scoped CSS applies a data- attribute to all selectors in the provided CSS so that it only applies to the elements of the component, not outside of it, nor to nested sub-components.相关文档所述,作用域 CSS 将data-属性应用于提供的 CSS 中的所有选择器,以便它仅适用于组件的元素,而不适用于组件外部,也不适用于嵌套的子组件。

For example, with this markup:例如,使用此标记:

<app>
  <a>outside link</a>
  <Parent>
    <a>parent link</a>
    <Child>
      <a>child link</a>
    </Child>
  </Parent>
</app>

scoped CSS of <Parent> will only affect parent link and will not affect the outside link nor the child link. <Parent> scoped CSS 只会影响父链接,不会影响外部链接子链接。

From what you're describing, you are trying to style a sub-component link.根据您的描述,您正在尝试设置子组件链接的样式。


To make your scoped CSS selectors affect deeply (apply to sub-components as well) you have to use the deep >>> combinator:为了让你的作用域 CSS 选择器产生深远的影响(也适用于子组件),你必须使用 deep >>>组合器:

<style scoped>
  * >>> a {
    color: red;
    text-decoration: underline;
  }
</style>

To see it in action, consider this example .要查看它的实际效果,请考虑此示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM