CSS (Cascading Style Sheets) allows separation of document content from document presentation and behavior. CSS handles the look and formatting of a document and is effective for maintaining a consistent appearance across multiple web pages. CSS declarations apply styles to HTML elements and are organized in a cascade by importance, origin, specificity, and source order to determine which styles get applied.
What is CSS?Every web page is composed of HTML (HyperText Mark-up Language) code that describes the content
6.
Example: <p> An<strong> important </strong><font color="#FFFF00"> paragraph </font> . </p> Displays: An important paragraph . Repetitive and hard to maintain
7.
What is CSS?Layers of a web page: Content: Text, images, animation, video, etc.
8.
Presentation: How thecontent will appear to a human through a web browser, text reader, etc.
CSS2.1 is thelatest and current revision of the CSS2 specification
17.
CSS3 specification isstill in draft but some parts have been implemented by some browsers
18.
Linking CSS toHTML 1) Inline Styles: Using the style attribute which is supported by every HTML tag: <h1 style="color:red;"> My Headline </h1> The simplest way but repetitive across multiple HTML elements
19.
Linking CSS toHTML 2) Embedded Styles: Using the style tag: <style type="text/css"> h1{ font-family:Verdana } //all h1 tags .warning{ color:red } //tags with this class #footer{ font-size:10px } //tag with this id </style> <h1> My Header </h1> <p class=”warning” > WARNING </p> <div id=”footer” > eSpace 2008 </div> Repetitive across multiple pages
Properties Size units( relative and absolute ): px = Pixels on the screen em = Current font size ex = Height of lowercase "x" mm = Millimeters cm = Centimeters in = Inches (1 inch = 2,54 centimeters) pt = Points (1 point = 1/72 inches) pc = Picas (1 pica = 12 points)
33.
Properties Backgrounds: div{ background-color: Black; } body { background-image: url(logo.gif); background-color: white; background-attachment: fixed; background-position: right top; background-repeat: no-repeat; } body { background: white url(logo.gif) repeat-x fixed right top; }
34.
Selectors 1) Universalselector: * { margin: 0; padding: 0; } 2) Element type selector: span { font-family: Verdana } 3) Class selector: p.big { font-weight: bold; }
Selectors CSS3 newattribute selectors: a[href ^ ="http:"] { ... } /* matches a elements whose href attribute value starts with "http:" */ img[src $ =".png"] { ... } /* matches img elements whose src attribute value ends with ".png" */ div[id * ="foo"] { ... } /* matches div elements whose id attribute value contains "foo" */
37.
Selectors Grouping: div,p { font-family: Verdana } a img { border: none } ul li ol li { color: blue } #menu a, div li, .note { color: red }
38.
Selectors Child selector:ul>li { ... } <ul> <li> <ol> <li> Will not be matched. </li> </ol> </li> </ul>
39.
Selectors Adjacent siblingselector: - sibling = has the same parent element - adjacent = immediately following h2+p { ... } <div> <h2> Heading </h2> <p> Will be matched. </p> <p> Will not be matched. </p> </div>
40.
Selectors General siblingselector: - sibling = has the same parent element - general = just following h2~p { ... }
41.
Selectors Example: h2~p { ... } <p> Will not be matched. </p> <h2> Heading </h2> <p> Will be matched. </p> <div> <p> Will not be matched. </p> </div> <p> Will be matched. </p>
Pseudo-elements (virtual) Newin CSS3 :: selection { ... } //represents a part of the document that’s been highlighted by the user, including text in editable text fields
The Cascade Styledeclarations cascade down to elements from many origins.
49.
The cascade combinesthe importance, origin, specificity, and source order of the style declarations to determine which declaration should be applied to a given element.
The Cascade Importance://Normal declaration p {font-size: 1em} //Important declaration p {font-size: 1em !important ;}
52.
The Cascade Importanceand origins priorities (low to high): 1. User agent declarations 2. Normal declarations in user style sheets 3. Normal declarations in author style sheets 4. Important declarations in author style sheets 5. Important declarations in user style sheets
53.
The Cascade Specificity:When multiple declarations (with the same importance and origin) try to set the same property to an element, the declaration with the most specific selector will take precedence.
54.
The Cascade Calculatingspecificity: 1. Inline styles (highest specificity) 2. Count ID selectors 3. Count class selectors ( .test ), attribute selectors ( [type="submit"] ), and pseudo-classes ( :hover ) 4. Count element type selectors ( div ) and pseudo-elements ( :first-letter )
55.
The Cascade Theprocess of the cascade: 1. For a given property, find all declarations that apply to a specific element. (user agent, author, user-defined). 2. Sort according to levels of importance and origins. 3. Sort declarations with the same level of importance and origin by selector specificity. 4. If declarations have the same level of importance, origin, and specificity, sort them by the order in which they’re specified
56.
Inheritance The processby which properties are passed from parent to child elements without explicit definition div { font-size: 20px; } <div> <p> My <em> cool </em> paragraph is <a href="#"> here </a>. </p> </div>
Block vs InlineUsing the display property: #menu li { display: inline ; } #menu a { display: block ; } CSS does not affect the markup: Setting the display property to block for a span element doesn’t allow you to nest an h1 element inside it, because the HTML document type definition forbids that.
68.
Browser Work 1)Parsing: The browser reads the markup and builds a document object model (DOM) tree of nodes.
Solutions: 1. Leavewith the default value: width=auto (doesn't work with a floated element) 2. Use a fixed width: width=500px 3. Apply the margin, padding, or border to a nested element
when the verticalmargins of two elements are touching, only the margin of the element with the largest margin value will be honored
84.
If one elementhas a negative margin, the margin values are added together
85.
If both arenegative, the greater negative value is used.
86.
CSS Box Example: h1 { margin: 0 0 25px 0; } p { margin: 20px 0 0 0; } <h1> Heading Content </h1> <p> Paragraph content </p>
87.
CSS Box Toavoid collapsing, use a border or padding: h1 { margin: 0; } div { margin: 40px 0 0 0; border: 1px solid #000; } p { margin: 20px 0 0 0; } <h1> Heading Content </h1> <div> <p> Paragraph content </p> </div>
88.
Positioning The CSS position property takes the values: static The default position in the page flow relative Relative to its normal position in the flow absolute Relative to its containing block (the first ancestor that is not static ) fixed Relative to the browser window (regardless of scrolling) To change the position of a box, use the top, left, bottom, right properties (no effect with static )
IE Conditional CommentsConditional comments are Microsoft’s recommended mechanism for delivering targeted CSS to Internet Explorer <!--[if IE 7]> <link href="ie7.css" type="text/css"> <![endif]--> <!--[if !IE 6]> <p> Other than IE 6 </p> <![endif]-->
101.
IE Conditional Comments<!--[if gte IE 6]> <p> Greater than or equal </p> <![endif]--> <!--[if (IE 6)|(IE 7)]> <p> IE 6 or IE 7 </p> <![endif]-->
Star Selector HackExample: .test {position: fixed;} * html .test {position: absolute;} Only IE 6 and earlier versions will apply the latter rule; other browsers will ignore it
107.
Backslash Hack Browsersshould ignore a backslash character in a property name: .test { height: 500px; he\ight: 400px; } IE 5.5 and earlier versions will ignore the whole declaration!
108.
Underscore Hack Browsersshould ignore the declaration of a property that starts with an underscore because it becomes an unknown property: .test { position: fixed; _position: absolute; } IE 6 and earlier versions will ignore the underscore and apply the declaration!