Skip to main content

CSS3

- Describes how HTML elements are to be displayed on screen, paper, or in other media
- Control the layout of multiple web pages all at once
- External stylesheets are stored in CSS files
-> CSS removed the style formatting from the HTML page!

Selectors

CSS Selectors

Used to "find" (or select) the HTML elements you want to style.

/* Apply to all */
* {
color: blue;
}

/* Group selectors*/
h1,
h2,
p,
.class-text-center,
strong.contain-class-center {
text-align: center;
}

#id-need-bold,
div#id-must-be-bold {
font-weight: bold;
}

Combination

/* Child */
.root > .specified-child { }

/* Adjacent Sibling */
.root + .first-placed-immediately-below { }

/* General Sibling */
.root ~ .all-placed-immediately-below { }

Pseudo

Structure
  • Pseudo Classes selector:pseudo-class
  • Pseudo Elements selector::pseudo-element
  • See more

Attribute

UsingDescriptionExample
selector[attribute]Has attributea[target]
selector[attribute="value"]Has attribute with exact valuea[target="_blank"]
selector[attribute!="value"]Has attribute with exact value or like "value-"div[class="date"]
selector[attribute~="value"]Has attribute containing exact word "value"img[title~='animal']
selector[attribute^="value"]Has attribute start with valuea[href^='https']
selector[attribute$="value"]Has attribute end with valuea[href$=".pdf"]
selector[attribute*="value"]Has attribute includes word: valueimg[title*="dog"]

Specificity

*                     -> 0
element -> 1
.class -> 10
#id -> 100
<inline style="" /> -> 1000
!important -> Infinity
note
  • Combination will sum all points.
    div#first-name.text-center { }
    /* 1 + 100 + 10 = 111 */
  • Selector with same point will apply latest rule

Colours

TypeExample
Namered, black, white, blue, ...
RGBrgb(0, 0, 0)
RGBArgba(0, 0, 0, 0.2)
HEX#000000
HSLhsl(0, 100%, 100%)
HSLAhsl(0, 100%, 100%, 0.2)
HSL colors
  • hue [0, 360]
    • 0 - red
    • 120 - green
    • 240 - blue
  • saturation
    • 0% - shade of gray
    • 100% - full color
  • lightness
    • 0% - dark
    • 50% - neither light or dark
    • 100% - light
Special colors
  • transparent: like opacity
  • currentcolor: get initial color of element, use with pseudo

Backgrounds

Shorthand
background[-position]: color image repeat position;
/* Small pieces */
background-color: #000000;
background-image: url("image_file.type");
background-repeat: repeat | repeat-x | repeat-y | no-repeat;
background-position: left | right | top | bottom;
background-attachment: fixed | scroll;

/* Advance */
background-clip
background-origin

Units

Absolute

-- POPULAR --
px -> pixels = 1/96 in

pt -> points = 1/72 in
pc -> picas = 12 pt
cm -> centimeters
mm -> milimeters
in -> inches

Relative

-- POPULAR --
em - current font size
rem - root font size
% - base on parent element
vh - n -> n% height of viewport
vmin - n -> n% viewport's smaller dimension
vmax - n -> n% viewport's larger dimension

vw - n -> n% width of viewport
ex - base on x-height (rarely)
ch - base on width of "0"

Borders

Shorthand
border[-position]: width style(required) color;
border[-position]-width: 2px;
/* dotted | dashed | solid | double | groove | ridge | inset | outset | hidden; */
border[-position]-style: solid;
border[-position]-color: #000000;

/* OPTIONAL */
border-position: top | right | bottom | left;
/* 0 (square) -> 50% (round) */
border-radius: 0;
border-radius[-top|-bottom][-left|-right]: 0;
border-image: url("some_img.jpg");

Margins & Padding

  • Margin: space around element
  • Padding: space inside element
/* [type][-position]: width; */
/* Example for margin on top */
margin-top: 20px;

Size

  • min-height - height - max-height
  • min-width - width - max-width

Box Model

Box model

Elements

Text

AttributeValueNote
background-colorColour
colorColour
text-alignleft, center, justify, right"justify" - each line is stretched so that every line has equal width
text-align-lastleft, center, justify, rightApply only last row of text (long text)
directionltr (default), rtl
unicode-bidibidi-overrideUsed together with direction
vertical-alignbase-line (default), text-top, text-bottom, sub, super
text-decorationExample: underline #333 solid 5pxShorthand: -line -color -style -thickness (All are optional)
text-transformuppercase, lowercase, capitalize
text-indentExample: 50pxThe indentation of the first line of a text
letter-spacingExample: 2pxThe space between the characters in a text
word-spacingExample: 10pxThe space between words in a text
line-heightExample: 0.8The space between lines. NOTE: should use between 0.8 -> 2
white-spacenowrap, wrap, pre, pre-line, pre-wrapSpecifies how white-space inside an element is handled
text-shadowExample: 2px 2px 5px blue, 2px -2px 5px red;Order: horizontal-shadow vertical-shadow blur(optional) color.
FEATURE: use "," and add more shadow;

