3. C03 – CSS
• Introduction
• Including CSS in HTML
pages
• CSS syntax
• CSS selectors
• IDs and classes
• Combining selectors
• Pseodo-element
selectors
• Selector priority
• Inheritance
• Styling color
• Styling font
• Specifying dimensions
• Borders, padding and
margins
• Positioning elements
• Adding opacity and
shadows
4. CSS
• Cascading Style Sheets (CSS) is a style sheet
language used for describing the presentation
of a document written in a markup language
like HTML
• CSS is a cornerstone technology of the World
Wide Web, alongside HTML and JavaScript
8. CSS
• The “cascade” part of CSS is a set of rules for
resolving conflicts with multiple CSS rules
applied to the same elements
• For example, if there are two rules defining the
color of an element, the rule that comes last in
the cascade order will “trump” the other
9. STYLING HTML
• HTML tags can be used to style HTML
• However, they shouldn’t be used
• Most formatting attributes have been deprecated in
HTML5
10. STYLING HTML
• Separation of concerns:
• HTML – structure of the webpage
• CSS – style of the webpage
12. INCLUDING CSS IN WEB PAGES
• Inlining styles – style is defined when the element
is defined
13. INCLUDING CSS IN WEB PAGES
• Using the <style></style> tags in the head of
the HTML document
14. INCLUDING CSS IN WEB PAGES
• Using a separate linked stylesheet – most
commonly used
index.html myStyles.css
15. CSS SYNTAX
• Three terms for describing your styles
• CSS rule
• CSS selector
• CSS declaration
16. CSS RULE
• Every style is defined by a selector and a
declaration
• The declaration contains at least one property:
value pair. Together they are called a CSS rule
selector {
property1: value1;
property2: value2;
…
propertyn: valuen;
}
declaration
18. CSS SELECTOR
• The selector is typed in front of the
declaration, with a space separating it and the
opening curly-bracket (aka curly-brace)
• Typically, extra indentation and returns are
added as shown for the sake of readability
19. CSS SELECTOR
• You can apply styles to multiple selectors in
the same rule by separating the selectors with
commas
20. CSS DECLARATION
• You can apply multiple declarations to a
selector by separating the declarations with
semi-colons.
21. CSS SELECTORS
• There are three categories of CSS selectors
• All (*)
• Type (element)
• ID
• Class
23. TYPE (ELEMENT) SELECTORS
• The simplest selector is the type selector,
which targets an HTML element by name
24. ID SELECTORS
• An ID is an HTML attribute that is added to
your HTML markup
• You reference that ID in your CSS with a hash
(#)
• IDs should be unique in your HTML/CSS
HTML CSS
25. CLASS SELECTORS
• A class is an HTML attribute that is added to
your HTML markup
• You reference that class in your CSS with a
period (.)
HTML CSS
26. IDS VS. CLASSES
• The most important difference between IDs
and classes is that there can be only one ID
assigned to an element, but multiple classes
• An ID is more specific than a class
• An element can have both an ID and multiple
classes
50. !IMPORTANT SPECIFIER
• The !important specifier can be used to
override normal cascading and inheritance
• Normal use case: never!
HTML
CSS
51. !IMPORTANT SPECIFIER
• The !important specifier can be used to
override normal cascading and inheritance
• Normal use case: never!
HTML
CSS
52. APPLYING COLOR
• color – text color
• background-color – background color
(duuuh…) HTML CSS
53. APPLYING COLOR
• There are 3 main ways to set color:
• By name: red, white, blue, magenta, etc.
• By hex-RGB values: #006699, #123456, #FFAACC
• By RGB(A) values: rgb(0, 66, 99), rgba(123, 255, 0,
0.5)
• By HSL values: hsl(100, 66%, 99%), hsl(360, 55%, 0%)
57. FONT ATTRIBUTES
• For fonts, we generally want to style the
following:
• color: color
• size: font-size
• effect: font-weight, font-style, text-
decoration
• family: font-family
61. FONT-FAMILY
• For the font family you can (and should)
specify, in order:
• The exact font name
• A fallback font name or font family
• A generic font type (if none of the above are
available)
• You can also import font from other font
repositories (like Google Fonts)
63. SPECIFYING DIMENSIONS
• You can specify dimensions (width and/or
height) to most HTML elements in:
• pixels
• percentages (of current viewport)
• You can also specify a maximum width of
length
66. MEASUREMENT UNITS IN CSS
Unit Description Example
% Defines a measurement as a percentage relative to another
value, typically an enclosing element.
p {
font-size: 16pt;
line-height: 125%;
}
px Defines a measurement in screen pixels. p {
padding: 25px;
}
em A relative measurement for the height of a font in em spaces.
Because an em unit is equivalent to the size of a given font, if
you assign a font to 12pt, each "em" unit would be 12pt; thus,
2em would be 24pt.
p {
letter-spacing: 7em;
}
rem Similar to “em” but while em is relative to the font-size of its
direct or nearest parent, rem is only relative to the html (root)
font-size.
p {
letter-spacing: 7rem;
}
cm/mm
/in
Defines a measurement in centimeters, millimeters or inches p {
word-spacing: 15mm;
}
pt Defines a measurement in points. A point is defined as
1/72nd of an inch.
body {
font-size: 18pt;
}
ex This value defines a measurement relative to a font's x-
height. The x-height is determined by the height of the font's
lowercase letter x.
p {
font-size: 24pt;
line-height: 3ex;
}
80. POSITIONING
HTML CSS
Fixed positioning will place the element relative to the
current viewport.
The element will stay in that position even if the page is