I sometimes notice a CSS or JS code surrounded with <![CDATA[
and ]]>
tags. I couldn't find any explanation to this. What does CDATA do? Why is it used by some people and what is it needed for?

- 13,543
- 9
- 37
- 39
-
See also http://stackoverflow.com/questions/3577524/why-should-you-use-xml-cdata-blocks – jasso Oct 04 '10 at 13:00
4 Answers
It tells the interpreter that it should not try to interpret the data enclosed in the tags. For example, if you want a XML file to contain a comment with < or >, XML interpreters will report the file as invalid because the caracters < and > will not be part of standard tag. You simply have to surround the code with the CDATA tags.

- 3,712
- 4
- 25
- 46
It marks data that shouldn't be parsed - in most cases, just HTML or text that needs to be written directly to the page.
Further explanation: http://www.w3schools.com/xml/xml_cdata.asp

- 144
- 5
-
1To be precise, data that shouldn't be parsed as XML / by the XHTML rendering engine. – Arc Sep 28 '10 at 14:13
Here's some info for you: http://en.wikipedia.org/wiki/CDATA
How you couldn't find anything on this is a mystery ;)

- 29,197
- 4
- 84
- 98
When you write inline javascript or CSS in an HTML file (using the script
and style
tags), Those CDATA comments prevent things like the HTML validators from catching false positives when validating an HTML document.

- 9,994
- 3
- 29
- 31