HyperText Markup Language/Tag List/code

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Contents

[edit] Use

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.

[edit] Example

<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>

[edit] Standard attributes

[edit] class

[edit] dir

[edit] id

[edit] lang

[edit] style

[edit] title

[edit] xml:lang

[edit] More info

Tag phrase elements on W3Schools

In other languages