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

<link href="./path/to/stylesheet/style.css" rel="stylesheet" type="text/css">

Internal stylesheet

<style>
body {
    background-color: linen;
}
</style>

Inline styles

<h2 style="text-align: center;">Centered text</h2>

<p style="color: blue; font-size: 18px;">Blue, 18-point text</p>

Add class

Support multiple classes on one element.

!important

Overrides all previous styling rules.

Selector

See: Selectors

Text color

See: Colors

Background

See: Backgrounds

Font

See: Fonts

Position

See also: Position

Animation

See: Animation

Flex layout

See: Flexbox | Flex Tricks

Grid layout

See: Grid Layout

Variable & Counter

See: Dynamic content

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

See also: Type / Class / ID / Universal selectors

Combinators

Selector
Description

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

See also: Adjacent / Sibling / Child selectors

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

See also: Attribute selectors

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

Placeholder attribute

: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

With indeterminate state

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

Property
Description

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

See also: Font

Shorthand

style
weight
size (required)
line-height
family

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

Property
Description

background:

(Shorthand)

background-color:

See: Colors

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

color
image
positionX
positionY
size
repeat
attachment

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

See also: Margin / Padding

Box-sizing

See also: Box-sizing

Visibility

See also: Visibility

Auto keyword

See also: Margin

Overflow

See also: Overflow

CSS Animation

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

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

See also: Animation

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

See also: CSS Variable

Counter

See also: Counter set

Using counters

Css 3 tricks

Scrollbar smooth

Also see

Last updated