CSS 3
CSS 3
This is a 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
Inline styles
Add class
Support multiple classes on one element.
!important
Overrides all previous styling rules.
Selector
Text color
Background
Font
Position
Animation
Flex layout
Grid layout
Variable & Counter
CSS Selectors
Examples
Groups Selector
Chaining Selector
Attribute Selector
First Child Selector
No Children Selector
Basic
*
All elements
div
All div tags
.classname
All elements with class
#idname
Element with ID
div,p
All divs and paragraphs
#idname *
All elements inside #idname
Combinators
div.classname
Div with certain classname
div#idname
Div with certain ID
div p
Paragraphs inside divs
div > p
All p tags one level deep in div
div + p
P tags immediately after div
div ~ p
P tags preceded by div
Attribute selectors
a[target]
With a target attribute
a[target="_blank"]
Open in new tab
a[href^="/index"]
Starts with /index
[class|="chair"]
Starts with chair
[class*="chair"]
containing chair
[title~="chair"]
Contains the word chair
a[href$=".doc"]
Ends with .doc
[type="button"]
Specified type
User action pseudo classes
a:link
Link in normal state
a:active
Link in clicked state
a:hover
Link with mouse over it
a:visited
Visited link
Pseudo classes
p::after
Add content after p
p::before
Add content before p
p::first-letter
First letter in p
p::first-line
First line in p
::selection
Selected by user
::placeholder
:root
Documents root element
:target
Highlight active anchor
div:empty
Element with no children
p:lang(en)
P with en language attribute
:not(span)
Element that's not a span
Input pseudo classes
input:checked
Checked inputs
input:disabled
Disabled inputs
input:enabled
Enabled inputs
input:focus
Input has focus
input:in-range
Value in range
input:out-of-range
Input value out of range
input:valid
Input with valid value
input:invalid
Input with invalid value
input:optional
No required attribute
input:required
Input with required attribute
input:read-only
With readonly attribute
input:read-write
No readonly attribute
input:indeterminate
Structural pseudo classes
p:first-child
First child
p:last-child
Last child
p:first-of-type
First of some type
p:last-of-type
Last of some type
p:nth-child(2)
Second child of its parent
p:nth-child(3n42)
Nth-child (an + b) formula
p:nth-last-child(2)
Second child from behind
p:nth-of-type(2)
Second p of its parent
p:nth-last-of-type(2)
...from behind
p:only-of-type
Unique of its parent
p:only-child
Only child of its parent
CSS Fonts
Properties
font-family:
<font>
font-size:
<size>
letter-spacing:
<size>
line-height:
<number>
font-weight:
<number> / bold / normal
font-style:
italic / normal
text-decoration:
underline / none
text-align:
left / right center / justify
text-transform:
capitalize / uppercase / lowercase
Shorthand
font:
italic
400
14px
/
1.5
sans-serif
style
weight
size (required)
line-height
family (required)
Example
Case
@font-face
CSS Colors
Named color
Hexadecimal color
rgb() Colors
HSL Colors
Other
CSS Backgrounds
Properties
background:
(Shorthand)
background-color:
background-image:
url(...)
background-position:
left/center/right top/center/bottom
background-size:
cover X Y
background-clip:
border-box padding-box content-box
background-repeat:
no-repeat repeat-x repeat-y
background-attachment:
scroll/fixed/local
Shorthand
background:
#ff0
url(a.jpg)
left
top
/
100px
auto
no-repeat
fixed;
background:
#abc
url(b.png)
center
center
/
cover
repeat-x
local;
color
image
posX
posY
size
repeat
attach..
Examples
CSS The Box Model
Maximums/Minimums
Margin / Padding
Box-sizing
Visibility
Auto keyword
Overflow
CSS Animation
Shorthand
animation:
bounce
300ms
linear
100ms
infinite
alternate-reverse
both
reverse
name
duration
timing-function
delay
count
direction
fill-mode
play-state
Properties
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
Example
Javascript Event
CSS Flexbox
Simple example
Container
.container {
}
Child
.container > div {
}
CSS Flexbox Tricks
Vertical center
Vertical center (2)
Reordering
Mobile layout
A fixed-height top bar and a dynamic-height content area.
Table-like
This creates columns that have different widths, but size accordingly according to the circumstances.
Vertical
Vertically-center all items.
Left and right
CSS Grid Layout
Grid Template Columns
fr Relative Unit
Grid Gap
CSS Block Level Grid
CSS grid-row
CSS syntax:
grid-row: grid-row-start / grid-row-end;
Example
CSS Inline Level Grid
minmax() Function
grid-row-start & grid-row-end
CSS syntax:
grid-row-start: auto|row-line;
grid-row-end: auto|row-line|span n;
Example
CSS grid-row-gap
Any legal length value, like px or %. 0 is the default value
CSS grid-area
Justify Items
CSS grid-template-areas
Justify Self
The grid items are positioned to the right (end) of the row.
Align Items
CSS Dynamic Content
Variable
Define CSS Variable
Variable Usage
Counter
Using counters
Css 3 tricks
Scrollbar smooth
Also see
Last updated