Show Navigation
Notices tagged with html5
-
While an HTML style element for inline CSS needs nothing but simple start and end tags (as of HTML5 and later)
<style>
p { margin:0 }
</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 { margin:0 } /* 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 that errant or deliberately misplaced 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.
Thus it makes your HTML more parseable, by more processors, if you can hide the entirety of the style sheet inside the style elmenet from such processing. A CDATA section does exactly that:
<style><![CDATA[
p { margin:0 } /* CDATA allows a </style> here to not close the element */
body > p { margin:1em } /* CDATA 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 { margin:0 } /* CDATA allows a </style> here to not close the element */
body > p { margin:1em } /* CDATA 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 { margin:0 } /* CDATA allows a </style> here to not close the element */
body > p { margin:1em } /* CDATA 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 { margin:0 } /* you may delete this sample style rule */
/*]]><!--*/--></style>
Q.E.D.
Afterword:
If you View Source on this 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
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
-
@einebiene Netflix explicitly has been actively promoting, pushing and implementing #EME in #HTML5 as we know from @dbd et al.
DVDs are bad but not I-don't-want-to-communicate-with-such-a-bad-person bad.
-
#HTML5 instead of YouTube video of music + album art:
<video src="m.mp3" poster="a.jpg" controls=""></video>
ht: @zcorpan
-
@fnadde42 Meh, what does it matter when #Netflix has made sure to implement #DRM in #HTML5 and thus noone will be able to right-click to copy anything at all anyway?
-
And no, #BBC et al., the problem isn't that "Media playback is unsupported on [my] device", it's that the open standard for media playback isn't supported by you. #NoScript #HTML5
-
And no, #BBC et al., the problem isn't that "Media playback is unsupported on [my] device", it's that the open standard for media playback isn't supported by you. #NoScript #HTML5
-
@cyberunions Yes you have new listeners. And they're coming from !gnusocial :] I'm wondering however if you could add time tags (I think that should be pretty easy with an #HTML5 player and simple javascript - or people who don't u…
-
Thanx @CBC, @CBCradio! #HTML5 support would be good; a link to plain audio files (mp3, ogg) would be better
-
Just reminding everyone: #Netflix coerced the #W3C into implementing #DRM in #HTML5. They e…
-
I attached a video to a notice on my local !gnusocial install. On pages containing that notice, #firefox…
-
Developing is too fun. Especially when I generally just complete stuff I've half-completed …
-
The element even uses the #HTML5 <time/> and is, I believe, properly marked up for #microformats2 ;)
-
#HTML5 visualizations of aesthetic proof-of-work schemes for #cryptocurrencies. e.g. "Cryptocu…
-
#HTML5 visualizations of aesthetic proof-of-work schemes for #cryptocurrencies. e.g. "Cryptocu…
-
@inscius That is my experience also, even with the #HTML5 addon.
-
@question I am starting to hate #YouTube. Whenever I visit to hear a song it wants #flash, not #HTML5. Rubbish.
-
@question I am starting to hate #YouTube. Whenever I visit to hear a song it wants #flash, not #HTML5. Rubbish.
-
Congrats! 5 days ago @hellekin and team finished Volume I, Number 0 of their monthly #newslett…
-
Congrats! 5 days ago @hellekin and team finished Volume I, Number 0 of their monthly #newslett…
-
«5 HTML5 Features you need to know» ~ http://yh5.ca/95 #WebDev #HTML5