Cascading Style Sheets

Sep 15
2009

Cascading Style Sheets (CSS) is a style sheet language used to describe the presentation semantics (the look and formatting) of a document written in a markup language. Its most common application is to style web pages written in HTML and XHTML, but the language can also be applied to any kind of XML document, including plain XML, SVG and XUL.

CSS is designed primarily to enable the separation of document content (written in HTML or a similar markup language) from document presentation, including elements such as the layout, colors, and fonts. This separation can improve content accessibility, provide more flexibility and control in the specification of presentation characteristics, enable multiple pages to share formatting, and reduce complexity and repetition in the structural content (such as by allowing for tableless web design). CSS can also allow the same markup page to be presented in different styles for different rendering methods, such as on-screen, in print, by voice (when read out by a speech-based browser or screen reader) and on Braille-based, tactile devices. While the author of a document typically links that document to a CSS style sheet, readers can use a different style sheet, perhaps one on their own computer, to override the one the author has specified.

CSS specifies a priority scheme to determine which style rules apply if more than one rule matches against a particular element. In this so-called cascade, priorities or weights are calculated and assigned to rules, so that the results are predictable.

CSS has a simple syntax and uses a number of English keywords to specify the names of various style properties.

A style sheet consists of a list of rules. Each rule or rule-set consists of one or more selectors and a declaration block. A declaration-block consists of a list of declarations in braces. Each declaration itself consists of a property, a colon (:), a value. If there are multiple declarations in a block, a semi-colon (;) must be inserted to separate each declaration.

In CSS, selectors are used to declare which of the markup elements a style applies to, a kind of match expression. Selectors may apply to all elements of a specific type, or only those elements that match a certain attribute; elements may be matched depending on how they are placed relative to each other in the markup code, or on how they are nested within the Document Object Model.

Pseudo-classes are another form of specification used in CSS to identify markup elements, and in some cases, specific user actions to which a particular declaration block applies. An often-used example is the :hover pseudo-class that applies a style only when the user ‘points to’ the visible element, usually by holding the mouse cursor over it. It is appended to a selector as in a:hover or #elementid:hover. Other pseudo-classes and pseudo-elements are, for example, :first-line, :visited or :before. A special pseudo-class is :lang(c), “c”.[clarification needed]

A pseudo-class selects entire elements, such as :link or :visited, whereas a pseudo-element makes a selection that may consist of partial elements, such as :first-line or :first-letter.

Selectors may be combined in other ways too, especially in CSS 2.1, to achieve greater specificity and flexibility.

Prior to CSS, nearly all of the presentational attributes of HTML documents were contained within the HTML markup; all font colors, background styles, element alignments, borders and sizes had to be explicitly described, often repeatedly, within the HTML. CSS allows authors to move much of that information to a separate style sheet resulting in considerably simpler HTML markup.

Headings (h1 elements), sub-headings (h2), sub-sub-headings (h3), etc., are defined structurally using HTML. In print and on the screen, choice of font, size, color and emphasis for these elements is presentational.

Prior to CSS, document authors who wanted to assign such typographic characteristics to, say, all h2 headings had to use the HTML font and other presentational elements for each occurrence of that heading type. The additional presentational markup in the HTML made documents more complex, and generally more difficult to maintain. In CSS, presentation is separated from structure. In print, CSS can define color, font, text alignment, size, borders, spacing, layout and many other typographic characteristics. It can do so independently for on-screen and printed views. CSS also defines non-visual styles such as the speed and emphasis with which text is read out by aural text readers. The W3C now considers the advantages of CSS for defining all aspects of the presentation of HTML pages to be superior to other methods. It has therefore deprecated the use of all the original presentational HTML markup.

How Do I Create My Own Website?

Sep 08
2009

Creating your own website can be useful for a number of purposes. Whether it’s to share your expertise on a particular topic, start an online business, foster a community, or just maintain an online journal of your activities, having a website will allow you to make your content accessible from any connected computer. It can also theoretically allow you to have a larger audience for your ideas than you would ever have otherwise. If you have useful ideas or observations to share, you may be able to share them with as many as a few dozen people throughout the day, if you are lucky. With a website, you can share them with hundreds or even thousands.

