Misy233 CH3
Misy233 CH3
CHAPTER 3
3.1. Introduction
• When more than one style sheet applies to a specific tag in a document, the
lowest level style sheet has precedence
• In a sense, the browser searches for a style property spec, starting with inline, until it
finds one (or there isn’t one)
3.2 Levels of Style Sheets (cont.)
• External style sheets are in separate files, potentially on any server on the Internet
• Written as text files with the MIME type text/css
• A <link> tag is used to specify that the browser is to fetch and use an external style sheet file
<link rel = "stylesheet" type = "text/css"
href = "http://www.wherever.org/termpaper.css">
</link>
• An alternative way to reference an external style sheet:
@import url (filename);
• Appears at the beginning of the content of a style element (later)
• General form:
<style type = "text/css">
rule list
</style>
• Form of the rules: selector {list of property/values}
• Each property/value pair has the form:
property: value
• Pairs are separated by semicolons, just as in the value of a <style> tag
• External style sheets
• Form is a list of style rules, as in the content of a <style> tag for document-
level style sheets
3.4. Selector Forms
• 2. Class Selectors
• Used to allow different occurrences of the same tag to use different style specifications
• A style class has a name, which is attached to a tag name
• For example,
p.narrow {property/value list}
p.wide {property/value list}
• The class you want on a particular occurrence of a tag is specified with the class attribute of the tag
• For example,
<p class = "narrow">
...
</p>
...
<p class = "wide">
...
</p>
3.4. Selector Forms (cont.)
• 3. Generic Selectors
• A generic class can be defined if you want a style to apply to more than one
kind of tag
• A generic class must be named, and the name must begin with a period
• Example,
.sale { … }
• Use it as if it were a normal style class
<h1 class = "sale"> Weekend Sale </h1>
...
<p class = "sale"> … </p>
3.4. Selector Forms (cont.)
• 4. id Selectors
• An id selector allows the application of a style to one specific element
• General form:
#specific-id {property-value list}
• Example:
#section14 {...}
• 5. Universal Selectors
* {color: red;}
• Applies to all elements in the document
3.4. Selector Forms (cont.)
• 6. Pseudo Classes
• Pseudo classes are styles that apply when something happens, rather than because the target element simply exists
• Names begin with colons
• hover classes apply when the mouse cursor is over the element
• focus classes apply when an element has focus
• font-family
• Value is a list of font names - browser uses the first in the list it has
• font-family: Arial, Helvetica, Futura
• Generic fonts: serif, sans-serif, cursive, fantasy, and
monospace (defined in CSS)
• Browser has a specific font for each
• If a font name has more than one word, it should be single-quoted
• font-size
• Possible values: a length number or a name, such as smaller, xx-
large, etc.
• Font variants
• Default is normal, but can be set to small-caps
3.6. Font Properties (cont.)
• font-style
• italic, oblique (useless), normal
• font-weight - degrees of boldness
• bolder, lighter, bold, normal
• Could specify as a multiple of 100 (100 – 900)
• font
• For specifying a list of font properties
font: bolder 14pt Arial Helvetica
• Order must be: style, weight, size, name(s)
• The text-decoration property
• line-through, overline, underline, none
• letter-spacing – value is any length property value
3.7. List Properties
• list-style-type
• Unordered lists
• Bullet can be a disc (default), a square, or a circle
• Set it on either the <ul> or <li> tag
• On <ul>, it applies to all items in the list
• On <li>, list-style-type applies to just that item
3.7. List Properties (cont.)
• Unordered lists (cont.)
<h3> Some Common Single-Engine Aircraft </h3>
<ul style = "list-style-type: square">
<li> Cessna Skyhawk </li>
<li> Beechcraft Bonanza </li>
<li> Piper Cherokee </li>
</ul>
• Padding – the distance between the content of an element and its border
• Controlled by padding, padding-left, etc.
3.11. Background Images
• The background-image property
<p>
Now is the <span> best time </span> ever!
</p>
<html>
<style>
.div1 { background:#FF0000; }
.div2 { background-color:green; }
</style>
</head>
<body>
<h1> THIS IS A DIV EXAMPLE</h1>
<div class="div1"> test1 </div>
<div class="div2"> test2 </div>
</body>
</html>
3.12 Conflict Resolution
• A conflict occurs when there are two or more values for the same
property on the same element
• Sources of conflict:
• 1. Conflicting values between levels of style sheets
• 2. Within one style sheet
• 3. Inheritance can cause conflicts
• 4. Property values can come from style sheets written by the document
author, the browser user, and the browser defaults
• Resolution mechanisms:
• 1. Precedence rules for the different levels of style sheets
• 2. Source of the property value
• 3. The specificity of the selector used to set the property value
• 4. Property value specifications can be marked to indicate their weight
(importance)
3.12 Conflict Resolution (cont.)
• Weight is assigned to a property value by attaching !important to the
value
• Conflict resolution is a multistage process, called the cascade:
• 1. Gather all of the style specs from the different levels of style sheets
• 2. All available specs, from all sources, are sorted by origin and weight, using the
following rules, which are given in precedence order:
• a. Important declarations with user origin
• b. Important declarations with author origin
• c. Normal declarations with author origin
• d. Normal declarations with user origin
• e. Any declarations with browser (or other user agent) origin
• 3. If any conflicts remain, sort them by specificity:
• a. id selectors
• b. Class and pseudo-class selectors
• c. Contextual selectors
• d. Universal selectors
• 4. If there are still conflicts, resolve them by precedence to the most recently seen