HTML <kbd> style Attribute

The style attribute on a <kbd> tag assigns a unique style to the keyboard shortcut element. A style contains one or more CSS property/value pairs that define the appearance of the i element.

Example

#

A style attribute on a <kbd> element.

CTRL + C is used to copy content.
CTRL + X is used to cut content.
CTRL + V is used to paste content.

<p>
 <kbd style="padding: 2px 4px;color: white;
           background-color: teal;border-radius: 3px;">
    CTRL + C</kbd> 
    is used to copy content.<br />

 <kbd style="padding: 2px 4px;color: white;
           background-color: teal;border-radius: 3px;">
    CTRL + X</kbd> 
    is used to cut content.<br />

 <kbd style="padding: 2px 4px;color: white;
           background-color: teal;border-radius: 3px;">
    CTRL + V</kbd> 
    is used to paste content.
</p>

Using style

The style attribute specifies the style, i.e. look and feel, of the <kbd> element.

A style contains any number of CSS property/value pairs, separated by semicolons (;).

The style attribute overrides any other style that was defined in a <style> tag or an external CSS file.

This inline styling affects the current <kbd> element only.


Syntax

<kbd style="CSS-styles">

Values

#

Value Description
CSS-styles One or more CSS property/value pairs separated by semicolons (;).

More Examples

A style attribute on a <kbd> element.
Clicking the button toggles the background and text colors.

CTRL + X is used to cut content.


<p>
   <kbd id="mykbd"class="kbd"
         style="padding: 0.2rem 0.4rem; color: #fff; background-color: teal;
                border-radius: 0.2rem;">CTRL + X</kbd> 
   is used to cut content.
</p>

<br/>
<button type="button" onclick="toggle();">Toggle style</button>

<script>
  let toggle = () => {
    let element = document.getElementById("mykbd");

    if (element.style.backgroundColor === "teal") {
       element.style.backgroundColor = "lightblue";
       element.style.color = "black";
    } else {
       element.style.backgroundColor = "teal";
       element.style.color = "white";
    }
  }
</script>

Code explanation

The style attribute assigns a background and text colors to the <kbd> element.

Clicking the button calls JavaScript which changes the color of the background and text.


Browser support

Here is when style support started for each browser:

Chrome
1.0 Sep 2008
Firefox
1.0 Sep 2002
IE/Edge
1.0 Aug 1995
Opera
1.0 Jan 2006
Safari
1.0 Jan 2003

You may also like

 Back to <kbd>
Guides