Title: Chapter 6 Cascading Style Sheets CSS
1Chapter 6 - Cascading Style Sheets (CSS)
Outline 6.1 Introduction 6.2 Inline
Styles 6.3 Embedded Style Sheets 6.4
Conflicting Styles 6.5 Linking External Style
Sheets 6.6 W3C CSS Validation Service 6.7
Positioning Elements 6.8 Backgrounds 6.9
Element Dimensions 6.10 Text Flow and the Box
Model
2Objectives
- In this lesson, you will learn
- To control the appearance of a Web site by
creating style sheets. - To use a style sheet to give all the pages of a
Web site the same look and feel. - To use the class attribute to apply styles.
- To specify the precise font, size, color and
other properties of displayed text. - To specify element backgrounds and colors.
- To understand the box model and how to control
the margins, borders and padding. - To use style sheets to separate presentation from
content.
36.1Â Â Introduction
- Cascading Style Sheets (CSS)
- Separation of structure from presentation
46.2Â Â Inline Styles
- Declare an individual elements format
- Attribute style
- CSS property
- Followed by a colon and a value
- lttag style"propertyvalue propertyvalue
..."gt - tag can be
- p, div, span, a, table, tr, td, etc.
- property can be
- background-color value can be colorname or
hexvalue - font-family value can be sans-serif, cursive,
fantasy, etc - font-size value can be number in pixels or
percentage n
5inline.html(1 of 2)
6inline.html(2 of 2)
7(No Transcript)
86.3Â Â Embedded Style Sheets
- Embed an entire CSS document in an XHTML
documents head section - ltstyle type"text/css"gt  selector
propertyvalue propertyvalue ...  ...
lt/stylegt - selector is the tag name to which style
declarations (properties and values) are assigned
9(No Transcript)
10declared.html(1 of 3)
11declared.html(2 of 3)
12declared.html(3 of 3)
136.4Â Â Conflicting Styles
- Inheritance
- Descendants properties have greater specificity
than ancestors properties
14CSS Syntax
- From (go to the link and read in details)
- http//www.w3schools.com/css/css_syntax.asp
- General syntax discussed earlier
- selector property value
- The selector is normally the HTML element/tag you
wish to define - The property is the attribute you wish to change
15Grouping
- You can group selectors.
- Separate each selector with a comma.
- In the example below we have grouped all the
header elements. All header elements will be
displayed in green text color - h1,h2,h3,h4,h5,h6
-
- color green
-
16The Class Selector
- With the class selector you can define different
styles for the same type of HTML element. - p.right text-align right
- p.center text-align center
- You have to use the class attribute in your HTML
document - ltp class"right"gt
- This paragraph will be right-aligned.
- lt/pgt
- ltp class"center"gt
- This paragraph will be center-aligned.
- lt/pgt
17The Class Selector
- You can also omit the tag name in the selector to
define a style that will be used by all HTML
elements that have a certain class . - .center text-align center
- In the code below both the h1 element and the p
element will follow the rules in the ".center"
selector  - lth1 class"center"gt
- This heading will be center-aligned
- lt/h1gt
- ltp class"center"gt
- This paragraph will also be center-aligned.
- lt/pgt
18Anchor Pseudo-classes
- A link that is active, visited, unvisited, or
when you mouse over a link can all be displayed
in different ways in a CSS-supporting browser - alink color FF0000 / unvisited link /
avisited color 00FF00 / visited link /
ahover color FF00FF / mouse over link /
aactive color 0000FF / selected link / - Note ahover MUST come after alink and
avisited in the CSS definition in order to be
effective!! - Note aactive MUST come after ahover in the CSS
definition in order to be effective!!
19CSS Pseudo-elements
- Make the first letter special
- Make the first line special
- css_firstline_letter.html
20The id Selector
- You can also define styles for HTML elements with
the id selector. The id selector is defined as a
. - The style rule below will match the element that
has an id attribute with a value of "green" - green color green
- The style rule below will match the p element
that has an id with a value of "para1" - ppara1
-
- text-align center
- color red
21CSS Classification
- The CSS classification properties allow you to
specify how and where to display an element. - CSS classification properties
- clear
- Sets the sides of an element where other floating
elements are not allowed - cursor
- Specifies the type of cursor to be displayed
- display
- Sets how/if an element is displayed
- float
- Sets where an image or a text will appear in
another element - position
- Places an element in a static, relative, absolute
or fixed position - visibility
- Sets if an element should be visible or invisible
22CSS Classification Examples
- cursor
- class_cursor.html
- display
- css_display.html
- visibility
- visibility_collapse.html
23(No Transcript)
24advance.html(1 of 3)
25advance.html(2 of 3)
26advance.html(3 of 3)
276.5Â Â Linking External Style Sheets
- External style sheets
- Can provide uniform look and feel to entire site
28styles.css(1 of 1)
29external.html(1 of 2)
30external.html(2 of 2)
316.6Â Â W3C CSS Validation Service
- Validates external CSS documents
- Ensures that style sheets are syntactically
correct
326.6Â Â W3C CSS Validation Service
Fig. 6.6 Validating a CSS document. (Courtesy of
World Wide Web Consortium (W3C).)
336.6Â Â W3C CSS Validation Service
Fig. 6.7 CSS validation results. (Courtesy of
World Wide Web Consortium (W3C).)
346.7Â Â Positioning Elements
- Absolute positioning
- z-index attribute
- Layer overlapping elements properly
- Relative positioning
- Elements are positioned relative to other elements
35(No Transcript)
36positioning.html(1 of 1)
37(No Transcript)
38positioning2.html(1 of 2)
39positioning2.html2 of 2
40(No Transcript)
416.8Â Â Backgrounds
- background-image
- Specifies the image URL
- background-position
- Places the image on the page
- background-repeat
- Controls the tiling of the background image
- background-attachment
- fixed
- scroll
- font-weight
- Specify the boldness of text
42(No Transcript)
43background.html(1 of 2)
44background.html(2 of 2)
45(No Transcript)
466.9Â Â Element Dimensions
- CSS rules can specify the actual dimensions of
each page element
47width.html(1 of 2)
48width.html(2 of 2)
496.10Â Â Text Flow and the Box Model
- Floating
- Move an element to one side of the screen
- Box model
- Margins
- margin-top, margin-right, margin-left,
margin-bottom - Padding
- padding-top, padding-right, padding-left, and
padding-bottom - Border
- border-width
- thin, medium, thick
- border-color
- Sets the color
- border-style
- none, hidden, dotted, dashed, solid, double,
groove, ridge, inset and outset
50(No Transcript)
51(No Transcript)
52floating.html(1 of 3)
53floating.html(2 of 3)
54floating.html(3 of 3)
556.10Â Â Text Flow and the Box Model
Fig. 6.13 Box model for block-level elements.
56borders.html(1 of 2)
57borders.html(2 of 2)
58(No Transcript)
59borders2.html(1 of 2)
60borders2.html(2 of 2)
61(No Transcript)
62(No Transcript)
63(No Transcript)