Why are styles in a <style> tag instead of in an external file? #41793
Replies: 4 comments 4 replies
-
I'm not too sure of the origins of why global styles are output in a
There's currently work underway to attempt to consolidate many of the different places where styles are generated. Some of this work is being tracked in the style engine tracking issue (#38167). While consolidating to a single (or close to it) stylesheet is a really good idea, I think it could be challenging attempting to balance the features that conditionally output styles based on what's currently rendered (e.g. what was introduced in #41160) with attempting to have a consistent output across page views. The idea of moving to loading the styles via the global styles endpoint is a very compelling idea, though! Within style engine work, looking into this is currently slated for once there's better consolidation of how styles are output, but it's worthwhile having the discussion here to imagine how things might work one day. CC: @ramonjd |
Beta Was this translation helpful? Give feedback.
-
Let's break down the benefits mentioned above, one by one... Cache-ableA browser will do the following:
Files caching comes in on step 2: The 1st time the browser visits a page, it will get the Performance-wise, it is a lot better to have styles inline when those styles are small in size. You can watch an interesting discussion about inlining CSS assets on https://www.youtube.com/watch?v=3sMflOp5kiQ too 👍 Version-ableVersions in assets make sense when using Easily import/exportableI don't understand this one... Why would you need to import/export styles that are generated on-the-fly? Smaller html documentThis is the only real downside I see in inlining CSS. The HTML does indeed get bigger - and uglier to look at. Global styles are currently a moving target. It's an API that is getting constant improvements and development. The size of styles printed gets constantly tweaked, and the goal is only to print the styles that are relevant for a page, depending on the page's actual contents and blocks used. Something like that will not be possible unless these styles are inlined, and in the future, there will be a lot fewer styles present on a page as a result of these optimizations. In an ideal scenario, when I visit a page, the browser will only need to make a single request: Get the HTML and print it. We're not there yet, but that is an admirable goal which will benefit performance, sustainability, accessibility and democratizing content. |
Beta Was this translation helpful? Give feedback.
-
Also thanks for the great responses, folks!
This is a narrow aspect of the overall issue, but there's active work that is attempting to reduce the impact of block-specific styles. See for example this issue: #41434 and the PR that is addressing such issues: #40875 |
Beta Was this translation helpful? Give feedback.
-
What is overlooked in this discussion is the impact on security with generating inline styles. It makes it very difficult to build a robust content security policy in inline styles. Whether to have styles brought inline or not should be a setting a WordPress administrator could set. For a site using asynchronous loading http2 and a CDN, having styles in css files is preferable to having them inline. |
Beta Was this translation helpful? Give feedback.
-
It seems like there are two interesting ways to make block & FSE styles external:
This would offer several advantages over inline styles:
Is there a discussion around this idea somewhere? I'd like to understand how we got to a giant <style> tag.
Beta Was this translation helpful? Give feedback.
All reactions