"Cascading Style Sheets" For Styling WWW Information: Mike Pangburn
"Cascading Style Sheets" For Styling WWW Information: Mike Pangburn
DSC340
Mike Pangburn
CSS Advantages
Makes website more flexible
CSS is reusable
Change stylesheet to change design of many pages
Example: CSS Zen garden http://www.csszengarden.com/
Easier to maintain
Cleaner HTML code
Separates styles from HTML tags and page content
Consistent look across entire website that is easily maintained by
changing styles in one place.
CSS Disadvantages
Not uniformly supported by all
browsers.
Firefox adheres to CSS standards
more than IE
For this course we use Firefox
CSS: adding style
CSS allows you to add “style” to an HTML (web page)
element
E.g., color, size, or positioning information
There are two aspects to adding style to a web page via CSS
Specifying what the style looks like
Called the CSS style “Declaration”
Naming the HTML (or XML) element to which the style applies
Referred to as specifying the CSS “Selector”
CSS: adding style
Wildcard selector
*{}
Selects all elements on a page
Can be used in combination with other selectors
Aside: grouping selectors
You can apply the same declaration to a group of
selectors by listing all of the desired selector names
separated by commas.
Example:
h1, h2, h3, h4, h5, h6 {color:#ff0000; font-family:sans-serif}
CSS: selector flexibility
You could use the declaration with the selector just for
the HTML <p> tag
p{
font-size: 10px;
background-color: #fff;
color: #222; }
…this tells the browser to apply the declared style to
HTML <p> tags.
But, what if you want <p> blocks in the About Me
section to look one way, and those within your
Education section to be styled differently?
Naming HTML elements
There are two naming options for an HTML element: assigning “ID”
names and “class names.”
<html>
<body>
<h1 class=”myboldandbluelook”> Introduction </h1>
</body>
<html>
Connecting a style declaration to a class name
To connect a style declaration to a particular class name
you wrote into your HTML document, you simply precede
the class declaration with: .theclassname
Example
.myboldandbluelook
{
Aside: if you want this style to be used
font-weight: bold; only once in the web page, then specify it
color: blue; as an ID style with this slight syntax
} change:
#myboldandbluelook
{
font-weight: bold;
color: blue;
}
More on selector options
Descendant (nested) selector
ul li a strong{color:green;}
Syntax is similar to the example of grouping selectors—but
without the commas
Related point: if both ID (#) and class (.) styles to the same HTML element,
the ID style “wins” because ID styles are supposed to be used just once per
web-page (thus, in some sense, quite specifically)
CSS: the cascade
What if there is a “tie” regarding how specific the selectors are?
p{font-weight:bold;}
p{font-weight:normal;}
p{color:green;}
<head>
<style type=“text/css”>
CSS Code Here
</style>
</head>
Preferred method: External Style Sheet
Aside: the above “link” tag works for Importing a stylesheet, and
there is also an equivalent “<@import>” tag
Recap: 3 places to define styles
Inline – apply style attribute to a single tag
Takes a lot of work to maintain across a website
Internal, (“embedded,” “global”)
stylesheet defined in the <head> tag of a page