A collection of tricks and common things to achieve with CSS.
Centering
- Centering horizontally:
- Inline?
text-align: center
on the parent. - Block?
margin: 0 auto; width: 200px
. - Multiple blocks?
display: flex; justify-content: center
. - Hack:
- Inline?
- Centering vertically:
- Inline (text)?:
- Block?
- Hack:
Images
- Fluid images to prevent images from ever overflowing:
img { max-width: 100%; }
. - To make background images fit neatly into cards:
- To make
<img>
keep its aspect ratio despite its dimensions:
Misc
- You can combine different units in a
calc
expression, for example:height: calc(100vh - 100px)
.