cheatsheets/css.md

215 lines
8.1 KiB
Markdown
Raw Permalink Normal View History

2017-08-29 21:35:28 +00:00
---
title: CSS
category: CSS
weight: -1
2017-10-15 18:02:23 +00:00
keywords:
- "margin, padding, border"
- "div, .class, #id, [attr]"
- "font, background"
- "display: block, inline, flex"
- Selectors
- Properties
2017-08-29 21:35:28 +00:00
---
## Basics
{: .-three-column}
### Selectors
```css
.class {
font-weight: bold;
}
```
{: .-setup}
2019-10-22 15:24:51 +00:00
| Selector | Description |
| ----------------- | ------------ |
2019-12-24 11:04:39 +00:00
| `*` | All elements |
2019-10-22 15:24:51 +00:00
| `div` | Element |
| `.class` | Class |
| `#id` | ID |
| `[disabled]` | Attribute |
| `[role="dialog"]` | Attribute |
2017-08-29 21:35:28 +00:00
### Combinators
2018-05-02 01:59:07 +00:00
| Selector | Description |
| ------------------- | ----------------- |
| `.parent .child` | Descendant |
| `.parent > .child` | Direct descendant |
| `.child + .sibling` | Adjacent sibling |
| `.child ~ .sibling` | Far sibling |
2019-12-24 11:04:39 +00:00
| `.class1.class2` | Have both classes |
2017-08-29 21:35:28 +00:00
### Attribute selectors
| Selector | Description |
| ------------------ | ----------------------------------- |
| `[role="dialog"]` | `=` Exact |
| `[class~="box"]` | `~=` Has word |
| `[class|="box"]` | `|=` Exact or prefix (eg, `value-`) |
| `[href$=".doc"]` | `$=` Ends in |
| `[href^="/index"]` | `^=` Begins with |
| `[class*="-is-"]` | `*=` Contains |
2017-08-29 21:35:28 +00:00
### Pseudo-classes
2019-10-22 15:24:51 +00:00
| Selector | Description |
| -------------------- | ------------------------------------------ |
| `:target` | eg, `h2#foo:target` |
| --- | --- |
| `:disabled` | |
| `:focus` | |
| `:active` | |
| --- | --- |
| `:nth-child(3)` | 3rd child |
| `:nth-child(3n+2)` | 2nd child in groups of 3 |
| `:nth-child(-n+4)` | |
| --- | --- |
| `:nth-last-child(2)` | |
| `:nth-of-type(2)` | |
| --- | --- |
2019-12-24 11:06:00 +00:00
| `:checked` | Checked inputs |
| `:disabled` | Disabled elements |
| `:default` | Default element in a group |
2019-10-22 15:24:51 +00:00
| --- | --- |
2019-12-24 11:06:00 +00:00
| `:empty` | Elements without children |
2017-08-29 21:35:28 +00:00
### Pseudo-class variations
2018-05-02 01:59:07 +00:00
| Selector |
| ----------------- |
| `:first-of-type` |
| `:last-of-type` |
2017-08-31 09:30:02 +00:00
| `:nth-of-type(2)` |
2018-05-02 01:59:07 +00:00
| `:only-of-type` |
| --- |
| `:first-child` |
| `:last-child` |
| `:nth-child(2)` |
| `:only-child` |
2017-08-29 21:35:28 +00:00
{: .-left-align}
2018-05-02 01:59:07 +00:00
## Fonts
2017-08-29 21:35:28 +00:00
{: .-left-reference}
### Properties
2018-05-02 01:59:07 +00:00
| Property | Description |
| ------------------ | ------------------------------------ |
| `font-family:` | `<font>, <fontN>` |
| `font-size:` | `<size>` |
| `letter-spacing:` | `<size>` |
| `line-height:` | `<number>` |
| --- | --- |
| `font-weight:` | `bold` `normal` |
| `font-style:` | `italic` `normal` |
| `text-decoration:` | `underline` `none` |
| --- | --- |
| `text-align:` | `left` `right` `center` `justify` |
| `text-transform:` | `capitalize` `uppercase` `lowercase` |
2017-08-29 21:35:28 +00:00
{: .-key-values}
### Shorthand
{: .-prime}
2018-05-02 01:59:07 +00:00
| | style | weight | size (required) | | line-height | family |
| ------- | -------- | ------ | --------------- | --- | ----------- | ----------------- |
| `font:` | `italic` | `400` | `14px` | `/` | `1.5` | `sans-serif` |
| | style | weight | size (required) | | line-height | family (required) |
2017-08-29 21:35:28 +00:00
{: .-css-breakdown}
### Example
```css
font-family: Arial;
font-size: 12pt;
line-height: 1.5;
letter-spacing: 0.02em;
color: #aa3322;
```
### Case
```css
text-transform: capitalize; /* Hello */
2018-05-02 01:59:07 +00:00
text-transform: uppercase; /* HELLO */
text-transform: lowercase; /* hello */
2017-08-29 21:35:28 +00:00
```
2018-05-02 01:59:07 +00:00
## Background
2017-08-29 21:35:28 +00:00
{: .-left-reference}
### Properties
| Property | Description |
2018-05-02 01:59:07 +00:00
| ------------------------ | ---------------------------------------- |
2017-08-29 21:35:28 +00:00
| `background:` | _(Shorthand)_ |
| --- | --- |
| `background-color:` | `<color>` |
| `background-image:` | `url(...)` |
| `background-position:` | `left/center/right` `top/center/bottom` |
2017-08-31 18:05:08 +00:00
| `background-size:` | `cover` `X Y` |
2017-08-29 21:35:28 +00:00
| `background-clip:` | `border-box` `padding-box` `content-box` |
| `background-repeat:` | `no-repeat` `repeat-x` `repeat-y` |
| `background-attachment:` | `scroll` `fixed` `local` |
{: .-key-values}
### Shorthand
2018-05-02 01:59:07 +00:00
| | color | image | positionX | positionY | | size | repeat | attachment |
| ------------- | ------ | ------------- | --------- | --------- | --- | -------------- | ----------- | ---------- |
2017-08-29 21:35:28 +00:00
| `background:` | `#ff0` | `url(bg.jpg)` | `left` | `top` | `/` | `100px` `auto` | `no-repeat` | `fixed;` |
2018-05-02 01:59:07 +00:00
| `background:` | `#abc` | `url(bg.png)` | `center` | `center` | `/` | `cover` | `repeat-x` | `local;` |
2017-08-29 21:35:28 +00:00
| | color | image | positionX | positionY | | size | repeat | attachment |
{: .-css-breakdown}
### Multiple backgrounds
```css
2018-05-02 01:59:07 +00:00
background: linear-gradient(to bottom, rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)),
url('background.jpg') center center / cover, #333;
```
## Animation
2017-08-29 21:35:28 +00:00
{: .-left-reference}
### Properties
| Property | Value |
| ---------------------------- | -------------------------------------------------------- |
| `animation:` | _(shorthand)_ |
| `animation-name:` | `<name>` |
| `animation-duration:` | `<time>ms` |
| `animation-timing-function:` | `ease` `linear` `ease-in` `ease-out` `ease-in-out` |
| `animation-delay:` | `<time>ms` |
| `animation-iteration-count:` | `infinite` `<number>` |
| `animation-direction:` | `normal` `reverse` `alternate` `alternate-reverse` |
| `animation-fill-mode:` | `none` `forwards` `backwards` `both` `initial` `inherit` |
| `animation-play-state:` | `normal` `reverse` `alternate` `alternate-reverse` |
2017-08-29 21:35:28 +00:00
{: .-key-values}
### Shorthand
| | name | duration | timing-function | delay | count | direction | fill-mode | play-state |
| ------------ | -------- | -------- | --------------- | ------- | ---------- | ------------------- | --------- | ---------- |
| `animation:` | `bounce` | `300ms` | `linear` | `100ms` | `infinite` | `alternate-reverse` | `both` | `reverse` |
| | name | duration | timing-function | delay | count | direction | fill-mode | play-state |
2017-08-29 21:35:28 +00:00
{: .-css-breakdown}
### Example
```css
animation: bounce 300ms linear 0s infinite normal;
animation: bounce 300ms linear infinite;
animation: bounce 300ms linear infinite alternate-reverse;
animation: bounce 300ms linear 2s infinite alternate-reverse forwards normal;
2017-08-29 21:35:28 +00:00
```
### Event
```js
.one('webkitAnimationEnd oanimationend msAnimationEnd animationend')
```