The <kbd> HTML tag is known as a phrase tag, it is mainly used to style text. By default it displays text in a fixed width font which is ideal for showing what is expected to be entered by a user. It works equally as well in table based layouts as well as CSS based ones. As with all phrase based HTML tags it can be used in situations where consistancy between browsers is difficalt to acheive in other ways. When text is put between the opening and closing <kbd> tag it is displayed as a keyboard text.
<kbd>Some keyboard text in here</kbd>
Any text between the opening and closing tag will be displayed as keyboard text.
It is possible to style and add functionality to the <kbd> tag, this is done by adding an attribute to after the p in the opening <kbd> tag
Attributes allowed for the <kbd>tag include :-class, dir, id, lang, style and title.
In most cases class and id are used and its basic usage is shown below
<kbd id=”header”>Some keyboard text</kbd>
With all attributes there is a space after the initial p then the attribute name, then an equal’s sign and the identifier of the attribute in double quotes.
Both id and class attributes refer to CSS styling of the element and is either stored in the <head> of the webpage on in an external file.
With id this usually refers to a single instance on a page where as class usually refers to a repeating element on a page, it is for this reason for the <kbd> tag may use either the id or class as an atribute.
The attribute style allows you to include css style information for a single <kbd> tag.
The dir attribute allows for left reading and right reading text, lang allows you to specify a language for the tag and title allows you to include extra information for a tag.
<kbd>Some keyboard text</kbd> – basic usage example.
<kbd id=”header>Some keyboard text</kbd> – keyboard text which is styled by the id header.
<kbd class=”headerclass”> Some keyboard text </kbd> – keyboard text which is styled by the class headerclass.
<kbd id =”header” class=”headerclass”> Some keyboard text </kbd> – keyboard text which is styled by the class headerclass and the id header.
<kbd style="color:#F00">Some keyboard text</kbd> – keyboard text which has a custom colour of red.
<kbd dir="rtl">Some keyboard text</kbd> – keyboard text where the text reads from right to left.
<kbd lang="uk">Some keyboard text</kbd> – keyboard text where the language has been tagged as UK.
<kbd title="ptitle">Some keyboard text</kbd> – keyboard text where the tag has been titled ptitle.
Touching on just the surface of the <kbd> HTML tag it is easy to see how versatile it is. To take full advantage of it an understanding of CSS is an advantage.