Cascading Style Sheet: 16 September 2016 - CSS
Cascading Style Sheet: 16 September 2016 - CSS
(CSS)
2
CSS Intro
Styling with Cascading Stylesheets
Chapter two 3
CSS
● Separate content from presentation!
Content Presentation
(HTML document) (CSS Document)
Title
Bold
Lorem ipsum dolor sit amet, consectetuer
adipiscing elit. Suspendisse at pede ut
purus malesuada dictum. Donec vitae Italic
neque non magna aliquam dictum.
● Vestibulum et odio et ipsum ■ List type
● accumsan accumsan. Morbi at
● arcu vel elit ultricies porta. Proin
tortor purus, luctus non, aliquam nec,
interdum vel, mi. Sed nec quam nec odio
lacinia molestie. Praesent augue tortor,
convallis eget, euismod nonummy, lacinia
ut, risus.
Chapter two 4
What is rendered
Title
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse at
pede ut purus malesuada dictum. Donec vitae neque non magna aliquam
dictum.
■ Vestibulum et odio et ipsum
■ accumsan accumsan. Morbi at
■ arcu vel elit ultricies porta. Proin
Tortor purus, luctus non, aliquam nec, interdum vel, mi. Sed nec quam
nec odio lacinia molestie. Praesent augue tortor, convallis eget, euismod
nonummy, lacinia ut, risus.
Chapter one 5
CSS Introduction
● Cascading Style Sheets (CSS)
○ Used to describe the presentation of documents
○ Define sizes, spacing, fonts, colors, layout, etc.
○ Improve content accessibility
○ Improve flexibility
● Designed to separate presentation from content
● Due to CSS, all HTML presentation tags and attributes are deprecated,
e.g. font, center, etc.
Chapter two 6
CSS Introduction
● CSS can specify different styles for different media
○ On-screen
○ In print
○ Tablets, smartphones, projectors, etc.
○ … even by voice or Braille-based reader
● Responsive design
Chapter two 7
Why “Cascading”?
● Priority scheme determining which style rules apply to element
○ Cascade priorities or specificity (weight) are calculated and
assigned to the rules
○ Child elements in the HTML DOM tree inherit styles from their
parent
■ Can override them
■ Control via !important rule
Chapter two 8
Why “Cascading”?
● Some CSS styles are inherited and some not
○ Text-related and list-related properties are inherited: color,
font-size, font-family, line-height, text-align,
list-style, etc
○ Box-related and positioning styles are not inherited: width,
height, border, margin, padding, position, float, etc
○ <a> elements do not inherit color and text-decoration
Chapter two 9
StyleSheet Syntax
● Stylesheets consist of rules, selectors, declarations, properties and
values
Chapter two 10
Selectors
Chapter Three 11
Selectors
● Three primary kinds of selectors:
○ By tag (type selector):
h1 { font-family: verdana,sans-serif; }
○ By element id:
#element_id { color: #ff0000; }
○ By element class name:
.element_class {border: 1px solid red}
● Selectors can be combined with commas:
h1, .link, #top {font-weight: bold}
● This will match h1 tags, elements with class link and element with id top
Chapter Three 12
Selectors
● Pseudo-classes define state
○ :hover, :visited, :active , :lang
● Pseudo-elements define element "parts" or are used to generate
content
○ :first-line, :before, :after
a:hover { color: red; }
p:first-line { text-transform: uppercase; }
.title:before { content: "»"; }
.title:after { content: "«"; }
Chapter Three 18
Structural Pseudo-classes(3)
● E:only-child
○ An E element, only child of its parent
● E:only-of-type
○ An E element, only sibling of its parent
● E:empty
○ An E element that has no children (including text nodes)
● More detailed descriptions:
http://www.w3.org/TR/css3-selectors/#structural-pseudos
Chapter Three 19
The UI Element States Pseudo-classes
● E:enabled
○ A user interface element E, which is enabled
● E:disabled
○ A user interface element E, which is disabled
● E:checked
○ A user interface element E which is checked (for instance a
radio-button or checkbox)
Chapter Three 20
Other CSS 3 Selectors
● E:target
○ A user interface element E, which is enabled
● E:not(s)
○ An E element that does not match simple selector
● E~F
○ An F element preceded by an E element
Chapter Three 21
CSS Specificity
● CSS specificity is used to determine the precedence of CSS style
declarations with the same origin. Selectors are what matters
○ Simple calculation: #id = 100, .class = 10, :pseudo = 10, [attr] = 10,
tag = 1, * = 0
○ Same number of points? Order matters.
○ See also:
■ http://www.smashingmagazine.com/2007/07/27/css-specificity-
things-you-should-know/
■ https://specificity.keegan.st/
■ http://css.maxdesign.com.au/selectutorial/advanced_conflict.htm
Chapter Three 24
Embedded Styles
● Embedded in the HTML in the <style> tag:
<style>
...
</style>
Chapter Three 25
Embedded Styles: Example embedded-stylesheets.html
<!DOCTYPE html>
<html>
<head>
<title>Style Sheets</title>
<style>
em {background-color: #8000FF; color: white}
h1 {font-family: Arial, sans-serif}
p {font-size: 18pt}
.blue {color: blue}
</style>
<head>
Chapter Three 26
Embedded Styles: Example embedded-stylesheets.html
<body>
<h1 class="blue">A Heading</h1>
<p>Here is some text. Here is some text. Here
is some text. Here is some text. Here is some
text.</p>
<h1>Another Heading</h1>
<p class="blue">Here is some more text.
Here is some more text.</p>
<p class="blue">Here is some <em>more</em>
text. Here is some more text.</p>
</body>
</html>
Chapter Three 27
External CSS Styles
● External linking
○ Separate pages can all use a shared style sheet
○ Only modify a single file to change the styles across your entire Web site
(see http://www.csszengarden.com/)
● link tag (with a rel attribute)
○ Specifies a relationship between current document and another
document
<link rel="stylesheet" type="text/css"
href="styles.css">
CSS Intro 29
Default Browser Styles
● Browsers have default CSS styles
○ Used when there is no CSS information or any other style
information in the document
● Caution: default styles differ in browsers
○ E.g. margins, paddings and font sizes differ most often and usually
developers reset them
* { margin: 0; padding: 0; }
Chapter Three 30
Fonts
Chapter Five 31
Text-related CSS Properties
● color – specifies the color of the text
● font-size – size of font: xx-small, x-small, small, medium,
large, x-large, xx-large, smaller, larger or numeric value
● font-family – comma separated font names
○ Example: verdana, sans-serif, etc.
○ The browser loads the first one that is available
○ There should always be at least one generic font
● font-weight can be normal, bold, bolder, lighter or a
number in range [100 … 900]
Chapter Five 32
CSS Rules for Fonts
Chapter Five 33
Compact Font Property
● font
○ Shorthand rule for setting multiple font properties at the same
time
font: italic normal bold 12px/16px verdana
Chapter Five 34
Font Embeds
● Use @font-face to declare font
● Point to font file on server
● Call font with font-family
● Currently not supported in IE
● Use font embedding instead of images
@font-face {
font-family: Fancy;
src: url('Fancy-Bold.ttf');
}
.my_class {
font-family: Fancy;
font-size: 3.2em;
}
Chapter Five 35
Text Shadow
● Applies shadow to text
● Syntax: text-shadow:
<horizontal-distance>
<vertical-distance>
<blur-radius> <shadow-color>;
● Do not alter the size of a box
Chapter Five 36
Text Overflow
● Specifies what should happen when text overflows the containing element
● Syntax: text-overflow: <value>;
● Possible values:
○ ellipsis - Display ellipses to represent clipped text
Chapter Five 37
Word Wrapping
● Allows long words to be able to be broken and wrap onto the next line
● Syntax: word-wrap: <value>;
● Possible values:
○ normal
○ break-word
Chapter Five 38
The Box Model
Chapter four 41
Margin, Padding Short Rules
● margin: 5px;
○ Sets all four sides to have margin of 5 pixels;
● margin: 10px 20px;
○ top and bottom to 10px, left and right to 20px;
● margin: 5px 3px 8px;
○ top 5px, left/right 3px, bottom 8px
● margin: 1px 3px 5px 7px;
○ top, right, bottom, left (clockwise from top)
● Same for padding
Chapter four 42
Width and Height
● width – defines numerical value for the width of element, e.g.
200px
● height – defines numerical value for the height of element, e.g.
100px
○ By default the height of an element is defined by its content
○ Inline elements do not apply height, unless you change their
display style.
Chapter four 43
CSS 3 box-sizing
● Determine whether you want an element to render its borders
and padding within its specified width, or outside of it.
● Possible values:
○ box-sizing: content-box (default)
box width: 288 pixels + 10 pixels padding and 1 pixel border
on each side = 300 pixels
○ box-sizing: border-box
box width: 300 pixels, including padding and borders
Chapter four 44
Display & Overflow,
Positioning
Chapter Six 45
Visibility
● visibility: determines whether the element is visible
○ hidden: element is not rendered, but still occupies place on the
page (similar to opacity: 0)
○ visible: element is rendered normally
Chapter Six 48
Overflow
● overflow: defines the behavior of element when content needs
more space than you have specified by the size properties or for
other reasons. Values:
○ visible (default) – content spills out of the element
○ auto - show scrollbars if needed
○ scroll – always show scrollbars
○ hidden – any content that cannot fit is clipped
Chapter Seven 55
What is CSS 3?
Chapter Seven 56
What is CSS 3?
● Cascading Style Sheet level 3 is the most recent iteration of
CSS.
● It is divided into several separate documents called
“modules”.
● CSS 3 has not been approved as a specification, but there are
already a lot of properties that are supported in various
browsers.
Chapter Seven 57
Colors
Chapter Eight 58
Opacity
● Sets the opacity level for an element
● Syntax: opacity: <value>;
● Value from 0.0 (fully transparent) to 1.0
● The opacity is supported in all major browsers.
● Note: IE8 and earlier supports an alternative, the filter property: filter:
Alpha(opacity=50).
Chapter Eight 59
Values in the CSS Rules
● Colors are set in RGB format (decimal or hex):
○ Example: #a0a6aa = rgb(160, 166, 170)
○ Predefined color aliases exist: black, blue, etc.
● Numeric values are specified in:
○ Pixels, ems, e.g. 12px , 1.4em
○ Points, inches, centimeters, millimeters
■ E.g. 10pt , 1in, 1cm, 1mm
○ Percentages, e.g. 50%
Chapter Eight 60
RGBA Colors
● Standard RGB colors with an opacity value for the color (alpha
channel)
● Syntax: rgba(<red>, <green>,
<blue>, <alpha>)
● The range for red, green and blue is between integers 0 and
255
● The range for the alpha channel is between 0.0 and 1.0
● Example: rgba(255, 0, 0, 0.5)
Chapter Eight 61
HSL Colors
● Hue is a degree on the color wheel
○ 0 (or 360) is red, 120 is green, 240 is blue
● Saturation is a percentage value
○ 100% is the full color
● Lightness is also a percentage
○ 0% is dark (black)
○ 100% is light (white)
○ 50% is the average
Chapter Eight 62
HSLA Colors
● HSLA allows a fourth value, which sets the Opacity (via the Alpha
channel) of the element.
● As RGBA is to RGB, HSLA is to HSL
● Supported in IE9+, Firefox 3+, Chrome, Safari, and in Opera 10+
● Example:
○ hsla(0, 100%, 50%, 0.5)
○ Result:
Chapter Eight 63
Backgrounds
Chapter Nine 64
Backgrounds
● background-image
○ URL of image to be used as background, e.g.:
background-image: url("back.gif");
● background-color
○ Using color and image and the same time
● background-repeat
○ repeat-x, repeat-y, repeat, no-repeat
● background-attachment
○ fixed / scroll
Chapter Nine 65
Backgrounds
● background-position: specifies vertical and horizontal position of
the background image
○ Vertical position: top, center, bottom
○ Horizontal position: left, center, right
○ Both can be specified in percentage or other numerical values
○ Examples:
background-position: top left;
background-position: -5px 50%;
Chapter Nine 66
Compact Backgrounds Property
Chapter Nine 67
Background-image or <img>?
● Background images allow you to save many image tags from the
HTML
○ Leads to less code
○ More content-oriented approach
● All images that are not part of the page content (and are used
only for "beautification") should be moved to the CSS
Chapter Nine 69
Gradient Backgrounds Example
/* For Safari 5.1+ */
background: -webkit-linear-gradient(blue, yellow);
/* Standard syntax */
background: linear-gradient(blue, yellow);
Chapter Nine 70
Multiple backgrounds
● CSS3 allows multiple background images
● Simple comma-separated list of images
● Comma separated list for the other properties
Chapter Nine 71
Borders
Chapter ten 72
Borders
● border-width: thin, medium, thick or numerical value, e.g.10px
● border-color: color alias or RGB value
● border-style: none, hidden, dotted, dashed, solid, double
● Each property can be defined separately for left, top, bottom and
right
○ border-top-style, border-left-color, …
Chapter ten 73
Border image
● Defines an image to be used instead of the normal border of an
element
● Split up into a couple of properties
● Example:
○ The border-image property has 3 parts:
border-image: url(border-image.png) 25% repeat;
Chapter ten 75
Box shadow
● Allows to easily implement multiple drop shadows (outer or
inner) on box elements
● Specifying values for color, size, blur and offset
● Example:
-moz-box-shadow: 10px 10px 5px #888;
-webkit-box-shadow: 10px 10px 5px #888;
box-shadow: 10px 10px 5px #888;
Chapter ten 76
Animations
Chapter Eleven 77
Animations
● Works in all webkit browsers
● Example:
@keyframes resize {
0% {...}
50% {...}
100% {...}
}
#box {
animation-name: resize;
animation-duration: 1s;
animation-iteration-count: 4;
animation-direction: alternate; animation-timing-function:
ease-in-out;
}
82
CSS Development Tools
● Chrome Dev Tools
– used to examine and
adjust CSS and HTML
83
CSS Development Tools
● IE Developer Toolbar
– to examine CSS and
HTML (press [F12])
84