Possibly the easiest way to create your own website is to use an online site creation tool. These services often provide intuitive interfaces for adding text, images, links, and other bits of content to a webpage. Many of these tools have a WYSIWYG web design platform, which means “What You See is What You Get” — what you create in the design interface is what your visitors will see.

If you want more options and functionality on your website, as well as your own dedicated web address, you’ll need to create pages in HTML, register a web domain, and get a hosting server to upload your pages to. Web domains can be registered at any number of registrars — Godaddy.com is a popular one. Search for “hosting” or “cheap hosting” to find many thousands of available hosting companies. As a general rule of thumb, if you’re paying more than about $10 US dollars (USD) per month for hosting for a low-traffic website, you’re paying way too much. Many companies offer space for even less.

To create HTML, you can write it from scratch in notepad, or use a WYSIWYG software program. HTML is not a programming language per se, and as such is much easier to use than true programming languages. You can learn basic HTML in less than an hour and start using it to create your very own web pages.

After you create your pages, they must be uploaded to your server. You will receive a username and password from your host once you register with them. You can use these in an application called an FTP program to connect your computer with the server, then send your completed pages from computer to server. Once uploaded, your website will be visible on the World Wide Web for all to enjoy. Within a few days, pages with inbound links will be indexed by major search engines and begin to appear in search results.

Anatomy of Styles

Sep 12
2006

Here we go again with the inconsistencies and exceptions. The last rule in any declaration need not end in a semicolon, and some designers leave out the final semicolon in a series of declarations. (That’s because, as in the English language, the semicolon functions as a separator, not a terminator.)

But most experienced CSS authors add the semicolon to the end of every declaration anyway. They do this partly for consistency’s sake, but mainly to avoid headaches when adding and subtracting declarations to and from existing rules. If every property/value pair ends in a semicolon, you don’t have to worry when you move declarations from one place to another.

Whitespace and Case Insensitivity

Most style sheets naturally contain more than one rule, and most rules contain more than one declaration. Multiple declarations are easier to keep track of and style sheets are easier to edit when we use whitespace:

body  {
     color: #000;
     background: #fff;
     margin: 0;
     padding: 0;
     font-family: Georgia, Palatino, serif;
   }
 
p     {
font-size: small;
   }

 

Whitespace, or its absence, has no effect on the way CSS displays in browsers, and unlike XHTML, CSS is not case-sensitive. Naturally, there is an exception: class and id names are case-sensitive when they’re associated with an HTML document. myText and mytext are not matches in such a situation. CSS itself is case-insensitive, but the document language might not be.

Alternative and Generic Values

A web designer might specify fonts for an entire site as follows:

body  {
      font-family: "Lucida Grande", Verdana, Lucida, Arial,
      Helvetica, sans-serif;
   }

 

Notice that multi-name fonts (“Lucida Grande”) must be enclosed within straight ASCII quotation marks, and that the comma follows rather than precedes the closing quotation mark, to the annoyance of literate American designers accustomed to placing the comma inside the quotation marks.

Fonts are used in the order listed. If the user’s computer contains the font Lucida Grande, text will be displayed in that face. If not, Verdana will be used. If Verdana is missing, Lucida will be used. And so on. Why these fonts in this order?

Using Order to Accommodate Multiple Platforms

Order matters. Lucida Grande is found in Mac OS X. Verdana is found in all modern Windows systems, in Mac OS X, and in older (“Classic”) Mac operating systems. If Verdana were listed first, OS X Macs would display Verdana instead of Lucida Grande.

