Marking Script and Style as CDATA

When using XHTML, the contents of <script> and <style> elements must be marked as CDATA. This is essential when serving XHTML correctly (as application/xhtml+xml).

When possible, use external script and style files and reference these from your XHTML document (behavioural and presentational separation). However, the examples below show how to place <script> and <style> inside your XHTML document correctly. This method should be compatible with older browsers, and therefore degrades gracefully.

Marking <script> as CDATA:

<script type="text/javascript">

Marking <style> as CDATA:

<style type="text/css">
  <!--/*--><![CDATA[/*><!-- */


If you are serving ASP.NET pages as application/xhtml+xml (see Serving the Correct MIME Type for XHTML using ASP.NET 2.0), then the automatically generated JavaScript will not be marked as CDATA, and will therefore not work. For a workaround see: Marking ASP.NET 2.0 Generated JavaScript as CDATA.