Cascading Style Sheets (CSS)

Similar documents
<body bgcolor=" " fgcolor=" " link=" " vlink=" " alink=" "> These body attributes have now been deprecated, and should not be used in XHTML.

2. Write style rules for how you d like certain elements to look.

Using CSS in Web Site Design

CSS مفاهیم ساختار و اصول استفاده و به کارگیری

CSC 121 Computers and Scientific Thinking

Title: Jan 29 11:03 AM (1 of 23) Note that I have now added color and some alignment to the middle and to the right on this example.

Using Dreamweaver CC. 6 Styles in Websites. Exercise 1 Linked Styles vs Embedded Styles

Assignments (4) Assessment as per Schedule (2)

Using Dreamweaver CS6

Lab 4 CSS CISC1600, Spring 2012

HTML Summary. All of the following are containers. Structure. Italics Bold. Line Break. Horizontal Rule. Non-break (hard) space.

recall: a Web page is a text document that contains additional formatting information in the HyperText Markup Language (HTML)

BIM222 Internet Programming

A Balanced Introduction to Computer Science, 3/E

CSS Cascading Style Sheets

Reading 2.2 Cascading Style Sheets

CSS: The Basics CISC 282 September 20, 2014

Introduction to Web Design CSS Reference

Using Dreamweaver. 6 Styles in Websites. 1. Linked or Imported Stylesheets. 2. Embedded Styles. 3. Inline Styles

Introduction to Web Design CSS Reference

CREATING A WEBSITE USING CSS. Mrs. Procopio CTEC6 MYP1

HTMLnotesS15.notebook. January 25, 2015

Session 4. Style Sheets (CSS) Reading & References. A reference containing tables of CSS properties

Creating and Building Websites

1 Creating a simple HTML page

Three Ways to Use CSS:

Lab Introduction to Cascading Style Sheets

CSS means Cascading Style Sheets. It is used to style HTML documents.

Which is why we ll now be learning how to write in CSS (or cascading sheet style)

In the early days of the Web, designers just had the original 91 HTML tags to work with.

CSS CSS how to display to solve a problem External Style Sheets CSS files CSS Syntax

