CSS 3 Cheat Sheet & Quick Reference
CSS 3 Cheat Sheet & Quick Reference
This is a quick reference cheat sheet for CSS goodness, listing selector syntax, properties, units and other useful bits of information.
# Getting Started
Introduction
CSS is rich in capabilities and is more than simply laying out pages.
External stylesheet
Internal stylesheet
<style>
body {
background-color: linen;
}
</style>
Inline styles
<div class="classname"></div>
<div class="class1 ... classn"></div>
!important
.post-title {
color: blue !important;
}
Selector
h1 { }
#job-title { }
div.hero { }
div > p { }
See: Selectors
Text color
color: #2a2aff;
color: green;
color: rgb(34, 12, 64, 0.6);
color: hsla(30 100% 50% / 0.6);
See: Colors
Background
background-color: blue;
background-image: url("nyan-cat.gif");
background-image: url("../image.png");
See: Backgrounds
Font
.page-title {
font-weight: bold;
font-size: 30px;
font-family: "Courier New";
}
See: Fonts
Position
.box {
position: relative;
top: 20px;
left: 20px;
}
Animation
See: Animation
Comment
/* This is a
multi-line comment */
Flex layout
div {
display: flex;
justify-content: center;
}
div {
display: flex;
justify-content: flex-start;
}
Grid layout
#container {
display: grid;
grid: repeat(2, 60px) / auto-flow 80px;
}
#container > div {
background-color: #8ca0ff;
width: 50px;
height: 50px;
}
See: Grid Layout
counter-set: subsection;
counter-increment: subsection;
counter-reset: subsection 0;
:root {
--bg-color: brown;
}
element {
background-color: var(--bg-color);
}
# CSS Selectors
Examples
Groups Selector
h1, h2 {
color: red;
}
Chaining Selector
h3.section-heading {
color: blue;
}
Attribute Selector
div[attribute="SomeValue"] {
background-color: red;
}
p:first-child {
font-weight: bold;
}
No Children Selector
.box:empty {
background: lime;
Basic
* All elements
Combinators
Attribute selectors
S l A ib l
User action pseudo classes
Pseudo classes
# CSS Fonts
Properties
font-family: <font>
font-size: <size>
letter-spacing: <size>
line-height: <number>
Shorthand
Example
Case
/* Hello */
text-transform: capitalize;
/* HELLO */
text-transform: uppercase;
/* hello */
text-transform: lowercase;
@font-face
@font-face {
font-family: 'Glegoo';
src: url('../Glegoo.woff');
}
# CSS Colors
Named color
color: red;
color: orange;
color: tan;
color: rebeccapurple;
Hexadecimal color
color: #090;
color: #009900;
color: #090a;
color: #009900aa;
rgb() Colors
HSL Colors
Other
color: inherit;
color: initial;
color: unset;
color: transparent;
background: (Shorthand)
background-image: url(...)
background-position: left/center/right
top/center/bottom
background-size: cover X Y
border-box
background-clip: padding-box
content-box
no-repeat
background-repeat: repeat-x
repeat-y
background-attachment: scroll/fixed/local
Shorthand
background: rgb(2,0,36);
background: linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(13,232,230,1) 35%, rgba(0,212,255,1) 100%);
.column {
max-width: 200px;
width: 500px;
}
Margin / Padding
.block-one {
margin: 20px;
padding: 10px;
}
Box-sizing
.container {
box-sizing: border-box;
}
Visibility
.invisible-elements {
visibility: hidden;
}
Auto keyword
div {
margin: auto;
}
Overflow
.small-block {
overflow: scroll;
}
# CSS Animation
Shorthand
Properties
animation: (shorthand)
animation-name: <name>
animation-duration: <time>ms
animation-delay: <time>ms
Example
Javascript Event
.container {
display: flex;
}
Container
.container {
display: flex;
display: inline-flex;
Child
/* This: */
flex: 1 0 auto;
/* Is equivalent to this: */
flex-grow: 1;
flex-shrink: 0;
flex-basis: auto;
order: 1;
.container {
display: flex;
}
.container {
display: flex;
/* vertical */
align-items: center;
/* horizontal */
justify-content: center;
}
Reordering
.container > .top {
order: 1;
}
Mobile layout
.container {
display: flex;
flex-direction: column;
}
Table-like
.container {
display: flex;
}
/* the 'px' values here are just suggested percentages */
.container > .checkbox { flex: 1 0 20px; }
.container > .subject { flex: 1 0 400px; }
.container > .date { flex: 1 0 120px; }
This creates columns that have different widths, but size accordingly according to the circumstances.
Vertical
.container {
align-items: center;
}
#grid-container {
display: grid;
width: 100px;
grid-template-columns: 20px 20% 60%;
fr Relative Unit
.grid {
display: grid;
width: 100px;
grid-template-columns: 1fr 60px 1fr;
}
Grid Gap
#grid-container {
display: block;
}
CSS grid-row
CSS syntax:
Example
.item {
grid-row: 1 / span 2;
}
CSS Inline Level Grid
#grid-container {
display: inline-grid;
}
minmax() Function
.grid {
display: grid;
grid-template-columns: 100px minmax(100px, 500px) 100px;
}
CSS syntax:
grid-row-start: auto|row-line;
grid-row-end: auto|row-line|span n;
Example
grid-row-start: 2;
grid-row-end: span 2;
CSS grid-row-gap
grid-row-gap: length;
CSS grid-area
.item1 {
grid-area: 2 / 1 / span 2 / span 3;
}
Justify Items
#container {
display: grid;
justify-items: center;
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-gap: 10px;
}
CSS grid-template-areas
.item {
grid-area: nav;
}
.grid-container {
display: grid;
grid-template-areas:
'nav nav . .'
'nav nav . .';
}
Justify Self
#grid-container {
display: grid;
justify-items: start;
}
.grid-items {
justify-self: end;
}
The grid items are positioned to the right (end) of the row.
Align Items
#container {
display: grid;
align-items: start;
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-gap: 10px;
}
Variable Usage
#firstParagraph {
background-color: var(--first-color);
color: var(--second-color);
}
Counter
/* Set "my-counter" to 0 */
counter-set: my-counter;
/* Increment "my-counter" by 1 */
counter-increment: my-counter;
/* Decrement "my-counter" by 1 */
counter-increment: my-counter -1;
/* Reset "my-counter" to 0 */
counter-reset: my-counter;
Using counters
body { counter-reset: section; }
h3::before {
counter-increment: section;
content: "Section." counter(section);
}
ol {
counter-reset: section;
list-marker-type: none;
}
li::before {
counter-increment: section;
content: counters(section, ".") " ";
}
# Css 3 tricks
Scrollbar smooth
html {
scroll-behavior: smooth;
}