Search code examples
htmlformattingtags

Tag to ignore HTML formatting code?


I thought the <code> tags were supposed to ignore HTML like with BBcode, but that doesn't seem to be the case. Is there a way to achieve this in HTML so I can show code examples? I found a page about <plaintext> and <xmp>, but they're apparently deprecated, so what should I use? I've seen <!-- --> tags for comments, but those are completely hidden from view. I just need something to ignore tags.


Solution

  • In HTML, the only solution guaranteed to work everywhere is to escape the code manually.

    <code>
      &lt;img src="https://www.google.co.uk/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"&gt;
    </code>
    
    • Replace the < character with &lt;
    • Replace the > character with &gt;
    • Optionally surround your HTML sample with or without tags. (e.x. <code> or <pre>)