With the first two fontsLucida Grande and Verdanathe designer has met the needs of nearly all users (Windows and Macintosh users). Lucida follows for UNIX folks, and then Arial for users of old Windows systems. Helvetica will be used in old UNIX systems. If none of the listed fonts is available, the generic sans-serif assures that whatever sans-serif font is available will be pressed into service. In the unlikely event that the user’s computer contains no sans-serif fonts, the browser’s default font will be used instead. If the browser has no default font, the teeth of a small chimp will be used. Just kidding with that last part.

Not a Perfect Science

No one pretends that Lucida, Verdana, Arial, and Helvetica are equivalent in their beauty, elegance, x-height, or fitness for screen service (or even that all pressings of Helvetica are equally good). Our goal is not to create identical visual experiences for all users; platform, browser, monitor size, monitor resolution, monitor quality, gamma, and OS anti-aliasing settings make that quite impossible. We simply try to ensure that all visitors have the best experience their conditions permit, and that those experiences are fairly similar from one user to the next.

Grouped Selectors

When several elements share stylistic properties, you can apply one declaration to multiple selectors by ganging them together in a comma-delimited list:

p, td, ul, ol, ul, li, dl, dt, dd {
     font-size: small;
   }

 

This ability comes in handy when you’re coping with old browsers that don’t understand CSS inheritance.

Link Effects, Lists, and Navigation

Jun 17
2006

CSS has been used to style links for as long as CSS has been available. In fact, along with text styles, link styles are by far the most common use of CSS to date. Of course, as we learn more about CSS and how to use it as a means of managing most, if not all, of our visual presentation, that truth becomes somewhat diluted. At this point, though, styling links with CSS remains one of the most widespread applications of the technology.

With CSS, you can create beautiful effects for links, including hover effects that, before CSS, had to be managed with JavaScript and numerous images or with a Java applet. Using color and images, you can reproduce the effects that demanded so many resources with much greater efficiency and control, and no reliance on anything but clean markup and savvy styling, resulting in beautiful designs that degrade well in older browsers and are completely accessible to those with disabilities.

Lists help you organize your content, and with CSS, you can do a great deal with lists. Along with styling the way list text is managed, you can use CSS to change the way the numbers or bullets are displayed, replace them with an image, or remove the list markers completely.

A very exciting opportunity comes about when you merge lists and links. You end up with a means of navigation. For, after all, what is navigation other than a list of links? Whether managed vertically in a side column or horizontally along the top of the page, using CSS, lists and links can be manipulated to create very richly featured navigation with a minimum of fuss.

In this chapter, you begin by creating simple styles for links and then progress to multiple link styles. You’ll then learn to manage lists with CSS. Finally, you’ll work with lists and links for both vertical and horizontal navigation. Along the way, you’ll be introduced to new CSS properties, as well as a number of selector types you’ve not worked with just yet.

Styling Text

Jun 17
2006

The basic principles for how text is styled in CSS come from traditional typography, although limitations based on both CSS and browser support have prevented certain growth in the area of web-based type.

A warning from the start, though: Working with fonts can be complicated. First, you must understand that, in terms of available fonts for text-based type on the Web, there’s dependence upon a person’s operating system and installed font base. If a specific font is not installed on someone’s machine, that person will not be able to view the font. The same limitation existed for presentational HTML, too, and not much has changed since then. Then there is a major browser flaw in font sizing that makes using font sizes a real pain. Over these concerns you are essentially powerless, except in what you can learn in terms of how to manage them. So bear with me through the details, okay?

The good news is that you do have some choice over fonts, and CSS expands the way you use the fonts by being able to add color, weight, height, spacingall kinds of features that add visual interest to the fonts you do choose. As you become more adept with CSS in general, you’ll learn to combine graphics and text-based type to come up with increasingly sophisticated type designs.

Within the CSS specification, properties for managing typography are found in two primary places. The first is in font-related properties, which are all specific to the way a font is chosen and then displayed. The second is text-related properties, in which text is further managed, usually without some manipulation to the actual font. Some font features come from the more general visual category of styles and can be used with (but are not limited to) text.

Visit Our Friends!

A few highly recommended friends...

Pages List

General info about this blog...