Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS <style scoped> applies outside of the scope

Tags:

html

css

The most simple example of scoped style doesn't work in Chrome (v 25):

<div>
    <h1>Hello 1</h1>
</div>
<div>
    <h1>Hello 2</h1>
    <style scoped> h1 { color: red; } </style>
</div>

Try it: http://jsfiddle.net/RWW8r/2/

Both h1 become red:

enter image description here

The scope style should only apply to the second h1.

I read that the functionality was implemented in Chrome, why doesn't that work? Am I doing something wrong?

like image 851
Matthieu Napoli Avatar asked Feb 28 '13 21:02

Matthieu Napoli


People also ask

What is scoped in style CSS?

The scoped attribute is a boolean attribute. If present, it indicates that the styles are intended just for the subtree rooted at the style element's parent element, as opposed to the whole Document.

What is the difference between scoped and non scoped CSS?

If you add css in global file you can import any where , but when you are doing with scoped css it will connect styling with respective component only.

Does CSS have scope?

The :scope CSS pseudo-class represents elements that are a reference point for selectors to match against. Currently, when used in a stylesheet, :scope is the same as :root , since there is not at this time a way to explicitly establish a scoped element.

What is scoped style in Vue?

Scoped styles allow us to apply CSS to the components element only. Which ultimately enables us to mix local component and global application styles. In this lesson, we'll learn how to use scoped styles in our Single File Components and what to be aware of when using them.


2 Answers

You are not doing anything wrong. As of this moment, scoped CSS is still an experimental feature which is not supported by any current browser.

However, if you want to play around with it in Chrome you can do the following thing:

  • Go to chrome://flags/ in your Chrome browser;
  • Find "Enable experimental WebKit features." and click enable
  • Restart your browser.
  • Try your code. It should work.
like image 126
R T Avatar answered Oct 06 '22 11:10

R T


I think when you read that Chrome has implemented it, they meant that it was in the development version, because it certainly isn't in the current release v25.

See the CanIUse site for compatibility charts: http://caniuse.com/style-scoped

Same applies to Firefox; it's only in the current alpha.

Given the pace of development, it'll be in the release version of both these browsers in a relatively short space of time, but the short answer is that it's not there now.

like image 2
Spudley Avatar answered Oct 06 '22 12:10

Spudley