Cascading Style Sheets - Part 01


Published on

Introduction to CSS

Published in: Design
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Cascading Style Sheets - Part 01

  1. Cascading Style Sheets Hatem Mahmoud [email_address]
  2. Part 1
  3. Introduction
  4. What is CSS?
  5. What is CSS? <ul><li>Every web page is composed of HTML (HyperText Mark-up Language) code that describes the content
  6. Example: </li></ul><p> An <strong> important </strong><font color=&quot;#FFFF00&quot;> paragraph </font> . </p> <ul><li>Displays: </li></ul>An important paragraph . <ul><li>Repetitive and hard to maintain </li></ul>
  7. What is CSS? Layers of a web page: <ul><ul><li>Content: Text, images, animation, video, etc.
  8. Presentation: How the content will appear to a human through a web browser, text reader, etc.
  9. Behavior: Real-time user interaction with the page: validation, sorting, drag-n-drop, etc. </li></ul></ul>
  10. What is CSS? <ul><li>CSS separates the presentation from the content </li></ul>
  11. Versions <ul><li>1996 - CSS1 became a W3C recommendation: fonts, alignment, margins, borders, backgrounds, floating, etc.
  12. 1998 – CSS2 with advanced features: table cell display, sheets could import others, targeted different output media, etc.
  13. Some parts of CSS2 were very difficult to implement, so the W3C decided to revise the specification </li></ul>
  14. Versions <ul><li>The name of the revised version was “Cascading Style Sheets, Level 2 Revision 1”
  15. References to CSS2 usually mean CSS2.1
  16. CSS2.1 is the latest and current revision of the CSS2 specification
  17. CSS3 specification is still in draft but some parts have been implemented by some browsers </li></ul>
  18. Linking CSS to HTML 1) Inline Styles: Using the style attribute which is supported by every HTML tag: <h1 style=&quot;color:red;&quot;> My Headline </h1> <ul><li>The simplest way but repetitive across multiple HTML elements </li></ul>
  19. Linking CSS to HTML 2) Embedded Styles: Using the style tag: <style type=&quot;text/css&quot;> 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> <ul><li>Repetitive across multiple pages </li></ul>
  20. Linking CSS to HTML 2) External Styles: Using separate files: mypage.html <head> <link type= &quot;text/css&quot; href= &quot;nice.css&quot; /> </head> nice.css h1{ font-family:Verdana } .warning{ color:red } #footer{ font-size:10px }
  21. Linking CSS to HTML <ul><li>Now, multiple HTML pages can share the same CSS file. </li></ul>
  22. Why CSS? <ul><li>Flexible, can be applied in several ways
  23. Easy to maintain
  24. Accessibility to different users with different devices.
  25. CSS caching = less bandwidth + fast loading </li></ul>
  26. CSS Syntax
  27. General Syntax <ul><li>Style definition: </li></ul>body{font-family:Verdana; font-size:9pt;}
  28. General Syntax <ul><li>Case insensitive
  29. Whitespace and line breaks have no semantic value
  30. Comments: </li></ul>/* This is a comment */
  31. Properties <ul><li>Font: </li></ul>div { color: black; } span { color: #00003D; font-size: 24px; font-family: Verdana, Arial; font-style: italic; font-weight: bold; text-decoration: underline; text-align:justify; ... }
  32. Properties <ul><li>Size units ( relative and absolute ): </li></ul>px = Pixels on the screen em = Current font size ex = Height of lowercase &quot;x&quot; 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 <ul><li>Backgrounds: </li></ul>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) Universal selector: * { margin: 0; padding: 0; } 2) Element type selector: span { font-family: Verdana } 3) Class selector: p.big { font-weight: bold; }
  35. Selectors 4) ID selector: #menu { font-size: 22pt; } // unique id 5) Attribute selector: input[type=&quot;submit&quot;] { color: blue; }
  36. Selectors <ul><li>CSS3 new attribute selectors: </li></ul>a[href ^ =&quot;http:&quot;] { ... } /* matches a elements whose href attribute value starts with &quot;http:&quot; */ img[src $ =&quot;.png&quot;] { ... } /* matches img elements whose src attribute value ends with &quot;.png&quot; */ div[id * =&quot;foo&quot;] { ... } /* matches div elements whose id attribute value contains &quot;foo&quot; */
  37. Selectors <ul><li>Grouping: </li></ul>div, p { font-family: Verdana } a img { border: none } ul li ol li { color: blue } #menu a, div li, .note { color: red }
  38. Selectors <ul><li>Child selector: </li></ul>ul>li { ... } <ul> <li> <ol> <li> Will not be matched. </li> </ol> </li> </ul>
  39. Selectors <ul><li>Adjacent sibling selector: </li></ul>- 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 <ul><li>General sibling selector: </li></ul>- sibling = has the same parent element - general = just following h2~p { ... }
  41. Selectors <ul><li>Example: h2~p { ... } </li></ul><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>
  42. Pseudo-classes (implicit) a:link { ... } //Normal a:visited { ... } //Visited a:hover { ... } //Mouse hovers { ... } a:active { ... } // Clicking textarea:focus { ... } li:first-child { ... } :lang(fr) { ... }
  43. Pseudo-classes (implicit) <ul><li>New in CSS3 </li></ul>:nth-child(N) :nth-last-child(N) :nth-of-type(N) :nth-last-of-type(N) :last-child :first-of-type :last-of-type :only-child :only-of-type :root :empty :target :enabled :disabled :checked :not(S)
  44. Pseudo-elements (virtual) <ul><li>In CSS1 </li></ul>p :first-letter { ... } p :first-line { ... }
  45. Pseudo-elements (virtual) <ul><li>New in CSS2 </li></ul>#breadcrumbs :before { content : &quot;You are here:&quot;; margin-right: 0.5em; } span.centimeters :after { content : &quot;cm&quot;; color: #cccccc; }
  46. Pseudo-elements (virtual) <ul><li>New in CSS3 </li></ul>:: selection { ... } //represents a part of the document that’s been highlighted by the user, including text in editable text fields
  47. The Cascade
  48. The Cascade <ul><li>Style declarations cascade down to elements from many origins.
  49. The cascade combines the importance, origin, specificity, and source order of the style declarations to determine which declaration should be applied to a given element. </li></ul>
  50. The Cascade
  51. The Cascade <ul><li>Importance: </li></ul>//Normal declaration p {font-size: 1em} //Important declaration p {font-size: 1em !important ;}
  52. The Cascade <ul><li>Importance and origins priorities (low to high): </li></ul>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 <ul><li>Specificity: </li></ul>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 <ul><li>Calculating specificity: </li></ul>1. Inline styles (highest specificity) 2. Count ID selectors 3. Count class selectors ( .test ), attribute selectors ( [type=&quot;submit&quot;] ), and pseudo-classes ( :hover ) 4. Count element type selectors ( div ) and pseudo-elements ( :first-letter )
  55. The Cascade <ul><li>The process of the cascade: </li></ul>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 <ul><li>The process by which properties are passed from parent to child elements without explicit definition </li></ul>div { font-size: 20px; } <div> <p> My <em> cool </em> paragraph is <a href=&quot;#&quot;> here </a>. </p> </div>
  57. Inheritance <ul><li>Exceptions ( borders, backgrounds, etc.)
  58. But you can enforce it: </li></ul>p { background-image: inherit ; }
  59. CSS Layout
  60. Block vs Inline 1) HTML block-level elements: <ul><li>May contain inline elements and other blocks
  61. Begin on new lines
  62. Examples: <h1>..<h6> , <p> , <ul> , <ol> , <li> , <table> , <tr> , <th> , <td> , <form> , <select> , <input> , <div> , etc. </li></ul>
  63. Block vs Inline 2) HTML Inline (text-level) elements: <ul><li>Must be nested within blocks
  64. May contain only text and other inline elements
  65. Don't begin on new lines
  66. Examples: <em> , <strong> , <a> , <img> , <abbr> , <span> , etc. </li></ul>
  67. Block vs Inline <ul><li>Using the display property: </li></ul>#menu li { display: inline ; } #menu a { display: block ; } <ul><li>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. </li></ul>
  68. Browser Work 1) Parsing: The browser reads the markup and builds a document object model (DOM) tree of nodes.
  69. Browser Work <ul><li>DOM Tree: </li></ul>
  70. Browser Work 2) Rendering: <ul><li>Each node in the DOM tree is rendered as zero or more boxes
  71. Inline elements generate inline boxes
  72. Block elements generate block boxes </li></ul>
  73. CSS Box <ul><li>Box Model (for block-level elements): </li></ul>
  74. CSS Box <ul><li>Internet Explorer box model bug: </li></ul>
  75. CSS Box <ul><li>Dimensions: </li></ul><ul><li>width = 352px
  76. height = 252px </li></ul>
  77. CSS Box <ul><li>Take care: width=100%
  78. Any margin, padding, or border will damage the layout
  79. Solutions: </li></ul>1. Leave with 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
  80. CSS Box <ul><li>Borders: </li></ul>div { border-style: solid; border-width: 1px; border-color: black; } div { border: dashed 1px black; } div { border-left: dotted 1px black; }
  81. CSS Box <ul><li>Margins and paddings: </li></ul>div { margin: 10px } div { padding-right: 10px } div { margin: 10px 5px 0 3px } //top right bottom left div { padding: 10px 5px 3px } //top right-left bottom div { margin: 10px 3px } //top-bottom right-left
  82. CSS Box <ul><li>Collapsing margins :
  83. when the vertical margins of two elements are touching, only the margin of the element with the largest margin value will be honored
  84. If one element has a negative margin, the margin values are added together
  85. If both are negative, the greater negative value is used. </li></ul>
  86. CSS Box <ul><li>Example : </li></ul>h1 { margin: 0 0 25px 0; } p { margin: 20px 0 0 0; } <h1> Heading Content </h1> <p> Paragraph content </p>
  87. CSS Box <ul><li>To avoid collapsing, use a border or padding: </li></ul>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 <ul><li>The CSS position property takes the values: </li></ul>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) <ul><li>To change the position of a box, use the top, left, bottom, right properties (no effect with static ) </li></ul>
  89. Positioning <ul><li>Boxes are positioned in three dimensions
  90. To change the stack level, use z-index (default value is 0 ) </li></ul>{ position: absolute ; left:50px; top:0; z-index: 1 }
  91. Floating <ul><li>A floated element is one whose float property has the value right or left (the default is none )
  92. Floating an element converts it to a block
  93. A floated box is shifted to the left or right as far as possible
  94. A floated box must have an explicit width. Otherwise, the results can be unpredictable </li></ul>
  95. Floating <ul><li>Elements below a floated element will wrap around it: </li></ul>
  96. Floating <ul><li>To avoid this, you can apply the clear property to the following element using: clear:left , clear:right or clear:both </li></ul>p2{ clear: left ; }
  97. Interaction <ul><li>If display:none , no box is generated, so float and position are ignored
  98. If position:absolute or position:fixed , float is ignored </li></ul>
  99. Workarounds and Hacks
  100. IE Conditional Comments <ul><li>Conditional comments are Microsoft’s recommended mechanism for delivering targeted CSS to Internet Explorer </li></ul><!--[if IE 7]> <link href=&quot;ie7.css&quot; type=&quot;text/css&quot;> <![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]-->
  102. Star Selector Hack <ul><li>Using the * html selector
  103. Should apply the rule to any html element that’s the descendant of any other element
  104. As html is the root element, it’s never a descendant of any other element
  105. IE 6 and earlier versions don't understand this! </li></ul>
  106. Star Selector Hack <ul><li>Example: </li></ul>.test {position: fixed;} * html .test {position: absolute;} <ul><li>Only IE 6 and earlier versions will apply the latter rule; other browsers will ignore it </li></ul>
  107. Backslash Hack <ul><li>Browsers should ignore a backslash character in a property name: </li></ul>.test { height: 500px; height: 400px; } <ul><li>IE 5.5 and earlier versions will ignore the whole declaration! </li></ul>
  108. Underscore Hack <ul><li>Browsers should ignore the declaration of a property that starts with an underscore because it becomes an unknown property: </li></ul>.test { position: fixed; _position: absolute; } <ul><li>IE 6 and earlier versions will ignore the underscore and apply the declaration! </li></ul>
  109. Tools
  110. Tools <ul><li>Adobe Dreamweaver </li></ul>
  111. Tools <ul><li>Firebug (Firefox extension) </li></ul>
  112. References
  113. References <ul><li>
  116. </li></ul>
  117. Thank You! Hatem Mahmoud [email_address]