code is an HTML tag that is used to specify a fragment of source code within an HTML document. It is typically not good for specifying long pieces of code because it ignores newlines and carriage returns, so depends largely on the br (line break) tag which can clutter up the code. It is usually much cleaner to utilize the pre (preformatted text) tag for long code segments.

See also: font, kbd, plaintext, pre, samp, tt, var

Attributes

The code tag does not have any required HTML attributes. Regardless of this fact, and the fact that attributes are rarely used with this tag, the following are all valid:

Usage

To use the code tag, simply place opening and closing HTML tags around the source code. For example:

<code>bool isLeapYear = ((!(year % 4) && (year % 100)) || !(year % 400));</code>

NOTE: The line C++ code above determines if a given year is a leap year, assuming that the value of year contains the year in question.

On browsers that support this tag, this will typically cause the source to be displayed in a monospace font. It should be noted that you cannot use the greater than ('>') or less than ('<') symbols in your code because these are used for delimiting HTML tags. You should therefore type &gt; to output the greater than symbol, and &lt; to output the less than symbol. A much easier solution is to simply copy and paste your code into the E2 Source Code Formatter which will format it correctly for web use.

Everything2 Support?

E2 does provide limited support for the code tag. It does not allow the use of any of its HTML attributes. Below is how your browser displays the example above here on Everything2:

bool isLeapYear = ((!(year % 4) && (year % 100)) || !(year % 400));

Similar to the problem with '<' and '>' above, keep in mind that on E2, square brackets ('[' and ']') can also cause problems with your source code. Again, use the code formatter to fix these problems. If you would like to use this tag with any of its attributes, you can do so in your Notelet Nodelet.

Common Browser Implementations*

Most web browsers support this tag, as it has been around since at least HTML 2. Most browsers will display the code in a monospaced font such as Courier New.

Previous HTML Tag: cite
Next HTML Tag: col (next E2 supported HTML tag: dd)
See Also: HTML tags and HTML attributes


* Please feel free to send me information about how other browsers implement this tag.