1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-11-15 17:45:31 +01:00
blog/content/posts/2012-07-16-using-kbd-for-fun-and-profit/index.md

104 lines
3.0 KiB
Markdown
Raw Normal View History

2013-11-18 23:54:59 +01:00
---
2018-07-19 02:22:01 +02:00
type: post
2013-11-19 23:21:19 +01:00
2018-07-18 23:04:31 +02:00
title: Using <kbd> for fun and profit
2018-07-19 00:22:06 +02:00
image: ./kremalicious-kbdfun-teaser.png
style: ./post-kbd.css
2013-11-18 23:54:59 +01:00
author: Matthias Kretschmann
2014-07-12 21:22:47 +02:00
featured: true
2013-11-19 23:21:19 +01:00
2013-11-18 23:54:59 +01:00
date: 2012-07-16 14:36:58+00:00
2018-09-13 20:35:08 +02:00
tags:
2018-08-30 22:50:09 +02:00
- design
- css
2017-11-18 11:56:36 +01:00
coinhive: true
2013-11-18 23:54:59 +01:00
---
There's this HTML element meant for marking up keyboard keys named `<kbd>`. Obviously it can be styled with CSS so why not use it to make those elements look a bit more like hardware or the iOS and Android software keys.
The above picture might be blurry depending on the device you're using so here's a live rendered demo:
2013-11-19 23:21:19 +01:00
<kbd>Light</kbd> <kbd class="dark">Dark</kbd> <kbd class="ios">iOS</kbd> <kbd class="android">An</kbd> <kbd class="android dark">dro</kbd> <kbd class="android color">id</kbd>
2013-11-18 23:54:59 +01:00
They are completely styled with CSS3 so they're sharp on all screens no matter how high the dpi. Have a look at the [full demo](http://lab.kremalicious.com/kbdfun/) or grab the project folder with the CSS & LESS files from GitHub. The code is under the MIT license so you're free to use it in any personal or commercial project.
2015-06-22 21:28:31 +02:00
<p class="content-download">
2015-08-18 22:45:30 +02:00
<a class="btn-primary icon-eye" href="http://lab.kremalicious.com/kbdfun/">Demo</a>
<a class="icon-github" href="https://github.com/kremalicious/kbdfun/">Github</a>
2014-07-13 01:08:55 +02:00
</p>
2013-11-18 23:54:59 +01:00
## Usage
### CSS
Just drop in the `kbdftw.css` in your `head`:
2013-11-18 23:54:59 +01:00
2014-08-09 23:37:26 +02:00
```html
<link rel="stylesheet" href="kbdfun.css">
```
2013-11-18 23:54:59 +01:00
If you want to use the Android key style, include roboto.css before:
2018-07-18 23:04:31 +02:00
```html
<link rel="stylesheet" href="roboto.css">
<link rel="stylesheet" href="kbdfun.css">
```
2013-11-18 23:54:59 +01:00
You also need to add all the Roboto font files from assets/fonts to your project.
### LESS
There're some variables in the `kbdfun.less` file you can customize.
2013-11-18 23:54:59 +01:00
For the Android style, there's `roboto.less` as include at the end. But the font files won't load unless you uncomment the `.font-roboto` line in kbdftw.less. This is to make sure, users won't download all the font files if you don't use the Android style.
2013-11-18 23:54:59 +01:00
### Markup
The default styling are light keys with Lucida Grande as font:
2018-07-18 23:04:31 +02:00
```html
<kbd>Q</kbd>
```
2018-09-24 01:09:50 +02:00
2018-07-18 23:04:31 +02:00
becomes <kbd>Q</kbd>
2013-11-18 23:54:59 +01:00
Add a dark class to get the dark keys:
2018-07-18 23:04:31 +02:00
```html
<kbd class="dark">Q</kbd>
```
2018-09-24 01:09:50 +02:00
2018-07-18 23:04:31 +02:00
becomes <kbd class="dark">Q</kbd>
2013-11-18 23:54:59 +01:00
Adding an ios or android class gives a replica of those system keys. Android uses three different colors on the default keyboard.
2018-07-18 23:04:31 +02:00
```html
<kbd class="ios">Q</kbd>
```
2018-09-24 01:09:50 +02:00
2018-07-18 23:04:31 +02:00
becomes <kbd class="ios">Q</kbd>
2013-11-18 23:54:59 +01:00
2018-07-18 23:04:31 +02:00
```html
<kbd class="android">Q</kbd>
```
2018-09-24 01:09:50 +02:00
2018-07-18 23:04:31 +02:00
becomes <kbd class="android">Q</kbd>
2013-11-18 23:54:59 +01:00
2018-07-18 23:04:31 +02:00
```html
<kbd class="android dark">Q</kbd>
```
2018-09-24 01:09:50 +02:00
2018-07-18 23:04:31 +02:00
becomes <kbd class="android dark">Q</kbd>
2013-11-18 23:54:59 +01:00
2018-07-18 23:04:31 +02:00
```html
<kbd class="android color">Q</kbd>
```
2018-09-24 01:09:50 +02:00
2018-07-18 23:04:31 +02:00
becomes <kbd class="android color">Q</kbd>
2013-11-18 23:54:59 +01:00
I've let the default `display: inline` intact so all padding on the `kbd` elements won't affect the line-height of the surrounding text. This leads to problems when you want to use them over multiple lines so just make them `display: inline-block` in this scenario.
2015-06-22 21:28:31 +02:00
Pro Tip: if you want to replicate all Mac keyboards after 2003 you have to get VAG rounded for the font.