KillTest *KIJGT 3WCNKV[ $GVVGT 5GTXKEG Q&A NZZV ]]] QORRZKYZ IUS =K ULLKX LXKK [VJGZK YKX\OIK LUX UTK _KGX

Dreamweaver CS6. Level 1. Topics Workspaces Basic HTML Basic CSS

How to create and edit a CSS rule

CSS worksheet. JMC 105 Drake University

Deccansoft Software Services

CS Multimedia and Communications REMEMBER TO BRING YOUR MEMORY STICK TO EVERY LAB!

Shane Gellerman 10/17/11 LIS488 Assignment 3

DAY 4. Coding External Style Sheets

Basic CSS Lecture 17

CISC1600-SummerII2012-Raphael-lec3 1

Cascading Style Sheets (CSS)

ADDING CSS TO YOUR HTML DOCUMENT. A FEW CSS VALUES (colour, size and the box model)

c122sep2214.notebook September 22, 2014

Class 3 Page 1. Using DW tools to learn CSS. Intro to Web Design using Dreamweaver (VBUS 010) Instructor: Robert Lee

Web Publishing Basics I

Teach Yourself HTML5 & CSS 3: Week 5 Task 13 - Anchors Part 3

HTML and CSS COURSE SYLLABUS

HTML & CSS. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Using Dreamweaver to Create Page Layouts

Introduction to Web Programming and Design

Azon Master Class. By Ryan Stevenson Guidebook #8 Site Construction 1/3

Table of Contents Chapter 9. Working with Cascading Style Sheets ... 1

COMP519 Web Programming Lecture 6: Cascading Style Sheets: Part 2 Handouts

Introduction to Computer Science Web Development

CMPT 165 Advanced XHTML & CSS Part 3

Study Guide 2 - HTML and CSS - Chap. 6,8,10,11,12 Name - Alexia Bernardo

**Method 3** By attaching a style sheet to your web page, and then placing all your styles in that new style sheet.

Appendix D CSS Properties and Values

Make a Website. A complex guide to building a website through continuing the fundamentals of HTML & CSS. Created by Michael Parekh 1

Introduction to Cascading Style Sheet (CSS)

1. Please, please, please look at the style sheets job aid that I sent to you some time ago in conjunction with this document.

What is CSS? NAME: INSERT OPENING GRAPHIC HERE:

Introduction to Cascading Style Sheets

Html basics Course Outline

Chapter 4 Creating Tables in a Web Site Using an External Style Sheet

Unveiling the Basics of CSS and how it relates to the DataFlex Web Framework

INTERNATIONAL UNIVERSITY OF JAPAN Public Management and Policy Analysis Program Graduate School of International Relations

APPLIED COMPUTING 1P01 Fluency with Technology

Wanted! Introduction. Step 1: Styling your poster. Activity Checklist. In this project, you ll learn how to make your own poster.

Web Recruitment Module Customisation

7300 Warden Avenue, Suite 503 Markham, Ontario Canada L3R 9Z6. Phone: Toll Free: Fax:

Creating Web Pages with SeaMonkey Composer

Using Dreamweaver To Edit the Campus Template Version MX

Unit 5 Web Publishing Systems Page 1 of 13 Part 4 HTML Part 4

Creating your first website Part 4: Formatting your page with CSS

IMY 110 Theme 6 Cascading Style Sheets

GIMP WEB 2.0 MENUS WEB 2.0 MENUS: HORIZONTAL NAVIGATION BAR CREATING AN HTML LIST

CMPT 165 INTRODUCTION TO THE INTERNET AND THE WORLD WIDE WEB

Introduction to Web Tech and Programming

INFORMATICA GENERALE 2014/2015 LINGUAGGI DI MARKUP CSS

Faculty of Engineering Department of Computer Engineering. CSS Tutorial. Eng. Ahmed J. Alajrami

Title and Modify Page Properties

Chapter 3 Style Sheets: CSS

HTML/XML. HTML Continued Introduction to CSS

Creating a CSS driven menu system Part 1

CITS1231 Web Technologies. Introduction to Cascading Style Sheets

Introduction to Multimedia. MMP100 Spring 2016 thiserichagan.com/mmp100

HTML Hints & Tips. HTML is short for HyperText Markup Language.

Creating a Website: Advanced Dreamweaver

HTML and CSS a further introduction

Objective % Select and utilize tools to design and develop websites.

CSS MOCK TEST CSS MOCK TEST III

Before you begin, make sure you have the images for these exercises saved in the location where you intend to create the Nuklear Family Website.

Setting a Background Image

Chapter 7 Typography, Style Sheets, and Color. Mrs. Johnson

Hypertext Markup Language, or HTML, is a markup

Controlling Appearance the Old Way

Beginning HTML. A tag is a command written between angle brackets (the less than and greater than symbols). Ex. <html>

Chapter 1 Self Test. LATIHAN BAB 1. tjetjeprb{at}gmail{dot}com. webdesign/favorites.html :// / / / that houses that information. structure?

Transcription:

Snyder4e_04 12/9/09 1:09 AM Page 111 Cascading Style Sheets (CSS) 111 Notice that the first row uses the <th> tag rather than the <td> tag to specify the column headings. Cascading Style Sheets (CSS) We have used the style attribute to make the Paradoxes Web page more attractive. Without much effort we improved the page a lot from its original design, but we aren t yet using the full power of the styling capabilities available to us. Cascading Style Sheets (CSS) is the facility that is responsible for much of what is very slick on the Web pages we see everyday as we surf. It is a general styling system for documents that greatly simplifies the task of creating complex page designs. In this section we see how to use CSS. Making Style Global In thinking about the Web page in Figure 4.6, we notice that the style information was repeated for both level 2 headings: <h2 style="color:darkorange; font-family:arial"> If there were twenty level 2 headings on the page, each of them would repeat this exact information. That wastes typing, of course, but that may only be a minor annoyance. Of greater concern is that if we decide later that we want to change the design maybe we really want orangered text in Trebuchet font we must change the specification in every one of the headings. Although this might be simply updated with a Find/Replace All, for any complex page, it means making each change manually. Setting global style. The solution to the problem of repeating style information wherever it is needed is to place it in one global location inside the <head> tags, so that it applies to the whole page. We do this by placing the style information inside a pair of <style> and </style> tags. Yes, style can be used both as a tag and as an attribute. For example, <style type="text/css"> h2 {color : darkorange; font-family : arial} </style> The <style> tag contains a type attribute specifying the form of the style information; this should always be used exactly as shown here. Within the <style> and </style> tags are specifications for each tag that should have its properties adjusted. The general syntax for these specifications is elem_name { prop_name1 : spec1 ;... ; prop_namen : specn} The text between the tag s angle brackets, known as the tag element, is given (h2 in the preceding example), and after that, inside curly braces ({}), is the list of property names/specification pairs, each separated by a semicolon. The meaning is that all occurrences of the tag will be styled as specified. In this

Snyder4e_04 12/9/09 1:09 AM Page 112 112 Chapter 4 A Hypertext Markup Language Primer way a document can be given a consistent look without having to repeat the styling information every time a style tag is used. As a further example, suppose we want to give our tables a standard form. The specification <style type="text/css"> table {outline-style : solid; outline-color : violet} th {background-color : purple; font-family : courier} td {background-color : fuchsia; font-family : arial; color : white; text-align : center} </style> does that. Several properties are specified for three table elements together when we write <tr> <th> col 1 </th> <th> col 2 </th> <th> col 3 </th></tr> <tr> <td> A </td> <td> B </td> <td> C </td></tr> <tr> <td> 1 </td> <td> 2 </td> <td> 3 </td></tr> and they produce this. The formatting is automatically applied. Of course, every table on the page will have this form. Notice that when the style information is given inside of <style> tags, as shown here, no quotations marks are used. In styling, closest wins. Global specification of style is extremely convenient and efficient, but what if there is an exception? For example, if all tables have the foregoing form, how do we make one of them have a violet cell and maybe change how the entries are aligned? Style the table data tags for that one table where it appears in the XHTML using the style attribute. So, we might write <tr> <td style="text-align : left"> 1 </td> <td style="background-color:violet"> 2 </td> <td style="text-align : right"> 3 </td></tr> which produces this. Notice what has happened: Only the background color in the one cell and the two alignments have changed. All of the other properties of the table the border color, the heading background, the background of the other cells, the font face, and alignment remain as given in the global specification in the <head>. This is exactly how we want styling information to work: We give the basic styling globally, and then if anything changes at a particular point on the Web page, we change it at that place, and we only say what is different from the default. Everything else applies as normal. Generally, the rule with styling information is this: The closest specification wins. In essence, the specification closer to the site blocks the specification(s) further out. It is a very smart and flexible way to organize a system.

Snyder4e_04 12/9/09 1:09 AM Page 113 Adding Class to Style Cascading Style Sheets (CSS) 113 The global specification of style associated with tags gives consistent formatting, but it only simplifies our Web design as long as the tag is used in only one way. For example, suppose we want different colors for tables. How can we add class to our page by using both the preceding spring pink table and this summer green table? Add class. If we want a tag, say the table tag, to be styled in several different ways, we say that we have several different classes of styling. A class is a family of styling specifications with a common name. The class is given in two places: In the style definition inside the style tags in the <head>, and at the site in the XHTML code of the <body> where we use a table of that class. For some reason, the two places work differently. For the style definition, we give the tag s class by appending the class name (here, sum) to the tag with a dot, as in <style type="text/css"> table.sum {outline-style : solid; outline-color : lime} th.sum {background-color : lightgreen; font-family : courier; color : darkgreen} td.sum {background-color : green; font-family : arial; color : white; text-align:center} </style> Then, throughout the Web page, the browser knows there is a table of the class sum that has a solid lightgreen border, and table heading and data tags of the same class to go with it. As another example, the earlier spring table in the class spr would use the form table.spr. In the <body>, when we need a table of class sum, we write <table class="sum">... </table> to use the class attribute to specify which class of style specifications will define the table. Thus, we can write <table class="sum"> <tr> <th class="sum">col 1</th><th class="sum">col 2</th> <th class="sum">col 3</th></tr> <tr> <td class="sum"> A </td> <td class="sum"> B </td> <td class="sum"> C </td> </tr> <tr> <td class="sum"> 1 </td> <td class="sum"> 2 </td> <td class="sum"> 3 </td> </tr> </table> to create a summer table different from the spring table. Class definitions are used extensively in CSS because most Web pages have many different parts that need separate styling, but the parts are built using the small set of standard XHTML tags like <table>. Obviously, style benefits greatly from class.

Snyder4e_04 12/9/09 1:09 AM Page 114 114 Chapter 4 A Hypertext Markup Language Primer One More Simplification We originally used the global style specifications of the <head> section as a way to avoid styling every use of a tag. Now, looking at the summer table specification, it seems like we re back to writing extra attributes inside the tag to get the style we want. True, it is only one attribute, class, but it quickly becomes a nuisance. Isn t there a better way? Yes. Because all of the table data cells are to be treated in the same way in the body of the table, it should be possible to define how we want them to be for the whole table. We could put them inside the table tag specification, but that confuses the table properties with the cell properties. However, there is a table body tag, <tbody>, that encloses the rows and data of a table. It can be used to style the whole table. Thus, we declare the fall class of the table at the beginning, <style> table.fall { outline-style : solid; outline-color : orangered} tbody.fall{ background-color : chocolate; font-family : arial; color : white; text-align:center} th.fall { background-color : darkorange; font-family : courier} </style> but this time we style the <tbody> tag rather than the <td> tag. This allows the fall table to be specified more simply as <table class="fall"> <tbody class="fall"> <tr><th class="fall">col 1</th><th class="fall">col 2 </th> <th class="fall">col 3</th></tr> <tr><td> A </td><td> B </td> <td> C </td></tr> <tr><td> 1 </td><td> 2 </td> <td> 3 </td></tr> </tbody> </table> We still style the heading tags to be customized for each season, and therefore have to specify the class name for them. This is less of a nuisance than specifying the class name for every cell in the table, because the heading occurs only once per column, but it is still more typing than we want. So, let s use the specify-the-styling-information-over-a-range idea like we used before for <tbody>. This time, notice that the headings are always in their own row, so we style a table row to handle headings. To do so, change th.fall {background-color : darkorange; font-family : courier} to tr.fall {background-color : darkorange; font-family : courier} in the global style specification. Then, at the table site in the XHTML, write <tr class="fall"><th>col 1</th>< >col 2</th><th>col 3</th></tr> and the result is as intended.

Snyder4e_04 12/9/09 1:09 AM Page 115 Style from Files 115 Notice that the <tbody> tag tells how to style all cells in the table, but when we get to the <tr> tag inside, it specifies how to style that one row. It takes precedence over the specification of the <tbody> for two properties. Again, we see that the closest style wins, but all other properties continue to apply. Style from Files The nice part about specifying the style information inside the <style> tags is that it is all in one place, so consistent changes can be made throughout the document very easily by modifying this one site. Reusing one s previous work is a common theme in computing, and we can apply it with CSS. All of the style information that would normally be placed inside the <head> tags can be moved to a file, and then the file name can be given with a <link> tag inside the <head>. In this way many Web pages can use the same styling information. This is how Web sites ensure that the whole site is styled in a consistent way: All pages use the same styling file(s). Packaging the Basic Style To illustrate this idea, suppose we have developed the American Writers Anecdotes (AWA) page in Figure 4.7, and plan to have a series of pages about different American writers all with the same heading and styling information. We have designed a page with compatible colors and customized the links of the author tabs. Before placing the style information in a file, let s see how the links were styled. Pseudo Classes Normally, the default is for links to be blue and underlined. To change this, we style the anchor tag, as shown in Figure 4.7 at the end of the style section in the <head>: a:link {color : darkviolet; text-decoration : none} a.me:link {color : gray; text-decoration : none} a:hover {color:red} These specifications are slightly different from the styling we have used for other tags, because the anchor tag has several different states that can be styled separately. These are referred to as pseudo classes and are separated from the element by a colon (:). The three main states are link, styling for an unvisited link (anchor text) hover, styling when the cursor hovers over a link (see Figure 4.7) visited, styling for links that have been visited In the style specification, the color is set, and text-decoration that s the underline is set to none. Also, notice that we have defined a class, me, that is used to gray out the link for the current page. Thus, the Thoreau page is classified as me and the link becomes gray. One more very strange fact: If hover is styled, its style must come after link.

Snyder4e_04 12/9/09 1:09 AM Page 116 116 Chapter 4 A Hypertext Markup Language Primer Figure 4.7 The American Writers Anecdotes page for Henry David Thoreau: the source and the result (inset). Moving Style to a File To use an external file for style information, place it into a file as a sequence of property/specification pairs. That is, place the specifications that are inside the <style> tags in the file, but don t include the <style> and </style> tags themselves. Like XHTML files, the style file should be created with a text editor and should be plain ASCII text. The file extension the letters that come after the last dot in the file name should be css. We follow these rules, moving the AWA page s style information into a file that we name AWAstyle.css. It contains the information between the <style> and </style> tags.

Snyder4e_04 12/9/09 1:09 AM Page 117 Style from Files 117 To incorporate the style information of the file into a page, use the <link> tag: <link rel="stylesheet" type="text/css" href="awastyle.css"> The attributes of this tag should, by now, be obvious: This link is related to the styling of the documents; the type, normally given in the <style> tag, is given here; and the file is named. After this link has been included in the program, the head section of AWA page is greatly reduced, as shown in Figure 4.8. Moving the style information out of the way certainly shortens this section of the XHTML; notice that the head section is down to three tags. Of course, it is possible to include more global style information in the head section along with styling information in a file. The global information inside the <head> will take precedence over information in the file. Cascading the Style Sheets We have seen that CSS uses the rule closest style wins. We have already seen several levels of style information, and that each can be shadowed by specifications closer to the site where the property applies. In fact, we can Figure 4.8 The AWA page for Steinbeck showing the <link> in the source code to the external style file AWAstyle.css and the result (inset).

Snyder4e_04 12/9/09 1:09 AM Page 118 118 Chapter 4 A Hypertext Markup Language Primer identify five levels of styling information. Starting from the farthest away, we have Default, given by browser settings External, given in a file Global, given in the <head> section Range, given in an enclosing tag Site, given by the style attribute Each level is broader and more general than the levels below it. Here s how we understand these levels. At the top, the browser knows how to style the entire document based on the settings that come as a default with the browser, as modified by the user. No further information is needed. These default settings can be selectively replaced by specifications in an external file, which can be replaced by still more specifications in the global style of the head section, which can be specified further by a range and, eventually, by the tag around the information being styled. All of the style information needed at any point in the XHTML code can be found by tracing back for the closest specification. It will be found, because the default contains a specification for every styling option. The rule: closest style wins. This idea of progressively becoming more site specific is the cascading idea of the Cascading Style Sheet. It allows general styling choices to be adopted at various levels, and to be overridden as the situation become better known. It is smart and powerful. And as we shall see, it illustrates many basic ideas of computing. HTML Wrap-Up In learning XHTML, you have seen how a Web page is encoded. Although XHTML has a few more exotic features beyond those presented here, and the other Web languages have even more powerful features, they are all variations on the same theme: Tags surround all objects that appear on the page, the context is set by specifying properties of the page s tags, and each feature of the format is specified in detail, <i>isn t it?!</i>. It s so easy, even a computer can do it! Indeed, that s what happens most of the time. Web authors usually don t write HTML directly; they use Web authoring tools, such as Adobe Dreamweaver. They build the page as it should look on the screen using a WYSIWYG Web authoring program, and then the computer generates the HTML to implement it. fluencybit Uploading. Pages are created and tested on a personal computer. To be accessed from other computers on the Internet, the HTML files, the image files, and the directory structure created for them must be uploaded (transmitted) to a Web server, a process known as publishing.