While an HTML style element for inline CSS needs nothing but simple start and end tags (as of HTML5 and later)
<style>
p { color: red }
</style>
a more robust style element requires a precise series of overlapping code comments.
Here is the answer if you want a code snippet to copy & paste
<style><!--/*--><![CDATA[*/
p { color: red } /* you may delete this sample style rule */
/*]]><!--*/--></style>
Here is why:
1. Not all HTML processors are CSS processors. While all modern browsers know how to parse CSS in style elements inside HTML, it is still quite reasonable for people to build HTML processors that do not, and many exist. There are plenty of ways to errantly or deliberately misplace markup inside a style element, like in a CSS comment, that such processors will not see, that can break them and cause unexpected and different results in different processors. Strictly speaking any use of > child combinator selector syntax should also be HTML escaped (as >) inside a style elment.
Thus it makes your HTML more parseable, by more processors, if you can hide the entirety of the style sheet inside the style element from such processing, including any child combinators. A CDATA section does exactly that:
<style><![CDATA[
p { color: orange } /* CDATA allows a </style> here to not close the element */
body > p { margin: 1em } /* CDATA also allows an unescaped > child combinator */
]]></style>
2. However CSS syntax does not recognize a CDATA directive (even as of the latest published CSS Syntax Module Level 3¹ or editor's draft² as of this writing). CSS parsers may very well treat a CDATA directive as a syntax error that invalidates the subsequent style rule.
Thus we must hide the CDATA directive, its opening and closing markup, from CSS parsers. CSS code comments /* ... */ can do exactly that:
<style>/*<![CDATA[*/
p { color: orange } /* CDATA allows a </style> here to not close the element */
body > p { margin: 1em } /* CDATA also allows an unescaped > child combinator */
/*]]>*/</style>
3. This is close but still exposes HTML processors that do not process CSS to a minimal bit of content, the CSS comment opener and closer that are outside the CDATA section:
/* */
This recently showed up in a draft of the This Week in The #IndieWeb newsletter³, because portions of it are automatically constructed by parsing the HTML of MediaWiki pages for content, and one of those used a MediaWiki template that included a minimal style element to style the marked up content inserted by the template. A draft of the newsletter was showing raw CSS, extracted as text from the style element by the CSS-unaware parser extracting content. I was able to hide nearly all of it using CSS comments around the CDATA section opener and closer. Except for that little bit of CSS comment noise outside the CDATA section: /* */
Fortunately there is one more tool in our toolbox that we can use. Simple HTML/SGML comments <!-- --> are ignored at the start and end of style sheets⁴ (noted there as CDO-token⁵ and CDC-token⁶), and thus we can use those to hide the last two remaining CSS comment pieces that were leaking out, like this: <!-- /* --> and <!-- */ -->. Note that the portion of the HTML comment directives that are inside CSS comments are ignored by CSS processors, which is why this works for both processors that parse CSS and those that do not.
This last addition produces our answer, with no fewer than three different comment mechanisms (CDATA, CSS, HTML/SGML), overlapping to hide each other from different processors:
<style><!--/*--><![CDATA[*/
p { color: orange } /* CDATA allows a </style> here to not close the element */
body > p { margin: 1em } /* CDATA also allows an unescaped > child combinator */
/*]]><!--*/--></style>
By replacing those informative style rules with a style rule to be deleted, we have recreated the code snippet to copy & paste from the top of the post:
<style><!--/*--><![CDATA[*/
p { color: red } /* you may delete this sample style rule */
/*]]><!--*/--></style>
Q.E.D.
Afterword:
If you’re reading this in a traditional feed reader and see any red or orange text, then your feed reader has a bug (or a few) in its HTML parsing code.
If you View Source on this post’s original permalink or my home page you can see the more robust style element in a real world example, following the IndieWeb Use What You Make⁷ principle.
#CSS #style #styleElement #styleSheet #HTML #HTML5 #CSSsyntax #codeComments #CDATA #SGML #CSScomment #HTMLcomment #SGMLcomment
Glossary:
CDATA
https://en.wikipedia.org/wiki/CDATA
CSS — Cascading Style Sheets
https://en.wikipedia.org/wiki/CSS
HTML — HyperText Markup Language
https://en.wikipedia.org/wiki/HTML
HTML5
https://en.wikipedia.org/wiki/HTML5
IndieWeb Principles
https://indieweb.org/principles
MediaWiki
https://en.wikipedia.org/wiki/MediaWiki
original permalink
https://indieweb.org/original_permalink
Q.E.D.
https://en.wikipedia.org/wiki/Q.E.D.
References:
¹ https://www.w3.org/TR/css-syntax-3/
² https://drafts.csswg.org/css-syntax/
³ https://indieweb.org/this-week-in-the-indieweb
⁴ https://www.w3.org/TR/css-syntax-3/#stylesheet-diagram
⁵ https://www.w3.org/TR/css-syntax-3/#CDO-token-diagram
⁶ https://www.w3.org/TR/css-syntax-3/#CDC-token-diagram
⁷ https://indieweb.org/use_what_you_make