<code>
or
span
.
If you’ve ever put in a keyboard shortcut into text, say for a blog post or some docs, you probably used
<code>
, specially if you’re writing in a markdown file because it’s so easy to simply wrap what you want in backticks.
But, if you want to do it properly, you should use the
<kbd>
element instead, which is the semantic elements for user text inputs, such as shortcuts.
You can use a single
<kbd>
for a single keystroke (like, "press
esc
to exit full screen"), but if you have a shortcut that involves multiple keys, you can nest them as well, something like
<kbd><kbd>cmd</kbd> + <kbd>c</kbd></kbd>
, which you can have output like this: like
command
+
c
.
This can present a styling challenge, if you style them to look more like keys like what I did in this post, but you can use
kbd:not(:has(kbd))
as you're selector, and you're set!
And if you are writing markdown, you can use HTML in there, so it takes just a few extra keystrokes to get the correct semantics into your work.
By itself,
<samp>
is used to show quoted text that's being output from your system. For example, if you're describing what an alert a user might see when they try to leave a page without saving.
Leave site? Changes you made cannot be saved
<kbd>
with
<samp>
There are two situations you might want to combine the two elements:
If those sound very different, it's because they are!
For the first one, we would nest the
<kbd>
inside the
<samp>
, and for second, it's the other way around!
When you submit, you will see the following popup before you can continue:
To confirm, click OK
To save a copy of your file, go to File => Save as .