CSS-I
CSS-I
• Why is it called “cascading”? In CSS, multiple styles can be applied to a particular document (usually
a web page or XML file). The browser will interpret these styles in a cascading fashion:
• Style rules set up site-wide are overridden by styles located within individual pages.
• Individual pages are overridden by styles inside an individual tag.
• In addition, the end user can set up styles in the browser that will override the author’s styles.
• All matching rules for a particular selector will be applied, except where they conflict with each other (in which case, the latter
rule would be applied, as determined by the cascade). In the following example, <h2> tags would be displayed in red and
italics (but not blue):
h2 {font-style: italic;}
h2 {color: darkblue;}
h2 {color: red;}
• To properly see the effects of the Style Sheets, your visitors will need to use a web browser that is version 4.0 or
newer. Fortunately, using CSS does not cause web pages to break when viewed on older browsers; however, the
styles won’t appear as defined. Since most people these days are using Internet Explorer 6, Netscape 7, or Firefox 1
or newer, most browsers can properly display CSS.
• CSS-aware browsers apply their own stylesheet for every HTML element as the first set of rules in the cascade, and
this set of rules form the default display for every element. For example, most browsers treat the <p> tag as a block
element, as though there were the explicit declaration p { display: block;} By using CSS, you modify the
default settings by overriding these implicit styles with an explicit declaration (for more on the block display, see
slide 17).
• By using CSS, you can also control text formatting and location on the page. Using CSS can eliminate the need for
tables as a layout tool. With CSS, logos can be created using just text, instead of having to rely on graphics. These
changes make pages more accessible to a wider audience.
• CSS Specifications:
• CSS 1: http://www.w3.org/TR/REC-CSS1-961217.html
• CSS 2: http://www.w3.org/TR/CSS2/
• CSS 2.1: http://www.w3.org/TR/CSS21/
• Differences between CSS 1, CSS 2, and CSS 2.1:
• Between CSS 1 and CSS 2: http://www.w3.org/TR/CSS2/changes.html
• Between CSS 2 and CSS 2.1: http://www.w3.org/TR/CSS21/changes.html
• Cons
• Different browsers may interpret Style Sheets in different ways
• Some styles may not be seen at all on some browsers
• Additionally, multiple selectors that have the same styles can be grouped by separating them with commas:
h1, h2, h3 { color: darkblue; font-style: italic;}
• Contextual selectors allow you to specify that something will change, but only when it is used in conjunction with something else. With the following style, strong will be
displayed in red, but only when it occurs within li within ul.
ul li strong { color: red;}
Elements being modified by contextual selectors need not appear immediately inside one another (using this style, blah would still be red text: <ul><ol><li><strong>
blah </strong></li></ol></ul>).
• Direct child selectors allow you to specify that something will change, but only those that are immediately inside of another element. With the following style, only those
strong elements that are directly inside of an h1 will be purple; no strong tags deeper within the sheet will be purple.
h1 > strong { color: purple;}
• Adjacent selectors allow you to specify that something will change, but only when preceded by something else. With the following style, only those links (a) that are
preceded by an h2 will be green.
h2 + a { color: green;}
Elements being modified by adjacent selectors appear immediately after one another. Using this style, this link would be green: <h2>Visit Stanford!</h2><a
href="http://www.stanford.edu">click here</a>.
This link would not: <h2>Visit Stanford! <a href="http://www.stanford.edu">click here</a></h2>.
• You can also group selectors by attribute. With the following style, centered h2 tags (<h2 align="center">) will be surrounded by a dotted border:
h2[align="center"] { border: dotted;}
<style type="text/css">
<head>
<!--
<link rel="stylesheet" type="text/css"
href="basic.css" media="all" /> @import url("basic.css") all;
</head>
• For example, if an external Style Sheet sets <h2> tags to purple and a particular page has an
internal style that changes that color to orange, the <h2> tags will be orange only on that
page and nowhere else in the site. If there is a single <h2> tag on that page which specifies
green as its color, then the color for that one tag will be green. All other <h2> tags on that
page would be orange; the <h2> tags on the rest of the site would be purple.
• However, by entering !important in a rule, that rule will take precedence, regardless of its
location. In this example, the body font will be Times, not Verdana.
body {font-family: Times !important;
font-family: Verdana;}
Note: !important does not work with all properties in Internet Explorer.
• IDs are similar to classes, but IDs are unique – they can only be used with one instance of an element within a
document.
• When defining a CSS rule using an ID-based selector, use a number/pound/hash sign (#) followed by the style name.
To limit the style to a particular element with that id attribute, use a selector combining the tag name with a # and
then the id name.
• The following rule would apply to any element with the attribute id="intro"
#intro { font-size: 2em; }
• The following rule would apply only to heading 1 tags with the id intro (<h1 id="intro">)
h1#intro { color: green; }
• Relative values have no fixed, specific values, and are calculated in comparison to something else (usually the size of the default
font or line size). Because different computers have different video cards, screen sizes, and users have differing eyesight abilities,
relative values tend to be a better choice. They give the designer less absolute control but it often creates a better experience for the
visitor.
em, ex, px, larger, smaller, num%
• Examples:
body { font-size: 12px; }
h1, h2, h3 { line-height: 200%;}
• Note – a warning about using percentages: if you use percentages, and nest an element inside of that same element, the
percentages will be cumulative.
To apply a font to a specific section of text, create a class, and use the span tag with that class:
.neatstuff {font-family: Comic Sans MS;}
<span class="neatstuff">This is in Comic Sans</span>
• Examples:
ul { list-style: disc; }
ol { list-style: upper-roman;}
li { list-style: url("blackball.gif");}
ul li { list-style-position: inside;}
• Under standard HTML, there is no mechanism to deal with mouse movements. But with CSS, the
pseudo-class :hover can be used to change the style of a link. In this example, a:hover is used to
change the link color to red and the underlining to disappear whenever a mouse hovers over links:
a:hover {color: #ff0000; text-decoration: none;)
• Absolute positioning defines the position of a given bounding box from the top and left side margins of
the web page. This not only allows objects to be placed in an exact location, it also allows objects to
be placed one on top of another.
• Relative positioning defines the positioning in such a way that elements are offset from the previous
element in the HTML code. This allows objects to be placed in relation to one another.
• Fixed positioning defines the position of a given box relative to the window and remains in its specified
location even as the content scrolls underneath it. This value does not work in Internet Explorer 6 or
earlier. In IE 7, the browser must be in “standards-compliance mode”.
• Inherit positioning explicitly sets the value to that of the parent (if the parent is position:absolute, the
child will be position:absolute; if the parent is position:fixed, the child will be position:fixed).
• Static positioning is the default. It defines the position of a given box essentially as an unpositioned
element – it flows in the normal rendering sequence of the web page.