CS Students' Brief On CSS
CS Students' Brief On CSS
Background
An early goal of the WWW Easy to update many pages at once Easier to maintain consistency
CSS 1 CSS 2
Extended the scope of the rules
2
We'll examine 4 interesting parts of the presentational instructions and options later
Colour
Font
Border
Position
What's Next?
Introduction to CSS rule method CSS selectors How CSS matches rules to elements
User agent (browser's default settings), Webpage (source file), The user (personal settings in the browser)
E.g. p {text-indent:1.5em }
Rules
Attached to elements
As attributes of elements (inline style) Tied to id attribute of elements Tied to class attribute of elements {Property Name : Value;}
Selectors
More often to a class of element <cite class="textbook book"> Matches both textbook and book
Special Elements
div and span
Only for grouping other elements div is block-level (think about paragraphs) span is in-line (think about <code>)
Selectors (cont.)
See the handout for more pattern matches Resources about selectors are listed on a later slide (just after the cascade)
9
UL
</head> <body>
<h1></h1> <p><span></span></p> <ul>
<li></li> <li></li> <li><span></span></li>
SPAN
LI
LI
LI
SPAN
</ul> <p></p>
</body>
</html>
will h1 + p match? What will ul > span match? What will ul {color:blue} do?
What
13
It works on elements that the same rules apply to It is only used for tie-breaking when 2 rules apply
The highest ranking rule wins Most specific wins (usually) But important rules override others
user !important > author !important > inline style inline style > author plain > user plain > browser
3.
15
b.
c. d.
class is the only attribute that can be selected with the . in CSS An equivalent method is shown on the next slide
16
The cascade algorithm in the standard uses a semi-numerical algorithm The computation looks like this: class is an attribute
1 if the selector is an inline style a=
0 otherwise
b= c= d=
Number of id attributes (but only if specified with #) Number of attributes (except those in b) and pseudo-attributes specified Number of non-id elements specified (including pseudo-elements)
To find the value for an element/property combination, user agents must apply the following sorting order:
Find all declarations that apply to the element and property in question, for the target media type. Declarations apply if the associated selector matches the element in question. 2. Sort according to importance (normal or important) and origin (author, user, or user agent). In ascending order of precedence: a. user agent declarations b. user normal declarations c. author normal declarations d. author important declarations e. user important declarations 3. Sort rules with the same importance and origin by specificity of selector: more specific selectors will override more general ones. Pseudo-elements and pseudo-classes are counted as normal elements and classes, respectively. 4. Finally, sort by order specified: if two declarations have the same weight, origin and specificity, the latter specified wins. Declarations in imported style sheets are considered to be before any declarations in the style sheet itself. Apart from the !important setting on individual declarations, this strategy gives author's style sheets higher weight than those of the reader. User agents must give the user the ability to turn off the influence of specific author style sheets, e.g., through a pull-down menu. CSS 2.1 6.4.1 Cascading order
1.
Pseudo-Elements? Pseudo-Attributes?!
CSS introduces the concepts of pseudo-elements and pseudo-classes to permit formatting based on information that lies outside the document tree.
Classes
Elements
Inline
<p style=text-align: center ></p> Inside the head element <link rel="stylesheet" type="text/css" href="site.css" /> <style type="text/css"></style> <style type="text/css">
@import url(site.css); /* other rules could go here */
</style>
22
Simple Example
23
Visual Formatting
24
font-family
body {font-family: Garamond, Times, serif} Serif fonts and sans-serif fonts
font-size:
Length (em,ex), percentage, relative size, absolute size
font-style:
Normal, italic, oblique
font-weight:
Lighter, normal, bold, bolder, 100, 200, , 800, 900
25
How to specify
16 Predefined names RGB values (%, #, 0255) System names: e.g. CaptionText See Lynda Weinman's charts Okay for photos, etc.
Dithered Colour
26
Major properties
background-color color
27
position:
static, relative, absolute, fixed
Static normal elements Relative translate from usual position Absolute scroll with the page Fixed like absolute, but don't scroll away Example: Jon Gunderson
28
29
Four types again Can all be set at once with border See Border slides by Jon Gunderson
31
Padding
Margin
Width is of content only. Neither the border nor the padding are included in width.
32
Jump Box
34
35
36
body {padding:4em}
37
38
div.up dl {margin:4em}
39
40
41
visible:hidden
element can't be seen but it still uses space
display:none
element isn't shown
visibility
42
Order of presentation Extra/hidden content v4.0 browsers dont recognize @import Some browsers ignore media rules Later, and more specific, rules override other rules Example follows
43
There are no fool-proof workarounds for every bug in every browser Some workarounds are incompatible with strict XHTML The workarounds take time and are sometimes inelegant But they are necessary if you want to reach the largest possible audience For more about hacks see <URL:http://tantek.com/log/2005/11.html>
45
46