Lecture 4
Cascading Style Sheets (CSS)
Working with <div> tags
CSS: A New Philosophy
SeparateContent
content from presentation! Presentation
(HTML document) (CSS Document)
Title
Lorem ipsum dolor sit amet,
Bold
consectetuer adipiscing elit.
Suspendisse at pede ut purus Italics
malesuada dictum. Donec vitae
neque non magna aliquam
dictum.
Indent
• 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.
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 Styles
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
4
Style Sheets Syntax
Stylesheets consist of rules, selectors,
declarations, properties and values
Selectors arehttp://css.maxdesign.com.au/
separated by commas
Declarations are separated by semicolons
Properties and values are separated by
colons
h1,h2,h3 { color: green; font-weight: bold; } 5
Selectors
Selectors determine which element the rule applies to:
All elements of specific type (tag)
Those that match a specific attribute (id, class)
Examples:
.header a { color: green }
#menu>li { padding-top: 8px }
6
Types of 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 (only for HTML):
.myClass {border: 1px solid red}
Selectors can be combined with commas:
h1, .link, #top-link {font-weight: bold}
This will match <h1> tags, elements with class
link, and element with id top-link 7
ID Selector Example
Class Selector Example
Class Selector Specifier
Grouping Selectors
Selectors (Pseudo elements/classes)
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: "«"; }
12
Selectors (Relative matching)
Match relative to element placement:
p a {text-decoration: underline}
This will match all <a> tags that are inside of <p>
* – universal selector (avoid or use with care!):
p * {color: black}
This will match all descendants of <p> element
> selector – matches direct child nodes:
p > .error {font-size: 8px}
This will match all elements with class error,
direct children of <p> tag
.class1.class2 (no space) - matches
elements with both (all) classes applied at the
same time 13
Properties
Detailed Properties can be known by Self study from the
following.
https://www.w3schools.com/cssref/
https://www.w3schools.com/css/css_list.asp
You might need them for your projects.
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; 15
Linking HTML and CSS
HTML (content) and CSS (presentation) can be linked in three ways:
Inline: the CSS rules in the style attribute
No selectors are needed
Embedded: in the <head> in a <style> tag
External: CSS rules in separate file (best)
Usually a file with .css extension
Linked via<link rel="stylesheet" href=…> tag or
@import directive in embedded CSS block
16
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
17
Inline Styles: Example
inline-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>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> 18
Inline Styles: Example
inline-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>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> 19
Embedded Styles
Embedded in the HTML in the <style> tag:
<style type="text/css">
The <style> tag is placed in the <head> section of the document
type attribute specifies the MIME type
MIME describes the format of the content
Other MIME types include text/html, image/gif, text/javascript …
Used for document-specific styles
20
Embedded Styles: Example
embedded-
stylesheets.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>Style Sheets</title>
<style type="text/css">
em {background-color:#8000FF; color:white}
h1 {font-family:Arial, sans-serif}
p {font-size:18pt}
.blue {color:blue}
</style>
<head> 21
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>
22
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>
23
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
<link and anothertype="text/css"
rel="stylesheet" document
href="styles.css">
link elements should be in the <head>
24
External CSS Styles (2)
@import
Another way to link external CSS files
Example:
<style type="text/css">
@import url("styles.css");
/* same as */
Ancient browsers do not recognize @import
@import "styles.css";
</style>
Use @import in an external CSS file to workaround the IE 32 CSS file limit
25
External Styles: Example
styles.css
/* CSS Document */
a { text-decoration: none }
a:hover { text-decoration: underline;
color: red;
background-color: #CCFFCC }
li em { color: red;
font-weight: bold }
ul { margin-left: 2cm }
ul ul { text-decoration: underline;
margin-left: .5cm }
26
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>
<ul>
<li>Milk</li>
27
…
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> 28
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> 29
Working with <div> tag and CSS Styles
<div class=“selector”></div>
<div id=“selector”></div>
<div> tag creates an element in HTML with automatic
line break before and after it.
Example of creating stylish button/links.
Example (<div> and CSS)
<style> <body>
#rcorners1 {
border-radius: 15px; border: 2px solid red;
<div id="rcorners1">
background: #73AD21; padding: 5px;
width: 150px; height: 15px;
<a href="test1.html"><center>Home</a>
} </div>
#rcorners2 {
border-radius: 15px; border: 2px solid <div id="rcorners2">
#73AD21;
align-content: center; padding: 5px; <a href="test1.html"><center>About
Us</a>
width: 150px; height: 15px;
} </div>
a{
font-family : Times-New-Roman; font-style: <div id="rcorners1">
bold;
<a href="test1.html"><center>Contact
color : RED;
Us</a>
Example (<div> and CSS)
<style> <body>
#rcorners1 {
border-radius: 15px; border: 2px solid red;
<div id="rcorners1">
background: #73AD21; padding: 5px;
width: 150px; height: 15px;
<a href="test1.html"><center>Home</a>
} </div>
#rcorners2 {
border-radius: 15px; border: 2px solid <div id="rcorners2">
#73AD21;
align-content: center; padding: 5px; <a href="test1.html"><center>About
Us</a>
width: 150px; height: 15px;
} </div>
a{
font-family : Times-New-Roman; font-style: <div id="rcorners1">
bold;
<a href="test1.html"><center>Contact
color : RED;
Us</a>