CSS
CSS
Cascading
HTML
Style Sheet
Browser
◼ Consistency (Uniformity)
All pages in the site look the same
◼ Accessibility
PC browsers, mobiles, PDAs, printers, TVs, users with disabilities,
etc…
No browser specific requirements, such as plug-ins
◼ Support Skinning
Why CSS?
The old way:
<font size=“14px”>
My First Header
</font>
<font size=“12px” color=“red” face=“Verdana”>
My information 1 goes here.
</font>
<font size=“14px”>
My Second Header
</font>
<font size=“12px” color=“red” face=“Verdana”>
Different information goes here.
</font>
Why CSS?
Form or Style
.header { font-size:14px;}
.info { font-family: verdana;
font-color: blue;
font-size: 12px; }
CSS Skinning
◼ “Skinning” - changing the look of a page or your site
Selecting an appearance by choosing which stylesheet to use.
◼ Must use style sheet syntax for the attribute value of STYLE
◼ Example:
<h1 style="color:gold; font-family:sans-serif"> Applying
an inline style</h1>
◼ Advantage:
Useful if you only want a small amount of markup
◼ Disadvantages:
Mixes display information into HTML
Clutters up HTML code
Can’t use full range of CSS features since contextual selectors, for example,
like li b{color:green} may not be specifiable inline.
HTML vs. CSS Attribute Syntax
◼ Handling multiple attributes
HTML: Use one or more spaces or lines to separate
attributes in the same tag
CSS: Separate attributes with a single semicolon
(spaces and extra lines optional)
</style>
Allows one style to be
</head> applied simultaneously
to many tags
External Style Sheets
◼ This is a file of pure CSS commands
◼ Link syntax:
<link href=“url” rel=“relation_type”
type=“link_type”> … </link>
◼ Link attributes
href: location of the external file
rel: must be “stylesheet” to tell HTML the link is
for a stylesheet
type: usually “text/css”, the MIME type needed to
download the file
<link> example
<head>
</head>
Resolving Style Preferences
◼ Inline style
4. Inline Style
@import
◼ Can be used in the <style> tag
or
◼ Can be used in a .css file by itself as a CSS
command
h1 {color:brown; font-family:sans-serif}
Site.css … other styles ...
inherit
Subsite.css
(Inherits styles @import url(Site.css)
and overrides h1 {color:green; font-family:Monotype}
… other styles ...
some styles)
inherit
<style>
MyHTML.htm @import url(Subsite.css)
(Inherits Subsite.css h1 {color:red; font-family:cursive}
styles and overrides </style>
some styles)
Style Sheet Inheritance
◼ Tags embedded in other tags inherit style
attributes
◼ Examples:
<p> inherits from <body> because it can only
appear in the <body> section
<li> inherits from <ol> because <li> appears
inside the <ol> tag
Style Sheet Inheritance Example
<body style=“color:red”>
<p>
This paragraph will appear with red text
because it inherits properties of the
body tag that encloses it.
</p>
<p style=“color:green”>
This paragraph will appear with green
text because it explicitly overrides the
red text inherited from the body tag.
</p>
</body>
Font Settings
◼ When text is displayed in a browser it appears in a
default font face, size, style, and color.
◼ EM Unit
Equal to width of capital letter “M” in the default font
◼ EX Unit
Equal to the height of a lower case “x” in the default
font
Why use relative units?
◼ Allows for scalable fonts
◼ Style Types:
normal
italic
oblique (similar to italic)
◼ Example:
p {font-style: italic}
4. font-weight Property
◼ Specified the degree of “boldness” of the type
◼ Example:
p {font-weight: 300}
5. font-variant Property
◼ Not supported by Netscape 4.x
◼ Attribute values:
normal
small-caps (EXAMPLE)
➢ Uppercases but reduces font size
➢ Specifying normal returns the text to standard display.
6. font Property
◼ Pools together a variety of text and font
attributes
◼ Example:
h2 {font: italic small-caps bold}
instead of
h2 {font-style:italic; font-variant:small-caps;
font-weight:bold}
Text Properties
◼ Font settings can be paired with other style sheet
properties to apply additional formatting to strings of
text.
◼ Syntax:
word-spacing: size
letter-spacing: size
line-height: size
◼ p {letter-spacing: 1 em}
Might render: L e t t e r
◼ p {word-spacing: 2 em}
Might render: This is an example
◼ p {line-height: 2}
Indicates line height is twice the font size height
Default is 1.2
text-align
◼ Specifies horizontal alignment to use
Essentially the same as the align attribute of various
HTML tags
◼ Syntax:
text-align:left|center|right|justify
◼ Example:
h2 {text-align: center}
vertical-align
◼ Specifies vertical alignment to use
◼ Syntax:
vertical-align:
➢ baseline|bottom|middle|sub|super|
➢ text-bottom|text-top|top
◼ Example:
h2 {vertical-align: text-bottom}
vertical-align attribute values
◼ baseline: aligns with bottom of font
◼ sub: Subscript
◼ super: Superscript
◼ Example:
p {text-indent: 2 em}
text-decoration
◼ Attribute values:
none
underline (Example)
overline (Example)
line-through (Example)
text-transform
◼ Attribute values:
none
capitalize (first letter of each word is capitalized)
uppercase
Lowercase
% of maximum
intensity, 50%
Red Green Blue of 256 is 128
References
◼ W3C Cascading Style Sheets home page
http://www.w3.org/Style/CSS/
◼ CSS2 Specification
http://www.w3.org/TR/CSS2/
◼ CSS Validators
http://jigsaw.w3.org/css-validator/
https://jigsaw.w3.org/css-validator/DOWNLOAD.html