Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSS: Cascading Style Sheets Part II. Style Syntax.

Similar presentations


Presentation on theme: "CSS: Cascading Style Sheets Part II. Style Syntax."— Presentation transcript:

1 CSS: Cascading Style Sheets Part II

2 Style Syntax

3 3 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; } Declaration Property Value(s) Selector

4 4 Style Rules (cont) A rule has the following form selector {declaration block} The selector determines when the rule is applied For example, the following rule applies to text that is inside a tag p {color: green; font-size: 1.5em; font-style: italic}

5 5 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 -Everything is in the Web ! -A good source: http://www.w3schools.com/csshttp://www.w3schools.com/css

6 Style Properties

7 7 Our Examples We use the following HTML example: This is our example for css. The tag is used to group elements for formatting with styles -Extremely useful tag...

8 8 Font Properties Font properties: family, size, weight, style, variant,... span { font-family: courier; font-size: 130%; font-style: italic; font-weight: bold}

9 9 Text Properties Text properties: color, transform, decoration, … span { color: #00cc00; text-decoration: line-through; text-transform: uppercase}

10 10 Background Properties Background properties: background-color, background-image, … span {background-color: #00ff00} span {background-image: url('bg.gif');}

11 Layout Properties

12 12 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

13 13 Inline vs. Block Elements There are two types of elements: Block elements: p, ol, table, div, h1, etc. Inline elements: b, i, a, span, cite, 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 and vice-versa, using the display property, e.g., h1 { display: inline }

14 14 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;}

15 15 Position Types But 1cm left to what?? For that, we have the position property Four position types: - static : the default position - absolute : relative to the parent layer coordinates - relative : relative to the static position - fixed : relative to the window coordinates

16 16 Position Examples span { position:absolute; left: 1cm; top: 1cm; color: #00cc00;} span { position:fixed; left: 1cm; top: 1cm; color: #00cc00;}

17 17 Position Examples span { position:static; left: 1cm; top: 1cm; color: #00cc00;} Totally Ignored! This is the default position type

18 18 More Layout Properties Layer properties - margin-top (-bottom, -left, -right) - padding-top (-bottom, -left, -right) - border-width (-color, -style, … ) Text Layout - direction, word-spacing, white-space, letter- spacing, line-height, text-align, text-indent, …

19 19 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

20 Selector Types

21 21 Several Kinds of Selectors Type Selectors Class Selectors ID Selectors Attribute Selectors Universal Selector Child Selectors Adjacent-Sibling Selectors Descendant Selectors Pseudo-Class Selectors Pseudo-Element Selectors Not supported by IE 5, 5.5 and 6

22 22 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: An item Another item

23 23 Class Selector A class selector is a selector of the form x.y It matches x s that have the class attribute with value y LI.reditem {color: red} Matches: An item Another item.reditem { color: red} will also work!

24 24 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} Matches: An item Another item #23 { color: red} will also work!

25 25 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 ”

26 26 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 }

27 27 Descendant Selector A descendant selector has the form S 1 S 2 where S 1 and S 2 are selectors It matches all elements that -match S 2, and - are descendants (nested in) elements that match S 1

28 28 An Example.href em {color: blue} Matches: This is not blue. This is blue and so is this. What will this match? p em {color: blue}

29 29 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

30 30 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}

31 31 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 either H1, P B, or H2[class=largehead] P B, H1, H2.largehead {font-size: 120%}

32 32 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: - Some text : does not stop the text flow - Some text : separated from the other text

33 Cascading and Inheritance

34 34 Cascading of Styles CSS merges style rules from different places (inline, document-level, linked and defaults) Different places may have conflicting style rules -conflicts may even arise in a single source The process of merging (cascading) styles from different places determines which style rules have higher priority

35 35 Determining Property Values Suppose that we would like to determine the value of property p for element e Choose all declarations that have a selector that matches e and have the property p Choose the first declaration in the cascading order, and apply the corresponding rule

36 36 Cascading Order The cascading order of declarations: 1.Primary sort: weight and origin 2.Secondary sort: class 3.Third sort: specificity of selectors 4.Final sort: order of appearance

37 37 Weight and Origin A style defined “closer” to a tag takes precedence: Author style sheets override user style sheets User style sheets override default (browser) style sheets !important declaration overrides normal declaration If both author and user declarations have !important, the author declarations still override the user declarations

38 38 Example (sort by class) Consider the two rules -P {…} -.introductory {…} If an element matches both, then the second rule has a higher specificity and overrides the first

39 39 An Example (specificity of selectors) Consider the following rules: - li {…} - #x34y {…} - ul ol li.red {…} Which is the most specific?

40 40 Order of Appearance If two rules have the same origin importance and specificity, then the one that appears last in the style sheet overrides the others Rules in imported style sheets (@import) are considered to appear before local rules

41 41 Inheritance of Properties If an element does not match any rule for some given property, then the element inherits the computed value for that property from its parent element For example, color, font and word-spacing are inherited Yet border, margin and padding are not!

42 42 An Example (1) Given the rules: - body { font-size: 10pt } - h1 { font-size: 120% } What will be the font size of the element? A large heading Computed Value: 12pt

43 43 An Example (2) Given the rules: - body { font-size: 10pt; color=red; } - h1 { font-size: 120% } What will be the font color of the element? A red heading ?

44 44 Inheritance of Properties If some properties have been set for the same selector in different style sheets, the values will be inherited from the more specific style sheet. Example: Result (properties for h3 in the document): color :red text-align : right font_size:20 pt external: H3 { color : red; text-align : left; font-size : 8 pt; } internal: H3 { text-align : right; font-size : 20 pt; }

45 45 The End! These slides are based on those developed for the course: http://www.cs.huji.ac.il/~dbi http://www.cs.huji.ac.il/~dbi More information about CSS can be found at: http://www.w3schools.com/css/default.asp http://www.htmlhelp.com/reference/css/ http://www.htmlgoodies.com/beyond/css/


Download ppt "CSS: Cascading Style Sheets Part II. Style Syntax."

Similar presentations


Ads by Google