HyperText Markup Language/Tag List/code

From Wikibooks, open books for an open world
Jump to: navigation, search

Use[edit]

With the code element you can define, as the name says, code. This could be any code, PHP, C++, HTML anything. Anywhere you have some code in a line of text you should use a code element to indicate it, e.g.

The html element must contain exactly one body element.

Using the element consistently allows you to specify a special layout in CSS for code.

If you are writing an article that uses code you will probably want to use the related elements:

var 
a variable in computer code;
kbd 
text the reader should type at the keyboard;
samp 
sample output from a program or script.

Example[edit]

<code><?php echo 'Hello World!';?></code>

Multi-line blocks of code should be wrapped in a pre element to preserve formatting.

<pre>
 <code>
  // Prints the authors' names as
  //  surname, forename
  for (Iterator i = authorList.iterator(); i.hasNext(); ) {
    Person author = (Person)i.next();
    System.out.println(author.surname() + ", " + author.forename());
  }
 </code>
</pre>

Standard attributes[edit]

More info[edit]

Tag phrase elements on W3Schools