1 / 84

CHAPTER 2

CHAPTER 2. CSS (Cascading Style Sheet). Topics. Introduction Inline Styles Embedded Style Sheets Conflicting Styles Linking External Style Sheets Positioning Elements Backgrounds Element Dimensions Text Flow and the Box Model User Style Sheets. Learning Outcomes.

dai
Download Presentation

CHAPTER 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 2 CSS (Cascading Style Sheet)

  2. Topics • Introduction • Inline Styles • Embedded Style Sheets • Conflicting Styles • Linking External Style Sheets • Positioning Elements • Backgrounds • Element Dimensions • Text Flow and the Box Model • User Style Sheets

  3. Learning Outcomes At the end of this lesson, students should be able to: • Control the appearance of a Web site by creating style sheets. • Use a style sheet to give all the pages of a Web site the same look and feel. • Use the class attribute to apply styles. • Specify the precise font, size, color and other properties of displayed text. • Specify element backgrounds and colors. • Understand the box model and how to control the margins, borders and padding. • Use style sheets to separate presentation from content.

  4. Introduction • Cascading Style Sheets (CSS) • Separation of structure from presentation • new way of formatting your page layout, text, fonts, images • allow you to position things on your page down to the exact pixel • style sheetstypes: • Inline style sheet • Embedded or Internal style sheet • External style sheet

  5. Inline Styles • An inline style loses many of the advantages of style sheets by mixing content with presentation. • Therefore, you should use this method sparingly, such as when a style is to be applied to a single occurrence of an element • Declare an individual element’s format • Attribute style • CSS property • Followed by a colon and a value

  6. inline.html(1 of 2)

  7. inline.html(2 of 2)

  8. Embedded/Internal Style Sheets • An internal style sheet should be used when a single document has a unique style. • Embed an entire CSS document in an HTML document’s head section • Property background-color • Specifies the background color • font-family(line 18)-specifies the name of font to use. h1 { font-family:arial, sans-serif }

  9. Embedded/Internal Style Sheets • Font arial will be used. If the arial font is not found on the system, the browser instead will display a generic sans-serif font. • Other generic font families: • Serif (e.g:Times New Roman, Georgia) • Sans-Serif (e.g: Helvetica, Verdana) • Cursive ( e.g: script) • Fantasy (e.g: critter) • Monospace (e.g: Courier, Fixedsys) • font-size(line 20)-specifies the size of font.

  10. Embedded/Internal Style Sheets • Other possible measurements in addition to pt(point)- will be introduced later. • Relative values- xx-small, x-small, small, smaller, medium, large, larger, x-large and xx-large.

  11. Embedded/Internal Style Sheets • Relative values are preferred because an author does not know the specific measurements of the display for each client. • Relative font-size values permit more flexible viewing of web pages. • If relative font-size is specified, the actual size is determined by the browser that displays the font. • E.g: Handheld device with a small screen.

  12. Embedded/Internal Style Sheets Dreamweaver environment

  13. declared.html(1 of 3)

  14. declared.html(2 of 3)

  15. declared.html(3 of 3)

  16. Embedded/Internal Style Sheets Dreamweaver environment

  17. Selector forms Simple Selector Forms • The simplest selector form is a single element name, such as h1. • Consider the following examples: h1 {font-size: 20pt;} h2, h3 {font-size: 20pt;} form em {font-size: 20pt;}

  18. Selector forms Class Selectors • Class selectors are used to allow different occurrences of the same tag to use different style specifications. • Consider the following examples:

  19. Selector forms p.normal {font-size: 20pt;} p.warning {font-size: 6pt;} <p class= “normal”> …………………………… </p> <p class= “warning”> …………………………… </p>

  20. Selector forms Generic Selectors • A class of style specifications that applies to the content of more than one kind of tag.

  21. Selector forms .sale {font-size: 20pt;} <p class= “sale”> …………………………… </p> <h2 class= “sale”> …………………………… </p>

  22. Conflicting Styles • Inheritance HTML <p class = "special"> The company's clients include many <em>Fortune 1000 companies</em>, government agencies………..</p> CSS em { background-color:#8000ff; color:white } p { font-size:14pt } .special { color:blue } • Child em element inherited the font-size property from its parent p element. • However, the child em element had a color property that conflicted with the color property of its parent p element.

  23. Conflicting Styles • Descendant’s properties have greater specificity than ancestor’s properties. • Conflicts are resolved in favor of properties with a higher specificity. • em will be white in color instead of blue.

  24. Conflicting Styles <html> <head> <style type="text/css"> .navtext {color: black; font-weight: bold} p {color: blue} </style> </head> <body> <p class="navtext" style="color: red">Link 1</p> <p class="navtext">Link 2</p> <p>Link 3</p> </body></html>

  25. advance.html(1 of 3)

  26. advance.html(2 of 3)

  27. advance.html(3 of 3)

  28. Linking External Style Sheets • External style sheets • External style sheets are the best method when you want the style to be applied to many pages. • With an external style sheet, you can change the look of an entire web site by changing one external style sheet file. • Text-decoration applies decorations to text within an element. • Possible values of Text-decoration – none, overline, line-through, underline, blink(not supported by IE)

  29. Linking External Style Sheets • hover (pseudoclass)(line 16-18)- activated dynamically when the user moves the mouse cursor over an element. • Pseudoclassses are separated by a colon from the name of the element to which they are applied.

  30. Linking External Style Sheets • Margin-left(line 23 & 26)- ul { margin-left:15px } • Left-hand margin of 15 pixels • Relative-length measurement- varies in size, based on screen resolution. Example: px, em, ex, percentage(%) • Absolute-length measurement- not vary in size based on the system. Example: in(inches), cm(centimeter), mm(milimeter), pt(points; 1pt= 1/72 in), pc(picas; 1pc= 12 pt)

  31. Linking External Style Sheets Dreamweaver environment

  32. styles.css(1 of 1)

  33. external.html(1 of 2)

  34. external.html(2 of 2)

  35. Properties & Property Values • There are a large number of CSS properties that you can use with HTML. • It is impossible for us to cover all the properties in this course. • At the end of this slide, there are a number of references provided for you to refer.

  36. List: Property & Property Values <h3> Some Common Single-Engine Aircraft </h3> <ul style = "list-style-type: square"> <li> Cessna Skyhawk </li> <li> Beechcraft Bonanza </li> <li> Piper Cherokee </li> </ul> <h3> Some Common Single-Engine Aircraft </h3> <ul> <li style = "list-style-type: disc"> Cessna Skyhawk </li> <li style = "list-style-type: square"> Beechcraft Bonanza </li> <li style = "list-style-type: circle"> Piper Cherokee </li> </ul>

  37. List: Property & Property Values • Other list-style-type values: Property value First four decimal1, 2, 3, 4 upper-alphaA, B, C, D lower-alphaa, b, c, d upper-romanI, II, III, IV lower-romani, ii, iii, iv

  38. Positioning Elements • Absolute positioning • Removes the elements from the normal flow of elements on the page, instead positioning it according to the distance from the top, left, right, or bottom margin of its containing block-level element (i.e: body, p) • z-index attribute • Layer overlapping elements properly

  39. Positioning Elements • Elements that have a higher z-index values are displayed in front of elements with lower z-index values. • If z-index is not specified or elements have same z-index value • Elements are placed from background to foreground in order they are encountered in the document. • Relative positioning • Elements are positioned relative to other elements

  40. positioning.html(1 of 1)

  41. Positioning Elements Dreamweaver environment

  42. Positioning Elements Dreamweaver environment

  43. span • One problem with the font properties is that they apply to whole elements, which are often too large • Solution: a new tag to define an element in the content of a larger element - <span> • The default meaning of <span> is to leave the content as it is

  44. span • Example: <p> Now is the <span> best time </span> ever! </p> • Use <span> to apply an inline style sheet to its content <p> Now is the <span style = "font-size: 40; font-family: Arial; color: red"> best time </span> ever! </p>

  45. Span & div • The <span> tag is similar to other HTML tags, they can be nested and they have id and class attributes • Another tag that is useful for style specifications: <div> • Used to create document sections (or divisions) for which style can be specified e.g.: A section of five paragraphs for which you want some particular style

  46. Span & div <html> <head> <style type="text/css"> .try {font-size:6pt}; .try1 {font-size:50pt}; </style> </head> <body> <div class="try"> <p> A </p> <p> A </p> <p> A </p> <p> A </p> <p> A </p> <p> A </p> </div> <p>Hello <span class="try1">Welcome </span>to Web Programming </p> </body> </html>

  47. positioning2.html(1 of 2)

  48. positioning2.html2 of 2 Output:

  49. Backgrounds • background-image • Specifies the image URL • Can also set background-color property in case the image is not found • background-position • Places the image on the page • Some of the values: top, bottom, center,left, right

More Related