Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cascading Style Sheets (CSS). Cascading Style Sheets With the explosive growth of the World Wide Web, designers and programmers quickly explored and reached.

Similar presentations


Presentation on theme: "Cascading Style Sheets (CSS). Cascading Style Sheets With the explosive growth of the World Wide Web, designers and programmers quickly explored and reached."— Presentation transcript:

1 Cascading Style Sheets (CSS)

2 Cascading Style Sheets With the explosive growth of the World Wide Web, designers and programmers quickly explored and reached the creative limits of HTML. More control over appearance of web documents was expected by frustrated graphic artists, who were used to the wider range of control that they had with traditional media. Since style sheets were a familiar tool to printers and print designers, they were a natural way to add more control and functionality to HTML.

3 Cascading Style Sheets (continued) A style sheet is a way of specifying details about the appearance of a document. We can specify such things as font colour, size, and face, like we can with the tag. Style sheets are one of the technologies that make up the concept known as DHTML.

4 Cascading Style Sheets (continued) With CSS we can control a large number of different kinds of properties, including font properties, text properties, colour properties, and margin and border properties. Important to DHTML, we can use also style sheets to control the positioning of elements. Many of the details of style sheets are for the use of graphic designers. The features of style sheets that we will focus on as web programmers involve setting common tag properties, and positioning.

5 Cascading Style Sheets (continued) Style sheets allow us to associate a style (collection of properties and their values) with most of the HTML tags. This means that we can specify the h1 tag, and define a style that makes all h1 tags a certain colour, and size, etc. Then we just have to use the tag normally, and it automatically has the defined properties.

6 Cascading Style Sheets (continued) Style sheets give us a range of capabilities. We can choose the level of effect, from a separate file that can be applied to all the pages of a site, down to a style that only applies to a particular word in a particular paragraph.

7 What kind of properties can we control? Some common properties that we can control with style sheets are: Font (size, weight, family) Text (decoration, capitalization) Background colour and text colour Borders and Margins (size, style) Positioning (relative and absolute)

8 CSS Rules CSS is a collection of rules. These rules can be found in one of three places: 1.Inline, inside a particular HTML tag. 2.Embedded, in the head of a page. 3.External, in a separate file.

9 CSS Rules (continued) Embedded and external rules have a specific, easy-to- learn syntax: selector { property : value; } 1.The selector is the tag or name that the rule applies to. 2.The property is the name of the property we want to set. 3.The value is one of the possible settings allowed for that property.

10 CSS Rules (continued) In the following example, h3 is the selector, color is the property, and #FF0000 is the value. h3 { color : #FF0000; } Note: h3 refers to the tag, but the brackets are never used when specifying styles. Note: quotes are never used when specifying embedded or external styles. Note: properties are case-sensitive, and all use lower case.

11 Embedded CSS: example 1

