如何在HTML中显示特定快捷键?HTML5下替代<u>标签的规范方案
Great question! You're right that the old <u> tag isn't recommended in HTML5 for this use case (since it's been repurposed for non-text annotations like spelling errors), and while a custom <span> with CSS works, there are more semantic and convenient options:
Use the
<kbd>tag (most common & semantic choice):
Originally designed to represent keyboard input,<kbd>is widely adopted in practice to highlight accesskey shortcuts. Browsers apply default styling (usually a subtle border or gray background) that makes the key stand out without needing custom CSS. Here's how you'd use it:<label accesskey="n"><kbd>n</kbd>ame</label>Reconsider the
<u>tag (with caution):
HTML5 didn't fully remove<u>—it just redefined its semantic meaning (for things like misspelled words or Chinese proper noun markers). If you only care about the visual underline and don't mind stretching its intended use, you could still use it. But<kbd>is better because it carries the right "keyboard input" semantic that matches accesskey's purpose.
One quick note: Some browsers automatically underline accesskey characters based on user settings, but manually marking them ensures consistency across all environments.
内容的提问来源于stack exchange,提问作者Little Santi

