Announcing: Keyboard CSS - Show off your keyboard shortcuts with style π¦
Dharmen Shah
Posted on October 31, 2020
Show off your keyboard shortcuts with style π¦.
Keyboard CSS
Keyboard CSS is a library of ready-to-use, cross-browser compatible keyboard like button UI for use in your web projects. Great for showing off your keyboard shortcuts.
According to W3C, the kbd element represents user input (typically keyboard input, although it may also be used to represent other input, such as voice commands).
The main purpose of Keyboard CSS is to enhance look and feel such kbd elements, but it can also be used with button and a elements, for better interactivity (like simulating click).
Basic Usage
You just have to add a single class to kbd, button or a to apply the related styles, i.e. kbc-button
Usage with kbd
When .kbc-button is used with kbd element, font-size and line-height is inherited from parent for better accessibility.
<h3>Press <kbdclass="kbc-button">/</kbd> to search this site.</h3><p>Press <kbdclass="kbc-button">Ctrl</kbd> + <kbdclass="kbc-button">Shift</kbd> + <kbdclass="kbc-button">R</kbd> to re-render this page.</p>
Usage with button and a
When used with button and a elements, it starts supporting interactions.
I have used sass to create this build. Mostly, everything is handled through sass variables, so you can easily override the defaults, thanks to !default flag.
To change the default base font-size of button and a elements, to 20px, you can do like below:
// assuming you have already done: npm i keyboard-css// define variables first$kbc-font-size-base:20/16*1rem;// and then import@import"path/to/node_modules/keyboard-css/dist/scss/main";
or with new @use rule, you can achieve the same using below code:
// assuming you have already done: npm i keyboard-css@use"path/to/node_modules/keyboard-css/dist/scss/main"with($kbc-font-size-base:20/16*1rem);
Add new size
You can also introduce your new size:
// add size in $kbc-btn-size-map$kbc-btn-size-map:("xl":("padding-y":0.75rem,"padding-x":1.25rem,"font-size":1.5rem,"line-height":1.5,"depth":11,"after-border-width":0.125rem,"after-adjust-x":-0.125rem,"after-adjust-y":-5,"after-border-radius":0.5rem,),);// and then import@import"path/to/node_modules/keyboard-css/dist/scss/main";
Keyboard CSS is a library of ready-to-use, cross-browser compatible keyboard like button UI for use in your web projects. Great for showing off your keyboard shortcuts.
According to W3C, the kbd element represents user input (typically keyboard input, although it may also be used to represent other input, such as voiceβ¦
π I would β€οΈ for you to contribute to Keyboard CSS and help make it even better than it is today! Checkout contributing guidelines for more details.