What are Style Sheets CSS: Cascading Style Sheets Representation and Management of Data on the Internet, CS Department, Hebrew University, 2007 A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style is specified by style rules The style rules appear either in the document or in external files, called style sheets W3Schools CSS tutorial 1 2 Style Sheets Usually, a file that ends with.css For example: Without a style sheet (i.e., with the default style definitions) - i.a.cnn.net/cnn/.element/ssi/css/1.1/main.css (CNN) - http://www.huji.ac.il/styles/style.css (HUJI) To attach a style sheet to an HTML file, add <link rel="stylesheet" type="text/css" href="css-file"/> to the head You can link to more than one css file 3 4 Simple Example without css <html> <head><title>a Joke</title></head> <body> <div><img src="tomato.gif" alt="joke"/></div> <h1>a joke</h1> div defines a division/section in a document. Using div you to group block elements and give them an equal style <p>a mama tomato, a papa tomato and a baby tomato are walking down the street. The baby tomato keeps falling behind so the papa tomato goes back, steps on the baby tomato and says, ketchup ("Catch-up!"). </p> </body> </html> 5 6 1
Style File: joke.css Simple Example - with css body { background-image: url("bg.gif"); } h1 { background-color: green; color: rgb(250, 200, 250); /* pink */ font-family: cursive} p { background-color: yellow; color: purple; font-size: 200%;} 7 <html> <head><title>a Joke</title> <link rel="stylesheet" type="text/css" href="joke.css"/> </head> <body> <div><img src="tomato.gif" alt="joke"></div> <h1>a joke</h1> <p>a mama tomato, a papa tomato and a baby tomato are walking down the street. The baby tomato keeps falling behind so the papa tomato goes back, steps on the baby tomato and says, ketchup ("Catch-up!"). </p> </body> </html> Open joke.html, joke_css.html 8 Background: Style in Legacy HTML In traditional HTML, every HTML tag which supported changing its font/colour/etc, supported this using a different syntax. Even tags which provided attributes which control their appearance, allowed for very limited customization. At a certain point, it was clear that because of these issues, the entire styling part of HTML needed to be revamped. One possibility to address this could have been adding a great number of standard styling attributes (spacing, font size, location, etc ) to all tags. Customizing each and every tag Why is this approach bad? with >10 attributes would have made HTML pages impossible to 9 write without automated tools! 10 The Solution: Style Sheets The Transition to Style Sheets Separates content from style Keeps HTML pages human-readable (how?) Reduces download time (how?) Allows to easily maintain a consistent appearance over a whole Web site (how?) A more flexible, unified way to specify style properties for different HTML elements (tables, paragraphs, etc ) 11 Style Sheets provide far more control over the appearance of the document than was available before but they also create redundancy: they provide new ways to accomplish tasks which were already possible. Once style sheets were introduced, most of the legacy features which provided equivalent functionality were deprecated in HTML 4.01 and in Transitional XHTML and were removed in Strict XHTML. It is good practice to use strict XHTML! 12 2
h1 { Style Rules Style Rules (cont) color: purple; Selector font-family: Impact, Arial Black; font-size-adjust:.46; Declaration A rule has the following form selector {declaration block} font-size: 2.33em; Property font-weight:400; Value(s) font-style:normal; text-decoration: none; word-spacing:normal!important; letter-spacing:normal; important rule The selector determines when the rule is applied For example, the following rule applies to text that is inside a <p> tag p{color: green; font-size: 1.5em; font-style: italic} text-transform: none; } 13 em is the current font-size of the current element 14 What Kind of Properties can a CSS Style Sheet Change? Style properties Layout properties There are many properties and many possible values - We will not cover all of them here - Look in the Web!!! Style Properties 15 16 Our Examples We use the following HTML example: This is <span> our example </span> for css. The <span> tag is used to group inline elements for formatting with styles - Extremely useful tag... Font Properties Font properties: family, size, weight, style, variant,... span { font-family: courier; font-size: 130%; font-style: italic; font-weight: bold} Enable to transform letters into small-caps 17 18 3
Text Properties Text properties: color, transform, decoration, Transforms the letters case (upper / lower / capital) span { color: #00cc00; text-decoration: line-through; text-transform: uppercase} Background Properties Background properties: background-color, background-image, span {background-color: #00ff00} span {background-image: url('bg.gif');} 19 20 Page Layout Layout Properties Each HTML element defines a layer (rectangular box) that is placed in some location on the page Layers are nested with correspondence to the nesting of their elements 21 22 Inline vs. Block Elements Positioning Elements There are two type of elements: Block elements: p, ol, table, div, h1, etc. Inline elements: b, i, a, span, cite, etc. Layers of block elements are separated from their adjacent elements (i.e., a new line before and after), while inline elements are not You can turn a block into an inline and vice-versa (highly useful for img elements, among others), using the display property, e.g., h1 { display: inline } 23 Using CSS, you can define the position of an element inside its parent layer For that, use the properties position, left, right, top and bottom span { position:relative; left: 1cm; top: 1cm; distance from left color: #00cc00;} distance from top 24 4
Position Types Position Examples But 1cm left to what?? For that, we have the position property Four position types are supported: - static: the default position - relative: relative to the static position - absolute: relative to the parent layer coordinates - fixed: relative to the window coordinates (remains at span { position:absolute; left: 1cm; top: 1cm; color: #00cc00;} span { position:fixed; left: 1cm; top: 1cm; color: #00cc00;} the same position regardless of scrolling) 25 26 Position Examples More Layout Properties Totally Ignored! span { position:static; left: 1cm; top: 1cm; color: #00cc00;} This is the default position type Layer properties - margin-top (-bottom, -left, -right) - padding-top (-bottom, -left, -right) - border-width (-color, -style, ) Text Layout The space around elements. Could be negative The space between the element borders and the element content. - direction, word-spacing, white-space, letterspacing, text-align, text-indent, Open joke1.html 27 28 Length Units CSS has several types of length units: em is the distance between the bottom of one row and the next (something like a grid size). - em, ex: height of current fonts (ex is the x-height) - px, in, cm, mm, pt, pc: international units - %: ratio of parent s respective dimension the height of a lowercase x. A page should remain a proper layout when fonts and/or windows are resized (usually by the user) - Hence, do not assume anything about default sizes Selector Types 29 30 5
Several Kinds of Selectors Type Selector Type Selectors Class Selectors ID Selectors A type selector is the name of an element type A type selector matches every instance of the Attribute Selectors Universal Selector Child Selectors Adjacent-Sibling Selectors Descendant Selectors Pseudo-Class Selectors Pseudo-Element Selectors Not supported by IE 5, 5.5 and 6. The good news (to us): supported by IE 7 The bad news (to others): can no longer be used to hide CSS code from IE 31 element type li {color: red; font-size: 16px} Matches: <ol> <li> An item </li> <li class="reditem"> Another item </li> </ol> 32 Universal Selector The universal selector matches every element The following rule means that all the text will have a size of 40px * {font-size: 40px } Attribute Selectors p[title] - matches p when its title attribute is set to any value p[title=intro] or p[title="intro"] (the quotes are optional) - matches p when its title attribute is set to intro p[class~=green] - matches p when the class attribute value includes the word green 33 34 Class Selector ID Selectors A class selector is a selector of the form x.y It matches xs that have the class attribute with value y (i.e., it is a shorthand for x[class=y]) li.reditem {color: red} Matches:.reditem {color: red} will <ol> also work! (it will match any element <li> An item </li> with class="reditem ) <li class="reditem"> Another item </li> </ol> 35 IDs are similar to classes, except that there can only be one element with a given ID in a document That is, an ID is unique per element li#23 {color: red} Matches: <ol> <li> An item </li> <li id="23"> Another item </li> </ol> #23 { color: red} will also work! 36 6
Descendant/Child/Sibling Selector emphasized text An Example A descendant selector has the form S 1 S 2 where S 1 and S 2 are (possible complex) selectors It matches all elements that - match S 2, and - are descendants (nested in any level in) elements that match S 1 To match only immediate descendants (children), use a Child Selector which has the form S 1 >S 2 To match S 2 immediately following S 2, use an Adjacent p em {color: blue} Matches: This is <em>not blue</em>. <p> This is <em> blue </em> <span><i>and so is <em> this </em></i></span>. </p> What will this match? Sibling Selectore which has the form S 1 +S 2.href div>span em {color: blue} 37 38 Pseudo-Classes Examples of Rules for Pseudo-Classes Pseudo class selectors are similar to class a:link {color: blue} selectors, but they match states rather than class values - For example, a link can be in the states: visited, active, mouse-over ( hover ), etc. - Another example: a text-area can be focused 39 a:visited {color: purple} a:hover {font-size: 1.5em} a:active{color: red} input[type=text]:focus {background-color: yellow} when typing a text into a text input box (meaningful with other input types as well) 40 Pseudo-Elements Grouping Selectors Pseudo element selectors select abstract elements which are not specified as elements in the source HTML. - For example, to transform the first line of every p into uppercase, use: P:first-line {text-transform: uppercase} Why can t this be faked by enclosing the first line with a span? The definition of first line might change, for example, if the user resizes the window 41 We can group several declarations by specifying several selectors, separated by commas For example, the following rule applies to all elements that match either h1, p b, or h2[class=largehead] p b, h1, h2.largehead {font-size: 120%} 42 7
Inline Styles Inserting Style to a Page In an inline style, the declaration block is the value of the attribute style of the element <p style="color: green; font-size: 1.5em; font-style: italic"> This text will be shown in italic green and the size will be 1.5 times the current font size </p> 43 Almost every tag can have the style attribute - exceptional: base, head, html, meta, param, script, style and title 44 Document-Level Style Imported Style Sheets <html> <head> <style type="text/css"> The @import rule imports the style rules of another style sheet body {color: red; background: skyblue;} Usage: @import url(file.css) h1 { color: blue } </style> Several import rules may appear at the beginning of the style sheet </head> <body>... </body> Import rules can appear in embedded style sheets or in external style sheets </html> 45 46 Imported Style Sheets An Example: @import url(general.css); body { color: red;background:skyblue } h1 { color: blue } Inheritance and Cascading Why do we need the import command when we have the <link> tag? Using @import in a css file, one can create stylesheets which are based on others 47 48 8
Inheritance of Properties Consider a property of an element that does not match any rule For some properties (inherited properties), the computed value of this property is inherited from the parent of the element For example, color, font and word-spacing are inherited Number of white spaces between words Yet border, margin and padding are not! An Example Given the rules: - body { font-size: 10pt } Computed Value: 12pt - h1 { font-size: 120% } What will be the font size of the <em> element? <body> <h1>a <em>large</em> heading</h1> </body> 49 50 Cascading of Styles CSS merges style rules from different places (inline, document-level, linked and defaults) Different places may have conflicting style rules - conflicts may even arise in a single source The process of merging (cascading) styles from different places determines which style rules have higher priority Determining Property Values Suppose that we would like to determine the value of property p for element e Choose all declarations that have a selector that matches e and have the property p Sort all declarations according to cascading order Choose the first declaration in the cascading order, and apply the corresponding rule 51 52 Cascading Order Importance of Origin The cascading order of declarations: 1. Primary sort: importance of origin 2. Secondary sort: specificity of selectors 3. Final sort: order of appearance 53 There are two rule origins: author and browser (either defaults or user customizations) strongest weakest browser!important rules author!important rules author rules browser rules For example, you can add stylesheets to IE in the following way: Tools internet options Accessibility User style sheet. Of course you can add!important browser rules this way as well 54 9
strongest weakest Specificity of Selectors is rule in style attribute? number of ID attributes number of attributes and pseudo-classes number of element names An Example Consider the following rules: - li { } - #x34y { } - ul ol li.red { } Which is the most specific? li.red is a descendent of ol, which is a descendant of ul 55 56 Order of Appearance If two rules have the same origin importance and specificity, then the one that appears last in the style sheet overrides the others Rules in imported style sheets (@import) are considered to appear before local rules 57 10