Cascading Style Sheets (CSS) Working With Tags
Cascading Style Sheets (CSS) Working With Tags
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
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
Selectors arehttp://css.maxdesign.com.au/
separated by commas
Declarations are separated by semicolons
Properties and values are separated by
colons
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
16
Linking HTML and CSS (2)
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 …
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/)
<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 }
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>