12 1 2 3 4 CSS test 5 h3 { color : #FF0000; } 6 7 8 9 This heading is red. 10 But this one is normal. 11 12

13 Embedded CSS: example 1 An embedded CSS goes in the head of the document, and uses the tag. The type attribute is always set to text/css. Any styles (rules) defined here apply to the whole HTML page. The preceding code will cause all tags on the page to use red type.

14 Embedded CSS: example 2

15 1 2 3 4 css example 2 5 6h3 { color : #FF0000; background-color : #FFFF00; } 7 8 9 10 This title is red, with a yellow background. 11 But this title is normal. 12 13

16 Embedded CSS: example 2 You can define as many properties as you want for the same tag, separating them with semi-colons. The preceding code causes all tags on the page to use red type, on a yellow background.

17 Embedded CSS: example 3

18 1 2 3 4 css example 3 5 6h2, h3 { color : #FF0000; background-color : #FFFF00; } 7 8 9 10 This title is red, with a yellow background. 11 Now this title is red, with a yellow background too. 12 13

19 Embedded CSS: example 3 You can define the same properties for as many tags (selectors) as you want. The preceding code causes all and tags on the page to use red type, on a yellow background.

20 Embedded CSS: example 4

21 1 2 3 4 css example 4 5 6h3 i { color : #00FF00; background-color : #0000FF; } 7 8 9 10This text is normal. 11 This text is different. 12 This text is normal too. 13 14

22 Embedded CSS: example 4 You can define a context for a style. This means that a style is only applied under given circumstances. The preceding code causes all tags to use green type on a blue background, but only when inside an tag. Otherwise they work normally.

23 Defining a class In addition to using tags as selectors, we can create a style (collection of properties and their values) and give it an arbitrary name that we choose. We can then use this name, called a class, anywhere we want on the page, including inside most tags. The following code creates a class called fancy, which can then be used to modify all tags it is used in. We use the class attribute of the tag to reference the desired style. We must precede the class name with a period when we define it, but not when we use it.

24 Defining a class 1 2 3 4 css example 5 5 6.fancy { color : #FF0000; background-color : #FFFF00; text-transform : capitalize; font-weight : bold; font-family : arial, courier; } 7 8 9 10 test with normal settings 11 test with fancy settings 12 13

25 Defining a class

26 In the following example, note that we can use the defined class in most tags, and we can use it as many different times as we want.

27 Defining a class 1 2 3 4 css example 6 5 6.fancy { color : #FF0000; background-color : #FFFF00; text- transform : capitalize; font-weight : bold; 7font-family : arial, courier; } 8 9 10 11 test with normal settings 12 test with fancy settings 13 test with fancy settings 14 test with normal settings 15 16 test with fancy settings 17 18 19

28 Defining a class

29 We can also use a class that we have defined in any particular place we want, using the and tags. The tag has no properties of its own. The tag leaves a line break above and below it, like the tag does.

30 Defining a class 1 2 3 4 css example 8 5 6.style1 { color : #FF0000; font-weight : bold; font-family : arial, courier; } 7.style2 { color : #00FF00; font-style : italic; font-family : courier, arial; } 8 9 10 11This text is normal, but this text changes 12and now it's different. 13This text is normal, but this text changes differently and now it's normal. 14 15

31 Defining a class

32 Defining an inline style We can also specify a style for just one particular use, in one particular tag. This is called an inline style, and uses the style attribute of the tag. The syntax is slightly different. We use the style attribute, and add a quote- enclosed list of property:value pairs, separated by semi-colons.

33 Defining an inline style 1 2 3 4 css example 9 5 6 7 This paragraph is normal. 8 This paragraph is different. 9 10

34 Defining an inline style

35 Defining an external style If we take the same style description that we would normally put in the head of a document, we can also create a separate file to contain our style description. The advantage of this is that we can then refer to it on all of our page, and have a site-wide consistent style. Any changes can be made once, and automatically apply to the whole site.

36 Defining an external style We just have to create a plain text file in notepad. The content should be just the same as you would put in an embedded style in the head of a document, without the and tags. By convention, we should store the file with a.css extension, but this is not mandatory. We use the tag in the head of the document to tell the browser which style sheet to use.

37 Defining an external style 1// css10.html 2 3 4 css example 10 5 6 7 8 This heading is different. 9 This paragraph is different. 10 11 12 13.style1 { color : #FF0000; font-weight : bold; font-family : arial, courier; } 14h2 { text-transform : capitalize; }

38 Defining an external style

39 Pitfalls CSS implementation by the browsers varies quite dramatically, and cross-browser testing of all CSS features that you use is essential. For example; Further experimentation shows that Netscape 4.7 does not implement most of the CSS style properties within tables, whether the style specified is inline or embedded.

40 Pitfalls 1 2 3 4 css example 7 5 6.fancy { color : #FF0000; background-color : #FFFF00; text-transform : capitalize; font-weight : bold; 7font-family : arial, courier; } 8 9 10 11 12 13 cell 1 14 cell 2 15 16… rest of code …

41 Pitfalls

42 Priority rules Cascading style sheets get their name because the order of priorities for the different styles “cascades” from one level to the next. When more than one rule applies, the rule sets will be mixed. If two rules govern the same property, a more specific rule will take precedence over a more general one, as the following example shows.

43 Priority rules 1 2 3 4 css example 12 5 6 7h3 { font-weight : bold; font-size : 14px; font-family : arial, courier; color : #FF0000; } 8 9 10 11 This is heading #1 12 This is heading #2 13 This is heading #3 14 This is heading #4 15… rest of code…

44 Priority rules 1// stylesheet2.css 2h2, h3, h4 { font-weight : bold; font-size : 60px; }

45 Priority rules

46 A look at the results of the preceding example show that heading #1 receives all its properties from the external style sheet. The rest of the headings are a mix of properties. If they are specified at a lower level, those are used. If not, higher level properties are used. The order, from highest level to lowest priority is 1.Inline 2.Embedded 3.External

47 Priority rules The order of priority can be overridden by using the !important modifier, which causes that property to be used, no matter what its level is. (currently only works on IE) In this example, the font-weight specified in this external style sheet will always be used, even if font-weight is also specified in an embedded or inline style for any of the heading sizes listed; h2, h3, h4 { font-weight : bold !important; font-size : 60px; }

48 The fun stuff One of the additions to CSS is the ability to state exactly where on a page content is placed. We also have the ability to change this placement while viewing a page, which is one of the aspects of Dynamic HTML.

49 Defining an ID In addition to defining a custom style (class) using dot notation e.g..highlight { background-color : #FFFF00; } we can also define a custom style called an ID, with the following notation; #warning { color : #FF0000; font-weight : bold; }

50 Defining an ID The difference is that an ID (like a person’s ID) is unique. When we define an ID, it is intended for one-time use on a page, whereas the class is intended to be used many times on a page or group of pages. To use an ID, we use the id attribute of a tag, instead of the class attribute.


Download ppt "Cascading Style Sheets (CSS). Cascading Style Sheets With the explosive growth of the World Wide Web, designers and programmers quickly explored and reached."

Similar presentations


Ads by Google