Skip to content

style tag should be allowed in body #1605

Description

@workguy66

This is a follow up on the issue on style scoped

We allow <link rel=stylesheet> in the body, we should allow <style> tags as well.

It's already supported in all the browsers and will be in the future, what is the benefit in making it invalid markup if it's often the best or even only solution to a problem and is widely used on many websites and CMS's?

This would also serve as a half functioning replacement for <style scoped> where you can work around the scoping issue using custom classes.

Also, I've seen some people ask, why not use inline styles? like <span style="color:red"></span>?
Well, you can't apply inline styles for hover states or pseudo elements, it also means a lot of repeated css most of the time.

Is it good to use it in the body instead of the head? No if you can help it, yes if you can't.

Edit:
Also many people like the convenience of doing ajax requests that come with html and their own style tags.

Activity

  1. added a commit that references this issue on Jul 28, 2016
    e4fcec0
  2. prlbr commented on Jul 28, 2016

    @prlbr

    Thanks, this is nice.

    (But I disagree with this being a “half functioning replacement for <style scoped>”.)

  3. workguy66 commented on Jul 28, 2016

    @workguy66
    Author

    @prlbr That's fine, it's not really a replacement for scoped but can be worked with. :)

  4. neil-h commented on Jul 28, 2016

    @neil-h

    I'm just going to point out that the obvious case for a style tag, over using inline styles, would be table cells.

  5. domenic commented on Jul 28, 2016

    @domenic
    Member

    I don't think we should change this.

    <link> is very different from <style>, in that the former causes a fetch. As such, in the original bug https://www.w3.org/Bugs/Public/show_bug.cgi?id=27303, the arguments were about delaying this fetch. They're performance arguments, in other words.

    <style> in body has a number of negative consequences, such as the famous "flash of unstyled content" (FOUC) and the re-paint and re-layout required. There are good reasons for keeping all styling in the head. For <link>-based styling, it turns out there are even better reasons for allowing it in the body (although doing so would require careful performance measurement). But I have seen no evidence of that for <style>.

    I'd like to hear from @igrigorik, who initiated the push for <link> in body.

  6. added and removed
    good first issueIdeal for someone new to a WHATWG standard or software project
    on Jul 28, 2016
  7. domenic commented on Jul 28, 2016

    @domenic
    Member

    Also, just to be very clear: this entire issue is not about changing browser behavior. The spec already requires, and browsers already implement, processing for <style> in body. It works, albeit with the negative effects I discussed above.

    This issue is entirely about what conformance checkers like https://checker.html5.org/ will do if you include <style> in body. Will they tell you that's bad practice and disallowed by the spec's authoring requirements? Or will they let it happen silently?

  8. sideshowbarker commented on Jul 29, 2016

    @sideshowbarker
    Member

    <style> in body has a number of negative consequences, such as the famous "flash of unstyled content" (FOUC) and the re-paint and re-layout required. There are good reasons for keeping all styling in the head.

    I strongly agree that if in practice <style> in body results in significant cases of things that are bad for end users—like FOUC and slower performance—then that should be a sufficient reason to continue to make it non-conforming. Because we want to be helping developers to avoid making authoring mistakes that cause actual bad user experience.

    That said, I wonder how we can assess how many such cases of FOUC and slowed performance the existing uses of <style> in body in documents on the Web are causing for users.

  9. sideshowbarker commented on Jul 29, 2016

    @sideshowbarker
    Member

    I added a <style>-in-body use counter to statistics collected by the W3C HTML checker:

    https://validator.w3.org/nu/stats.html

    I just now added it but the initial results indicate that about 14% of the documents it checks have <style> in body. [Update 2017-02-07: The long-term data from the use counter indicates that about 17% of documents the HTML checker processes have <style> in body.]

    Some more well-known sites with <style> in body that I’ve come across so far in the logs for the checker include www.amazon.com www.pinterest.com www.bing.com/search?q=foo www.ebay.com www.gettyimages.co.uk www.weightwatchers.com www.quora.com & www.google.de www.google.it www.google.pl www.google.co.in www.google.com.hk www.google.bg & other Google search sites.

    The HTML checker and legacy W3C validator have always reported an error for <style> in body—for 20 years or whatever now—so if in spite of that we still have 14% of sites being run through the checker that are using <style> in body anyway, that means a lot of developers are just ignoring the error message and not considering it a mistake. So this might be a case where making something non-conforming is in practice not actually having much effect on preventing its use.

    But it would be good to look at more relatively widely-used sites and see what evidence can be found of FOUC or re-paint/re-layout affecting users of those sites.

    In the end not matter what the numbers are, if it seems like keeping <style> in body non-conforming is the right way to try to ensure the best user experience for users, then I support continuing to make it non-conforming.

  10. workguy66 commented on Jul 29, 2016

    @workguy66
    Author

    @sideshowbarker I think the validators should give out a warning as a potential error and not as an actual error, that is the key difference here.

    Just a fun link from stackoveflow, I like the top answer being "Doesn't matter"... http://stackoverflow.com/questions/4803518/why-can-t-style-tags-within-the-html-body-tag-validate-on-w3c
    http://stackoverflow.com/questions/1303416/does-style-have-to-be-in-the-head-of-an-html-document

    I think standards matter and should not be implemented in a way that promotes them to be ignored.

    The w3c validator is seen as an authority and often means that if it says that something is not allowed then it must never be done, and some systems actually penalize you for not having it valid and I find that silly in the case of the <style> tag in the body because it works reliably and people use it.

    If the flash is the only issue here, then the validator should warn about it, but not prohibit the usage...

  11. sideshowbarker commented on Jul 30, 2016

    @sideshowbarker
    Member

    @sideshowbarker I think the validators should give out a warning as a potential error and not as an actual error, that is the key difference here.

    My experience in dealing with issue reports from users of the validator is that many or most users don’t actually care about the difference between a validator message flagged as an error and one flagged as a warning. Regardless of what category the message is, they just want it to go away.

    I find that silly in the case of the <style> tag in the body because it works reliably and people use it… If the flash is the only issue here, then the validator should warn about it, but not prohibit the usage

    There are a number of existing cases where we have made a particular markup case non-conforming in the spec because even though it can be made to work reliably, it also carries a high risk of being used wrongly and resulting in degraded user experience.

    The rationale is to attempt to help prevent developer-authors from shooting themselves in the foot by unknowingly using something that ends up making the user experience of their content worse at least for some portion of users (whether it be users of older browsers, browsers on older devices, screen-reader users, users on networks with very limited bandwidth, etc.)

  12. 12 remaining items

  13. domenic commented on Feb 25, 2017

    @domenic
    Member

    Shouldn’t the spec reflect reality?

    That's phrased kind of strangely. "reflecting reality" is usually a question about the spec reflecting the reality of browser implementations, in its implementation specifications. It's not a question about the spec sanctioning any bad markup, in its author-facing markup guidelines.

    If we just specced any sufficiently-popular markup, we'd do lots of weird things, like speccing <i> to mean "icon".

    So I don't think "reflecting reality" is relevant here.

    If <style> in body is as bad as you say, performance-wise, then why do top websites use it anyway? Do the benefits outweigh the disadvantages after all?

    That's a good question. You'd have to ask them to be sure, but we have to note that in general the top websites are not very good at performance. This leads to lots of railing against them on Twitter by certain devrel people, of course, which is a whole other situation. Often these websites choose to sacrifice performance and user experience for the sake of advertising revenue, or developer experience, or their CMS tooling... you'd have to ask each individual site to see why. In some cases they might have made a calculated business decision that other factors are more important than writing conformant documents. (How many of those websites also pass the other checks on https://checker.html5.org?)

  14. workguy66 commented on Feb 27, 2017

    @workguy66
    Author
  15. domenic commented on Feb 27, 2017

    @domenic
    Member

    I agree with this statement, but as I said before, if the spec says that you must not do something and then looks the other way when people do it, then it's the spec at fault here.

    I think we're just going to have to fundamentally disagree here.

    Let's look at some of the other things the spec is "looking the other way" on, using the example of www.amazon.com: https://checker.html5.org/?doc=https%3A%2F%2Famazon.com

    As you can see, there are many many pieces of invalid HTML on popular sites. We're not going to say that all of them are valid, just because people do it.

  16. unilobo commented on Aug 17, 2017

    @unilobo

    I agree that if we allow link in the body then we should also allow style, especially as using style offers performance improvements over link, and according to w3c it is correct markup and and pretty much supported by every browser on the market.

    If you are using a modular or component based approach, it seems reasonable to have a way of attaching any styles and also scripts.

  17. domenic commented on Aug 17, 2017

    @domenic
    Member

    @unilobo I think you may not have read the whole thread. This is not about browser support; and style creates performance problems over link, as attested to by several vendors per the above. See especially the two comments at #1605 (comment).

  18. unilobo commented on Aug 17, 2017

    @unilobo

    Hi @domenic, although I get your point, as you admit this has been implemented already by pretty much every browser and is a w3c recommendation. If I have modules or components that sit on my page, I would like to have a way to load any related required styles. It then makes sense to include these inline, as indeed w3c says is possible,

    However because wcag objects to this practice it breaks w3c validation.

    However as far as i know there is no alternative way of doing this, unless i use link which would cause an additional http request for each file. which would be more sub optimal.

    I am open to your suggestion(s) of how best to do this, although I acknowledge putting all styles in the head is the preferred method, in my case that is not really an option unless I postprocess my styles into one file that again would be a suboptimal solution.

    I know frameworks like Facebook's React basically inline there styles to make their components atomic. In my case I do not want to go down this route but I would like to componentise or decouple my modules that sit on the page. As WCAG has also objected to the adoption of the scoped attribute, web developers are left in limbo.

    If you have another suggestion of how to implement this without using link, i would be keen to hear it?!

  19. unilobo commented on Aug 18, 2017

    @unilobo

    I agree that ideally styles should be in the head.

    I guess my point is that in certain circumstances, you may want to load the styles inline, and it reasonable that there should be a solution that validates.

    The frustration is that there does not appear to be a solution that validates.

  20. prlbr commented on Aug 18, 2017

    @prlbr

    I kind of understand the position of WHATWG. They acknowledge that there are situations in practice where you need to put <style> inside <body>, e.g. because of factual limitations of CMS that are not fixed yet and maybe never will. It works and that’s fine. But they also think that it is a bad practice, so they don't want to call it valid, so that people are discouraged from using it. It makes sense from a certain point of view.

    But this also means that some people like me are educating people that validation does not matter anymore, at least validation concerning WHATWG's HTML standard. It's not very hard to convice people, when they see that ignoring validation provides them the solutions they seek, that it works perfectly and you can show them that huge companies deeply involved in internet technologies like Google ignore the standard with their flagship products deliberately too.

  21. tedw commented on Jun 8, 2020

    @tedw

    My takeaway is that under ideal circumstances (including a fast network connection and small page), with carefully-crafted markup, style-in-body can be made to work. But in the general case, it is error-prone and likely to lead to expensive restyling, relayout, and repainting. As such we should continue to recommend against it.

    Can’t something be valid markup even if it’s a bad practice in certain circumstances? For example, tables are valid even though using them for layout is a bad practice.

    Since validation tools like https://validator.w3.org can’t determine if a particular use of <style> in <body> is a performance or security issue, I think a warning would be better than labeling it invalid. At the very least, adding a link to this thread after the invalidation warning would be nice.

    Anyway, completely agree that browser support and prevalence on popular sites isn’t a good enough reason to make something valid.

    Appreciate all of your replies above, and your work for WHATWG 🙌

  22. mangelozzi commented on Nov 9, 2021

    @mangelozzi

    I would like to point out fire can be dangerous, so according to case why not to have style in the body, we we should never use fire. Although fire is dangerous, its also very useful.

    There are very good cases for doing so. Most of the time people do it with styles that only affect a component that follows the newly added style.

    Instead of treating us developers like children, please treat us like consenting adults. There is lighthouse and many tools to help one write better code.

    If its really not allowed, then stop supporting it so some other solution can be developed. I think it will never be pulled because a huge portion of the web will break. So instead of pretending can we not make the spec follow what the html parser actually allows.

  23. simevidas commented on Jan 8, 2022

    @simevidas

    Sorry for continuing this discussion, but I’ve read through the thread, and one important question does not seem to have been answered yet: Do we have proof that <style> in <body> significantly affects performance on a real website?

  24. dieulot commented on Nov 9, 2023

    @dieulot
    Contributor

    But in the general case, it is error-prone and likely to lead to expensive restyling, relayout, and repainting.

    @domenic If style-in-body would be limited to the upcoming CSS @scope rule, wouldn’t the performance issues disappear in practice?

    If so, could the HTML standard get such a rule (style in body allowed if it contains only @scope rules) or would it need a hint such as a scoped attribute?

  25. o-t-w commented on Apr 30, 2025

    @o-t-w

    I also wonder about whether @scope changes the situation? @scope is location dependent so it might be useful to place a <style> tag within the <body> when working with it (although there are also other ways to work with @scope)?

    The CSS Cascading and Inheritance Level 6 spec itself uses the following example code:

    <div>
      <style>
        @scope {
          p { color: red; }
        }
      </style>
      <p>this is red</p>
    </div>
    <p>not red</p>
  26. HansBrende commented on Sep 16, 2026

    @HansBrende

    @domenic I agree with the above: with @scope having special semantics based on its parent element (implying that it must be a descendant of the <body> for those semantics to matter), it really makes no sense for the spec to continue to say that <style> must not be in the <body>.

    Should this issue not be re-opened in light of the @scope rule, which as of March 2026 is supported by all major browsers?

  27. annevk commented on Sep 16, 2026

    @annevk
    Member

    Could someone file a new issue for that specific suggestion? That would make it easier to consider than requiring everyone to wade through this entire discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions