Computer Code

11-2-24

HTML contains several elements for defining user input and computer code.

Some programming code:

x = 5;
y = 6;
z = x + y;

The kbd Element

The HTML <kbd> element is used to define keyboard input. The content inside is displayed in the browser's default monospace font.

The kbd element is used to define keyboard input:

Save the document by pressing Ctrl + S

The samp Element

The HTML <samp> element is used to define sample output from a computer program. The content inside is displayed in the browser's default monospace font.

The samp element is used to define sample output from a computer program.

Message from my computer:

File not found.
Press F1 to continue

The code Element

The HTML <code> element is used to define a piece of computer code. The content inside is displayed in the browser's monospace font.

Programming code example

x = 5;
y = 6;
z = x + y;

The code element does not preserve whitespace and line-breaks.

To fix this, you can put the code element inside a pre element:


x = 5;
y = 6;
z = x + y;

The var Element

The HTML <var> element is used to define a variable in programming or in a mathematical expression. The content inside is typically displayed in italic.

The area of a triangle is: 1/2 x b x h, where b is the base, and h is the vertical height.