Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between <pre> and <code> HTML Tag? [duplicate]

Tags:

html

html4

How does a <pre> HTML tag differs from <code> html tag.

I have checked on W3Schools page, it seems they are just the same.

If there any major difference between them ?

like image 315
Parimal Raj Avatar asked Apr 17 '13 16:04

Parimal Raj


People also ask

What is pre and code in HTML?

Definition and Usage The <pre> tag defines preformatted text. Text in a <pre> element is displayed in a fixed-width font, and the text preserves both spaces and line breaks. The text will be displayed exactly as written in the HTML source code.

What is the difference between P and pre tag in HTML?

The <p> tag defines a paragraph. Browsers automatically add some space (margin) before and after each <p> element while, the <pre> tag defines pre-formatted text. Text in a <pre> element is displayed in a font (usually Courier), and it preserves both spaces and line breaks.

What is code tag in HTML?

Definition and Usage. The <code> tag is used to define a piece of computer code. The content inside is displayed in the browser's default monospace font.

What is the advantage of preformatted pre tag in HTML?

The <pre> tag in HTML is used to define the block of preformatted text which preserves the text spaces, line breaks, tabs, and other formatting characters which are ignored by web browsers.


2 Answers

Don't go to w3schools. Check the w3c documentation for pre and code instead.

PRE:

The PRE element tells visual user agents that the enclosed text is "preformatted". When handling preformatted text, visual user agents:

  • May leave white space intact.
  • May render text with a fixed-pitch font.
  • May disable automatic word wrap.
  • Must not disable bidirectional processing.

CODE:

Designates a fragment of computer code.

like image 116
gpojd Avatar answered Oct 06 '22 00:10

gpojd


<pre> says "The whitespace in this element is important" (so new lines and spaces get rendered as new lines and spaces).

<code> says "This is code".

They have nothing in common aside from being HTML elements.

W3Schools page, it seems they are just the same

W3Schools is often wrong, often out of date, and often teaches worst practises. If they claim the two elements do the same thing, then that is just another reason to avoid the site.

like image 43
Quentin Avatar answered Oct 06 '22 00:10

Quentin