Fonts

Safe font for HTML & CSS
  • Arial (sans-serif)
  • Verdana (sans-serif)
  • Tahoma (sans-serif)
  • Trebuchet MS (sans-serif)
  • Times New Roman (serif)
  • Georgia (serif)
  • Garamond (serif)
  • Courier New (monospace)
  • Brush Script MT (cursive)
Using Google font
<head>
<!-- Other tags -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=FontName" />
</head>
AttributeValueNote
fontExample: 12px Arial, sans-serif-style -variant -weight -size(required)/line-height -family(required)
font-familyExample: Arial, Helvetica, sans-serif;Main font, Second font, fallback font (recommend)
font-stylenormal, italic / oblique
font-weightnormal, bold, (100 -> 900)
font-variantnormal, small-caps
font-sizeExample: 0.8remRecommend: use em, rem instead of px
/* unvisited link */
a:link {
color: violet;
}

/* visited link */
a:visited {
color: green;
}

/* mouse over link */
a:hover {
cursor: pointer; /* default */
}

/* selected link */
a:active {
color: darkgreen !important;
}

List

AttributeValueNote
list-styleExample: square inside url("sqpurple.gif");-type -position -image
list-style-typenone,
(ul): circle, disc, square;
(ol): decimal, decimal-leading-zero, [lower, upper]-[alpha, greek, latin, roman]
list-style-positionoutside, inside (indent first line)
list-style-imageurl("image.type")Change mark icon

Table

Recommend setup
table {
width: 100%; /* full width */
border-collapse: collapse;
}
AttributeValueNote
caption-sidetop, bottom
border-collapsecollapse, separate
border-spacing(horizontal vertical) Example: 10px 20pxuse in case border-collapse: separate;
empty-cellshow (default), hideuse in case border-collapse: separate;
table-layoutauto (default), fixedTip: fixed make table render faster (same size column)

Display

Toggle element: visibility: visible|hidden;

display valueDescription
noneRemove element
inline
block
flexa block-level flex container
grida block-level grid container
inline-blockdisplay inline but it can set height & width
inline-flexa inline-level flex container
inline-grida inline-level grid container
contentsonly show contents

Position

position valueDescription
staticdefault
fixedfixed on screen
relativebase on current position
absolutebase on nearest element has position: relative;
stickyrelative (normal) + fixed (scroll to sticks in place)
note

POSITION not static:

  • Combine with left, top, right, bottom (not includes 4)
  • Can use z-index to specifies the stack order

Overflow

overflow[-x/-y] valuedescription
visibledefault
hiddenhide content
scrollshow scroll if content out of box
auto
Handle overflow words
overflow-break: normal | break-word;

Layout: float and clear

/* specifies how an element should float. */
float: none | left | right | inherit;

/* specifies what elements can float beside the cleared element and on which side. */
clear: none | left | right | both | inherit;

Math function

MethodNoteExample
calc()Good for set flexible sizewidth: calc(100% - 10px);
min()Use smaller valueheight: min(50%, 200px);
max()Use larger valueheight: max(400px, 60%);

Counter

body {
counter-reset: section;
}

h1 {
counter-reset: subsection;
}

h1::before {
counter-increment: section;
content: "Section " counter(section) ". ";
}

h2::before {
counter-increment: subsection;
content: counter(section) "." counter(subsection) " ";
}

@font-face: use your font

@font-face {
font-family: myFirstFont;
src: url(sansation_light.woff);
/* font-weight: bold; */
/* font-style: italic; */
}

div {
font-family: myFirstFont;
}

Topics

Properties

  • box-shadow
  • transforms + Animation
  • Image properties
  • Multiple Columns
  • resize
  • var(--variable)
  • box-sizing
  • Media Query

UI Design

  • Flex system
  • Grid system
  • Responsive Web Design

Resources

CSS Preprocessors

Feature/AspectSASSLess.jsStylus
SyntaxSCSS (CSS-like), Indented SyntaxCSS-likeFlexible, allows omission of braces, colons, and semicolons
VariablesYesYesYes
NestingYesYesYes
MixinsYesYesYes
FunctionsYesYesYes
OperationsYesYesYes
InheritanceYesNoNo
InterpolationYesYesYes
Conditionals/LoopsYesYesYes
ImportsYesYesYes
ProsLarge community, extensive documentation, widely used, supports both SCSS and SASS syntaxEasy to learn, lightweight, fast, good for smaller projectsFlexible and concise syntax, powerful and expressive, good for programmatic approach
ConsOverkill for smaller projects, steeper learning curveSmaller community, fewer advanced featuresSmaller community, less documentation, less intuitive for traditional CSS users
Best ForLarge projects and teamsSmaller projects, quick and easy-to-learnDevelopers preferring a more programmatic approach, concise syntax