How style sheets work Web Designing

The key to working with style sheets is understanding how to define rules and then attach those rules to one or more HTML documents.

Rule Syntax
Style sheets consist of one or more rules for describing how a page element should be displayed. The following sample contains two rules. The firstmakes all the H1s in a document red; the second specifies that paragraphsshould be set in 12 pixel high Verdana or some sans-serif font:

Figure shows the components of a style sheet rule.

Figure: Parts of a style sheet rule

Parts of a style sheet rule

The two main sections are the selector (which identifies the element to be affected) and the declaration (the style or display instructions to be applied to that element). In the sample code above, H1 and P are the selectors.

The declaration, enclosed in curly brackets, is made up of a property and its value . Properties are separated from their values by the colon (:) character followed by a space. A property is a stylistic parameter that can be defined, such as color, font-family, or line height.

A declaration may contain several property/value pairs. Multiple properties must be separated by semicolons (;). Technically, the last property in a string does not require a semicolon, but developers usually include it anyway to make it easy to append the rule later. In addition, the inclusion of the trailing semicolon avoids a rare bug in older browsers.

Values are dependent on the property. Some properties take lengthmeasurements, some take color names, and others have a predefined list of accepted values. The syntax for length measurement and color values are discussed later in this chapter.

Adding Styles to an HTML Document
Rules (and sets of rules) can be applied to HTML documents in three ways: as inline style directions, as style elements embedded at the top of the HTML file, and as external files that can be either linked to or imported into the document.

Inline styles
Style information can be added to an individual element by adding the style attribute within the HTML tag for that element. The value of the styleattribute is one or more standard style declarations, as shown here:

Although a perfectly valid use of style information, inline styles areequivalent to the <font> tag in that they "pollute" the document withpresentation information. Style information is still tied to each individualcontent element, and any changes need to be made in every tag, in every file, rather than globally. Inline styles are best used only occasionally to override higher-level rules.

Embedded style sheets
A more compact method for adding style sheets is to embed a style block in the top of the HTML document using the <style> element, summarized here:

<style> NN 4, 6 MSIE 3, 4, 5, 5.5, 6 HTML 4.01 WebTV Opera5<style>...</style>

Allows authors to embed style sheet rules in the head of the document.There may be any number of <style> elements in a document.

Attributes

media=screen|tty|tv|projection|handheld|print|braille|aural|all

Specifies the target medium to which the style sheet applies.

type=content-type
Required. Specifies the style sheet language of the element's contents. The only viable type at this time is text/css.

title=text
Provides a title for the element.

The following example shows our sample rules embedded in a simple HTML document:

<HTML><HEAD><STYLE TYPE="text/css"><! --H1 {color: red}P{font-size: 12pt;font-family: Verdana, sans-serif;}-- > </STYLE><TITLE>Style Sheets</TITLE></HEAD>...</HTML>

The <style> element must be placed within the <head> tags in thedocument. In addition, it is usually necessary to place HTML comment tags (<! -- and -- >) around the <style> contents. This hides the style information from browsers that don't understand the <style> tag. (Otherwise, theymight display the rules as text in the browser window.)

Currently, Cascading Style Sheets is the only widely supported style sheet language, but the W3C has prepared for the possibility of additionallanguages to be added in the future by providing the type attribute within the <style> element. The only viable style type as of this writing is text/css. If the type attribute is omitted, some browsers may ignore the entire style sheet.

External style sheets
The most powerful way to use styles is to collect them all in a separate text document and create links to that document from all the HTML pages in a site. In this way, you can make stylistic changes consistently across a whole site by editing the style information in a single document. This is a powerful tool for large-scale sites.

The style sheet document is a simple text document that contains a collection of style sheet rules. It may not contain HTML tags (after all, it isn't an HTML document). It also may not include HTML comments.

There are two ways to refer to external style sheets (which should be named with the .css suffix) from within an HTML document:

Linking
The most standard and best-supported method is to create a link to that document using the <link> tag in the <head> of the document, as shown here:

<HEAD><LINK REL="STYLESHEET" HREF="/pathname/stylesheet.css"TYPE="text/css"></HEAD>

The rel attribute defines the linked document's relation to the current document – a "style sheet." The href attribute provides the URL to the file containing the style sheet information. Authors can link to more than one style sheet in a document.

Importing
An alternative to linking is to import external style sheets into the <style> element using the @import function:

@import commands must come before any style rules.

As in linking, importing allows multiple style sheets to be applied to the same document. When additional @import functions are added within the <style> element, the style information from the last file read (the one at the bottom of the list) takes precedence over the previous ones. The drawback to @import is limited browser support (it is currently supported only by Internet Explorer 4.0+ and Netscape 6).

Inheritance
An important feature of style sheets is the concept of inheritance, in which style properties are passed down from an element (the parent) to any element contained within it (the child). An element is said to inherit properties applied to elements higher in the HTML hierarchy. In CSS most properties can be inherited, but some (such as margins) cannot -- the CSS specifications later in the chapter point out if the property inherits or not.

For example, if you set the text color for a <ul> list, then this color will be inherited by every list item (<li>) within that list. If you specify that the <body> of a document should be red, all text elements contained in the body of the document will be red (unless specified otherwise).

Styles applied to specific elements override settings higher in the hierarchy. With planning, inheritance can be used to make style specification more efficient. For example, if you'd like all the text on the page to be blue except for list items, you can set the color property at the <body> level to apply to the whole document and then use another rule to make <li>s a different color. The more specific rules override more general rules.

If two rules of equal weight are listed in a style sheet, whichever one is later in the style sheet will apply.

Conflicting Style Sheets: The Cascade
Style sheets are said to be cascading because many different style sheet rules, coming from many different possible style sheets (inline, embedded, or external) can simultaneously affect the presentation of a single document. For example, it is possible to add inline styles to a document that is already linked to an external style sheet: the final look will result from all these style components cascading together.

With several styles applied to a document, conflicts are certain to arise. For example, when an inline style says the paragraph should be maroon, but the external style sheet says all paragraphs are blue, which style gets used?

The W3C anticipated this situation and devised a hierarchical system that assigns different weights to each type of style information. This cascadeorder provides a set of rules for resolving conflicts between competing style sheets. Styles with more weight (those defined at the more specific level) take precedence over styles set in a higher-level style sheet.

As in inheritance, more specific rules override more general rules. This allows you to design a general style for a whole site, then modify it for particular pages or elements, alleviating redundancy.

The following list shows the hierarchy of style instructions from general to specific, such that elements lower in the list have more weight and override styles above them.

  • Browser default settings.
  • User style settings (set in browser as a "reader style sheet").
  • Linked external style sheets. A document may have many linked style sheets. When multiple style sheets are linked to a document, the commands from the last file read take precedence over the first ones listed.
  • Imported style sheets. When multiple styles are imported, the commands from the last file read take precedence over the first ones listed.
  • Embedded style sheets (rules within the <style> element). Later rules have greater weight than earlier rules.
  • Inline style information.

If you want a rule never to be overridden by a subsequent rule, include the !important indicator just after the property value and before the semicolon for that rule. For example, to always set all paragraph text to blue, use the following rule in a style sheet for the document:

p {color: blue !important;}

Even if the browser encounters an inline style later in the document (which should override a document-wide style sheet), such as the following, that paragraph will still be blue because the rule with the !important indicator can not be overridden.

<p style="color: red">


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Web Designing Topics