Css Intro
Css Intro
G. Kagombe
ICS 2203
Outline
• Motivation for CSS
• How to use CSS in your documents
• Formatting text with CSS
–
Font properties
–
Text properties
• Formatting lists with CSS
• Summary
HTML
• HTML (when used correctly) describes the
different parts of a document
–
Paragraphs, section headings, quotes, images, ...
• HTML (when used correctly) does not describe
the formatting of a document
• HTML is a logical markup language not a
physical markup language
• HTML was designed to be rendered on a wide
variety of devices
–
Graphical web browsers, text terminals, screen readers, ...
CSS
• CSS (Cascading Style Sheets) is for the
formatting side of the Web
• CSS describes how rendered HTML documents
should look
• CSS considers the physical and visual
display of the document (the Style)
Advantages of CSS
• The use of CSS separates document layout from
document content
–
Different people can be responsible for the two parts
–
Document author can focus on content
–
Graphic designer can focus on layout
• A single file can control the look of an entire
web site
–
Easy to modify look of web site without affecting its contents
–
Easy to obtain a consistent look (the R in CRAP)
• If done correctly, documents degrade gracefully
on platforms that don't support visual formatting
Disadvantages of CSS
• More to learn
–
CSS is powerful but complex
• Not fully supported on some browsers
–
Even some modern browsers are not fully CSS 2 compliant
• The formatting is separated from the
document
–
Makes it hard to write a document and format it
simultaneously
How to Write a Document
1. Decide what you want to write
–
First and foremost the content of the document is important
–
Decide on the logical structure of the document
2. Write it
–
Write the document content
–
Markup the document's logical structure
3. Format it
–
Use CSS to do formatting
–
Add other formatting-specific data (e.g., navigation)
• Points 1 and 2 should take the majority of
the time
Using CSS
• There are three ways to use CSS
• External Style Sheets
–
Using the LINK tag (in the document HEAD)
<link rel=”stylesheet”
href=”../mystyle.css” type=”text/css”>
• Using the CSS @import directive,
<style>
@import url('styles.css');
</style>
Using CSS (Cont'd)
• Inline Style Sheets
– The STYLE tag (in the document HEAD)
<style type=”text/css”>
/* CSS information goes here */
</style>Inline STYLE Attributes
• The STYLE attribute (within another HTML
tag)
<a style=”font-size: 10pt;”
href=”xsk.html”>
=>Don't use these
–
Gives the disadvantages of CSS without the most important
advantages
Simple CSS
• An Inline CSS example that modifies some
of the common HTML tags
<style type=”text/css”>
h1 { font-size:12pt;
font-family: “Luxi Sans”, sans-serif;
font-weight: bold; }
p {
font-size: 12pt;
font-family: “Luxi Serif”, serif;
}</style>
Using Comments
• You can place a comment within a pair
of /* ... */ tags, like this:
/* This is a CSS comment */
h1 { font-weight: bold;
font-size: 200%;}
h2 { font-weight: bold;
font-size: medium;}
The Font Style: font-style
• Can be one of normal, italic, or oblique
• An oblique font is a skew transformation
of a regular font (not well supported)
em { font-style: italic; }
h1 { font-family: sans-serif;
font-weight: bold;
font-style: italic;}
Other Font Properties
• Use font-variant for producing small caps
• Use font-stretch to expand or condense a
font
h1 { text-align: center; }
p { text-align: left; }
Text Properties: text-decoration
• Add some decoration to the text
• Can be one of none, underline, overline,
line-through, or blink
• Treat blink like a contagious disease
h1 { text-decoration: underline; }
a { text-decoration: none; }
Text Properties: text-indent
• Specifies a length by which the first
line of text should be indented
• Length can be measured in a relative unit
–
em: the font-size of the current font (width of a letter m)
–
ex: the x-height of the current font (height of a letter x)
–
px: one screen pixel
• Or an absolute unit
–
Inches (in) centimeters (cm) millimeters (mm), points (pt), or
picas (pc)
●
1pt = (1/72)in and 1pc=12pt
P { text-indent: 5em; }
Text Properties: text-transform
• Can actually transform the text
• Can be one of
–
none: don't do anything to the text
–
capitalize: Start each word with an uppercase letter
–
uppercase: Make everything uppercase (allcaps)
–
lowercase: Make everything lowercase
ul.menu { padding: 0; }
ul.menu li { text-decoration: none; }
...
<ul class=”menu”>
<li>Menu item1</li>
<li>Menu item2</li>
</ul>
CSS and the A Tag
• CSS has several options for the A tag
–
A:link defines the style for normal unvisited links
–
A:visited defines the style for links that have already been visited in
the past
–
A:active defines the style for links after the user clicks on them
(usually while the next page loads)
–
A:hover defines the style for links when the mouse pointer is
hovering over them
• A:hover is useful for making things that look
like menus
Summary
• CSS provides fine-grained control over
–
fonts
–
text
–
the display of lists
• The CSS lookup mechanism allows us to
apply styles to tags that only appear
within other tags
• In practice, many authors ignore most HTML
tags other than DIV and SPAN