Using Cascading Style Sheets (CSS) Technology
Total Page:16
File Type:pdf, Size:1020Kb
2Lesson 2: Using Cascading Style Sheets (CSS) Technology Objectives By the end of this lesson, you will be able to: 2.1: Explain the function and purpose of the Cascading Style Sheets (CSS) technology. 2.2: Explain the importance of using style guides when designing sites and documents. 2.3: Define common CSS terms and syntax. 2.4: Apply CSS styles to format pages. 2.5: Develop and apply page layouts with CSS. 2.6: Use CSS to position content and apply document flow. 2.7: Define common CSS positioning schemes. 2.8: Use the CSS Box Model. 2-2 Advanced HTML5 and CSS3 Specialist Pre-Assessment Questions 1. In CSS, what is the name for a selector, property and value all grouped together? a. Declaration b. Tag c. Structure d. Rule 2. In a CSS style sheet, which of the following determines the image behind the contents of a page? a. The background-image property b. The background attribute of the <body> tag c. The background-image selector in the <body> tag d. The img property 3. What is liquid layout? © 2014 Certification Partners, LLC. — All Rights Reserved. Version 1.0 Lesson 2: Using Cascading Style Sheets (CSS) Technology 2-3 Cascading Style Sheets (CSS) OBJECTIVE As a Web designer, you know that there is more to creating a good Web page than posting 2.1: CSS technology good written content and images. Most users have multiple sites to choose from when they are searching the Web for something, whether it is information, entertainment or products. If several reputable sites offer the same content and are easy to use, then which one will users prefer? Quite often, it will be the one that looks the best. Cascading Style Cascading Style Sheets (CSS) is a Web design technology that enables developers to Sheets (CSS) A W3C language for determine how the HTML elements in their Web pages will display, giving a page or entire applying structure site a particular "look and feel." and formatting instructions to HTML CSS instructions can be: pages and other markup language documents. • Applied inline to individual HTML elements on an HTML page. • Embedded within an HTML page to affect all specified elements on that page. • Collected in an external file that is linked to multiple HTML pages. ® CIW Online Resources – Movie Clips Visit CIW Online at http://education.Certification-Partners.com/CIW to watch a movie clip about this topic. Lesson 2: Using Cascading Style Sheets (CSS) Technology External style sheets An external CSS file, or style sheet, is a text file that contains CSS formatting instructions to define the font, color and position of elements used on any markup page(s) to which you attach the style sheet. NOTE: CSS benefits include: Have you experienced any • Consistency — CSS easily gives an entire site a consistent look and feel. benefits from using CSS in your past • projects? How has Easy change management — You have the ability to change the look and feel of an CSS helped you? Do entire site by simply changing one part of a single line of code, rather than having to you think it is change possibly thousands of lines in hundreds of Web pages. absolutely necessary? Are If all pages on your site are linked to the same external style sheet, then one simple there situations when it is not change to the style sheet will change all associated elements across the site. For example, necessary? if you want to change your <h1> heading styles, you do not need to change every page manually. Simply change one line in a style sheet file attached to all your pages, and then all your <h1> headings will change their appearance to conform to the style sheet. This technology can save a great deal of development and maintenance time, as well as provide a more consistent, accessible interface for your Web site. Style Guides OBJECTIVE A style guide or style manual is a set of standards for the writing and/or design of 2.2: Style guides documents, either for general use or for a specific publication, organization or field. Implementing a style guide provides uniformity in style and formatting within a document and across multiple documents. © 2014 Certification Partners, LLC. — All Rights Reserved. Version 1.0 2-4 Advanced HTML5 and CSS3 Specialist Style guides are common for general and specialized use, especially for those who write, edit and/or design documents in the following fields: • Various academic disciplines (both students and scholars) • Journalism • Technology • Medicine • Law • Government • Business and industry Some organizations follow a CSS style guide to define the CSS formatting to be used for all Web documents across the organization. It defines styles such as the page layout, font sizes, colors, and image placement for Web sites. Following a CSS style guide creates consistency in appearance for an organization's communications. Many companies create their own CSS Style Guides and some even share them with other organizations (Figure 2-1). Google and WordPress make them available to the public: • Google HTML/CSS Style Guide (http://google-styleguide.googlecode.com/svn/trunk/htmlcssguide.xml) • WordPress CSS Coding Standards (http://make.wordpress.org/core/handbook/coding-standards/css/) Figure 2-1: Google CSS style guide Following are some examples of guidelines and tips from the Google HTML/CSS Style Guide: • Be consistent — When editing existing code, for example, match the written code style (spacing, comment use, etc.). If code that you add to a file looks drastically different from the existing code around it, other users of the code can become distracted or even confused. The purpose of style guidelines is to provide a common vocabulary for coding. Global style rules are helpful in general, but local style (dictated by your organization) is also important. © 2014 Certification Partners, LLC. — All Rights Reserved. Version 1.0 Lesson 2: Using Cascading Style Sheets (CSS) Technology 2-5 • Capitalization — Use only lowercase letters for your code, including HTML element names, attributes, attribute values, CSS selectors, properties and property values. • Declaration order — In a style sheet, alphabetize your declarations to create consistent code that is easy to remember, search and maintain. For example: background: fuchsia; border: 1px solid; border-radius: 4px; color: black; text-align: center; text-indent: 2em; CSS and HTML CSS was created to solve a problem with HTML. The developers of HTML intended it to identify only the content and structure of a document, such as <title> and <body>, not the style, look and feel of the content. Before style sheets, the HTML standard introduced many style elements that quickly complicated the language. Developers had to check each page of a Web site to ensure it had the correct HTML style tags for each element. Imagine the time and difficultly involved with this task. To save developers from many headaches, the W3C quickly went into action and standardized CSS, which greatly simplifies the definition and use of styles. Today a style, such as a background color or image, can be added to all Web pages with one change to an external CSS file. CSS versions Currently, three standards exist for style sheets: • Cascading Style Sheets (CSS1) — governs the basic structure of style sheets. • Cascading Style Sheets 2 (CSS2) — adds more capabilities to the CSS1 specification, including the ability to support media types (such as specific printers) and work with tables. NOTE: • Cascading Style Sheets 3 (CSS3) — provides a modularized standard so that when Remember that changes must be made to a style, only a particular module within CSS3 will need to HTML5, CSS3 and JavaScript are be updated, rather than the entire standard. This will allow for a more flexible and considered the timely upgrade of the CSS standard as a whole. New functions are being added to future of Web development as CSS3 to enhance its support of borders, backgrounds, colors, text effects and so mobile devices forth. continue to proliferate. HTML5 adopts CSS as the preferred way to format a page. The CSS standard has evolved over the years and continues to build upon itself. For instance, HTML 4.01 and XHTML 1.0 used CSS1 and CSS2. HTML5 uses CSS1, CSS2 and CSS3. Any given Web page may include CSS rules from all three versions. HTML5, CSS and legacy browsers Because CSS3 contains the latest instructions, most non-HTML5-compliant browsers cannot interpret it. Most of the basic Web page formatting uses CSS1 and CSS2. CSS3 provides advanced features to extend a Web page’s functionality beyond basic formatting. HTML5 is not supported by Internet Explorer 8 or earlier. This is a challenge because Windows XP cannot run IE9, which is the first Internet Explorer version that supports © 2014 Certification Partners, LLC. — All Rights Reserved. Version 1.0 2-6 Advanced HTML5 and CSS3 Specialist HTML5. The simple solution is to install the latest version of the Chrome or Firefox browser on Windows XP or Vista systems. But what about your site visitors? You cannot force them to install Chrome or Firefox. They may be using IE8, 7 or 6. The only way to support these older IE browsers is to add JavaScript code to your HTML documents and a new rule to CSS. Because older IE browsers will not recognize the new HTML5 elements (such as <header>, <footer>, <article>, etc.), you must use JavaScript to create these elements in your Web pages so that older IE browsers will recognize them. This requires several steps, as follows. NOTE: You can try coding HTML5 document additions for older IE browsers a page to support At a minimum, you should create the basic HTML5 structural elements by adding the legacy browsers in following JavaScript in the <head> element: Optional Lab 2-1: Supporting legacy browsers.