630 likes | 740 Views
CSS: Cascading Style Sheets. History. HTML tags were originally designed to define the content of a document. The layout of the document was supposed to be taken care of by the browser, without using any formatting tags.
E N D
History • HTML tags were originally designed to define the content of a document. • The layout of the document was supposed to be taken care of by the browser, without using any formatting tags. • As the two major browsers - Netscape and Internet Explorer continued to add new HTML tags and attributes (ex: <font> tag, color attribute) to the original HTML specification, it became more and more difficult to create Web sites where the content of HTML documents was clearly separated from the document's presentation layout.
What are Style Sheets • A style sheetis 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.
Style Sheets • A file that ends with .css • Contains a list of style rules for HTML elements • Case insensitive • Comments are enclosed in /* */ • Demo: http://www.w3schools.com/css/demo_default.htm
Simple Example <html> <head><title>A Joke</title></head> <body> <div><imgsrc="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> The <div> tag defines a division/section in a document.
Style File: joke.css body { background-image:url("water.jpg"); } h1 { background-color: green; color:rgb(250, 200, 250); /* pink */ font-family:cursive } p { background-color: yellow; color: purple; font-size:200%;}
Simple Example (with css) <html> <head><title>A Joke</title> <linkrel="stylesheet"type="text/css"href="joke.css"/> </head> <body> <div><imgsrc="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>
Why do we Need a Style Sheet? • Separates content from format • In HTML 4.01 styling is very limited • Consistent appearance over a site • Allows to easily change style • In one page • In a whole site • Reduces download time (how?) the same style sheet may apply to many pages in a website reduces the need to send redundant presentation information over the network
How and Where? • The style rules appear either in the document or in external files, called style sheets • Inside a document, there are inline styles and embedded style sheets • External style sheets are either linked or imported
Inline Styles • The style declaration appears as the value of the attribute style of the element • Almost every tag can have the style attribute • Some exceptions: head, html, meta, style and title • Use this method sparingly, such as when a style is to be applied to a single occurrence of an element. <pstyle="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>
Embedded Style <html> <head> <style type="text/css"> body{color: red;background: skyblue;} h1{color: blue } </style> </head> <body>... </body> </html> Inside the head element • Use when a single document has a unique style.
Link External Style Sheets Inside the head element <html> <head> <link rel="stylesheet" type="text/css" href="mystyle.css" /> </head> <body>... </body> </html> • Ideal when the style is applied to many pages.
Imported Style Sheets • The@importrule imports style rules to the beginning of the style sheet • The @import rule imports the style rules of another style sheet • Several import rules may appear @importurl(general.css); body { color: red;background:skyblue } h1 {color: blue } An Example:
Declaration Selector Property Value(s) Style Rules h1 { color:purple; font-family: Impact, Arial Black; font-size-adjust:.46; font-size:2.33em; font-weight:400; font-style:normal; text-decoration:none; word-spacing:normal letter-spacing:normal; text-transform:none; }
Style Rules (cont) • A rule has the following form: selector {property: value} • Selector: HTML element/tag to define • Property: attribute to change • Value: value given to the property • For example, the following rule applies to text that is inside a <p> tag em: the 'font-size' of the relevant font p{color: green;font-size:1.5em;font-style:italic}
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 (list, table, border) • Everything is in the Web ! • A good source: http://www.w3schools.com/css
Our Examples • We use the following HTML example: This is<span>our example </span>for css. • The <span> tag is used to group elements for formatting with styles • Extremely useful tag...
Font Properties • Font properties: family, size,weight,style,... span { font-family: courier; font-size:130%; font-style:italic; font-weight:bold}
Text Properties • Text properties: color, transform, decoration, word-spacing, text-align, direction, … span { color:#00cc00; text-decoration:line-through; text-transform:uppercase}
Background Properties • Background properties: background-color, background-image, background-repeat,… span {background-color:#00ff00} span {background-image:url('bg.gif');}
Page Layout • 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
Inline vs. Block Elements • There are two types of elements: • Block elements: p, ol, table, div, h1, etc. • Inline elements: b, i, a, span, 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 or block, using the display property, e.g., h1 { display: inline }
Positioning Elements • 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; color:#00cc00;}
Relative vs. Absolute • In CSS, positions (of boxes) and sizes (of fonts and boxes) could be either relative or absolute • In an “absolute” style • Font size is fixed • Sizes and positions of elements are fixed • In a “relative” style • you can change the font size • The sizes and positions of elements may change when the size of the window is changed
Position Types • But 1cm left of what?? • For that, we have the position property • Four position types: • static: the default position • relative: relative to the static position • absolute: relative to the parent layer coordinates • fixed: relative to the window coordinates
Position Examples span { position:static; left:1cm; top: 1cm; color:#00cc00;} Totally Ignored! This is thedefault position type
Position Examples span { position:absolute; left:1cm; top: 1cm; color:#00cc00;} span { position:fixed; left:1cm; top: 1cm; color:#00cc00;}
More Layout Properties • Layer properties • margin (-top, -bottom, -left, -right) • Defines the space around elements • border (-width, -color, -style, … ) • Defines the border around an element • padding (-top, -bottom, -left, -right) • Defines the space between the element border and the element content • Text Layout • direction, word-spacing, white-space, letter-spacing, line-height, text-align, text-indent, …
Length Units • CSS has several types of length units: • em, ex: height of current fonts (relative) • px, in, cm, mm, pt, pc: international units • %: ratio of parent’s respective dimension • A page should remain a proper layout when windows (and even fonts) resize • Hence, do not assume anything about default sizes, always check
Several Kinds of Selectors • Type Selectors • Class Selectors • ID Selectors • Attribute Selectors • Universal Selector • Descendant Selectors • Pseudo-Class Selectors • Pseudo-Element Selectors
Type Selector • A type selector is the name of an element type • A type selector matches every instance of the element type LI {color: red;font-size:16px} Matches: <ol> <li> An item </li> <liclass="reditem"> Another item </li> </ol>
Class Selector • A class selector is a selector of the form x.y • It matches xs that have the classattribute with value y LI.reditem {color: red} .reditem { color: red} will also work! Matches: <ol> <li> An item </li> <liclass="reditem"> Another item </li> </ol>
ID Selectors • IDs are identical to classes, except that there can only be one element with a given ID in a document LI#23 {color: red} #23 { color: red} will also work! Matches: <ol> <li> An item </li> <liid="23"> Another item </li> </ol>
Attribute Selectors • p[attribute] • matches p when 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”
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 }
Descendant Selector • A descendant selector has the form S1 S2 where S1 and S2are selectors • It matches all elements that • match S2, and • are descendants (nested in) elements that match S1
An Example 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? .reditem em {color: blue}
Pseudo-Classes • Pseudo class selectors are similar to class 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
Examples of Rules for Pseudo-Classes • A:link {color: blue} • A:visited {color: purple} • A:hover {font-size: 1.5em} • A:active {color: red} • INPUT:focus {background-color: yellow}
Pseudo-Elements • The syntax of pseudo-elements: selector:pseudo-element {property: value} • CSS classes can also be used with pseudo-elements: selector.class:pseudo-element{property: value}
Grouping Selectors • We can group several declarations by specifying several selectors, separated by commas • For example, the following rule applies to all elements that match eitherH1,P B, orH2[class=largehead] P B, H1, H2.largehead{font-size: 120%}
Adding Style to Inner Text • We want to add style to a fragment of some text • We need to wrap the text with tags that do not have a style of their own: • <SPAN> Some text </SPAN>: does not stop the text flow • <DIV> Some text </DIV>:separated from the other text