CSS Lecture 6 - 7
CSS Lecture 6 - 7
Sheets
(CSS)
Lecture 6 -7 -8
Table of Contents
⬥What is CSS?
⬥ Styling with Cascading Stylesheets (CSS)
⬥Selectors and style definitions
⬥Linking HTML and CSS
⬥Fonts, Backgrounds, Borders
⬥The Box Model
⬥Alignment, Z-Index, Margin, Padding
⬥Positioning and Floating Elements
⬥Visibility, Display, Overflow
⬥CSS Development Tools
2
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.
3
CSS Introduction (2)
⬥CSS can be applied to any XML document
⬩ Not just to HTML / XHTML
⬥CSS can specify different styles for different
media
⬩ On-screen
⬩ In print
⬩ Handheld, projection, etc.
⬩ … even by voice or Braille-based reader
4
Why “Cascading”? (3)
⬥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
5
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
6
Using <DIV> and
<SPAN>
Block and Inline Elements
Definition and Usage
The <div> tag defines a division or a section in an
HTML document.
The <div> tag is used as a container for HTML
elements - which is then styled with CSS or
manipulated with JavaScript.
The <div> tag is easily styled by using the class or id
attribute.
Any sort of content can be put inside the <div> tag!
Note: By default, browsers always place a line break
before and after the <div> element
DIV Element
<!DOCTYPE html>
<html>
<style>
div {
background-color: #FFF4A3;
}
</style>
<body>
<div>
<h2>London</h2>
<p>London is the capital city of England.</p>
<p>London has over 13 million inhabitants.</p>
</div>
</body>
</html>
The <span>
⬥Inline style element
Tag
⬥ Useful for modifying a specific portion of text
⬩ Don't create a separate area
(paragraph) in the document
⬥Very useful with CSS
span.html
<p>This one is <span style="color:red; font-
weight:bold">only a test</span>.</p>
<p>This one is another <span style="font-
size:32px;
font-weight:bold">TEST</span>.</p> 10
Style Sheets Syntax
⬥ Stylesheets consist of rules, selectors,
declarations, properties and values
http://css.maxdesign.com.au/
1. Simple selector
2. Combinator selector
3. Attribute selector
4. Pseudo class selector
5. Pseudo element Selector
NO 1 Simple Selector (5 types)
1. Element selector
2. Id selector
3. Class selector
4. Universal Selector
5. Grouping Selector
Element Selector The element selector selects all elements
With the specified element name.
<html>
<head>
<style>
p {
background-color: yellow;
}
</style>
</head>
<body>
<div>
<p id="firstname">My name is Donald.</p>
<p id="hometown">I live in Duckburg.</p>
</div>
</body>
</html>
Id Selector
The id of an element is unique within a page, so the id selector is used to
select one unique element! To select an element with a specific id, write a
hash (#) character, followed by the id of the element
<!DOCTYPE html>
<html>
<head>
<style>
#firstname {
background-color: yellow;
}
</style>
</head>
<body>
<div class="intro">
<p id="firstname">My name is Donald.</p>
<p id="hometown">I live in Duckburg.</p>
</div>
</body>
</html>
Class Selector
CLass selector is formatted as a period
(.) character followed by the name of the class.
It selects all elements with that class
attribute so that unique CSS declarations can be
applied to those specific elements without
affecting other elements on the page.
Class selector
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
</body>
</html>
Grouping SelectorThe grouping selector selects all the HTML
elements with the same style definitions. It will be better
to group the selectors, to minimize the code.
<html>
<head>
<style>
h1, h2, p {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<h2>Smaller heading!</h2>
<p>This is a paragraph.</p>
</body>
</html>
Universal Selector
<h1>Hello world!</h1>
</body>
</html>
NO2 Combinator selectorA combinator is something
that explains the relationship between the
selectors
4 types
1. Descendent selector (space)
2. Child selector >
3. Adjacent Sibling selector +
4. General sibling selector (~)
Descendent selector
<h2>Descendant Selector</h2>
<p>The descendant selector matches all elements that are descendants of a specified
element.</p>
<div>
<p>Paragraph 1 in the div.</p>
<p>Paragraph 2 in the div.</p>
<section><p>Paragraph 3 in the div.</p></section>
</div>
</body>
</html>
Child Selector (>)
. The child selector selects all elements that are the
children of a specified element. The following example
selects all <p> elements that are children of a
<div> ..
div > p {background-color: yellow;}
<div>
<p>Paragraph 1 in the div.</p>
<p>Paragraph 2 in the div.</p>
<span>
<p>Paragraph 3 in the div</p>
</span>
</div>
<p>Paragraph 4. Not in a div.</p>
<p>Paragraph 5. Not in a div.</p>
25
Adjacent Sibling Selector (+)
26
General Sibling Selector (~)
27
No 3 Attribute selector
The [attribute] selector is used to select elements with a
specified attribute
<!DOCTYPE html>
<html>
<head>
<style>
[class|="top"] {
background: yellow;
}
</style>
</head>
<body>
<h1 class="top-header">Welcome</h1>
<p class="top-text">Hello world!</p>
</body>
</html>
The ::first-letter CSS pseudo-element
applies styles to the first letter of the
first line of a block container, but only
when not preceded by other content (such as
images or inline tables).
4 First letter Pseudo element
<!DOCTYPE html>
<html>
<head>
<style>
p::first-letter {
color: #ff0000;
font-size: xx-large;
}
</style>
</head>
<body>
</body>
</html>
NO 4 Pseudo class Selector
Lab Task
Hints
/* visited link */
a:visited {
color: green;
}
/* selected link */
a:active {
color: blue;
}
</style>
</head>
<body>
</body>
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%
⬩ Percentage of what?...
⬩ Zero can be used with no unit: border: 0; 33
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; }
34
Linking HTML and CSS
35
Linking HTML and CSS (2)
⬥ Using external files is highly recommended
⬩ Simplifies the HTML document
⬩ Improves page load speed as the CSS file is
cached
36
Inline Styles: Example
inline-styles.html
<html>
<head>
<title>Inline Styles</title>
</head>
<body>
<p>Here is some text</p>
<!--Separate multiple styles with a semicolon-->
<p style="font-size: 20pt">Here is some
more text</p>
<p style="font-size: 20pt;color:
#0000FF" >Even more text</p>
</body>
</html>
37
Inline Styles: Example
inline-styles.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "htt
p://www.w3.org/TR/xhtml1/ DTD/xhtml1-
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Inline Styles</title>
</head>
<body>
<p>Here is some text</p>
<!--Separate multiple styles with a semicolon--
>
<p style="font-size: 20pt">Here is some
more text</p>
<p style="font-size: 20pt;color:
#0000FF" >Even more text</p>
</body>
</html> 38
CSS Cascade (Precedence)
⬥There arebrowser, user and author stylesheets
with "normal" and "important" declarations
⬩ Browser styles (least priority)
⬩ Normal user styles
⬩ Normal author styles (external, in head, inline)
⬩ Important author styles
⬩ Important user styles (max priority)
a { color: red !important ; }
http://www.slideshare.net/maxdesign/css-cascade-1658158
39
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/
40
Embedded Styles
⬥Embedded in the HTML in the <style> tag:
<style type="text/css">
<html>
Embedded Styles: Example
<head>
<style>
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
42
Embedded Styles: Example (2)
…
<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>
43
Embedded Styles: Example (3)
…
<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>
44
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">
⬩ Ancient browsers do
not recognize
@import
46
External Styles: Example
styles.css
/* CSS Document */
a { text-decoration: none }
li em { color: red;
font-weight: bold }
ul { margin-left: 2cm }
ul ul { text-decoration: underline;
margin-left: .5cm }
47
External Styles: Example (2)
external-styles.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Importing style sheets</title>
<link type="text/css" rel="stylesheet"
href="styles.css" />
</head>
<body>
<h1>Shopping list for
<em>Monday</em>:</h1>
<li>Milk</li>
… 48
External Styles: Example (3)
…
<li>Bread
<ul>
<li>White bread</li>
<li>Rye bread</li>
<li>Whole wheat bread</li>
</ul>
</li>
<li>Rice</li>
<li>Potatoes</li>
<li>Pizza <em>with mushrooms</em></li>
</ul>
<a href="http://food.com" title="grocery
store">Go to the Grocery store</a>
</body>
</html>
49
External Styles: Example (4)
…
<li>Bread
<ul>
<li>White bread</li>
<li>Rye bread</li>
<li>Whole wheat bread</li>
</ul>
</li>
<li>Rice</li>
<li>Potatoes</li>
<li>Pizza <em>with mushrooms</em></li>
</ul>
<a href="http://food.com" title="grocery
store">Go to the Grocery store</a>
</body>
</html>
50