Showing posts with label tutorial. Show all posts
Showing posts with label tutorial. Show all posts

Saturday, October 23, 2010

Css Tutorial : letter-spacing (Kerning)

In the real (print) world, the spacing of characters has the professional name of kerning. Again,
CSS has enabled web designers to emulate this tight text control with the letter-spacing property.
Where line-height creates extra whitespace between lines of text, so letter-spacing is used to
adjust the spacing between characters. Again, normal can be declared to override inherited
letter-spacing, but mostly you will declare letter-spacing in pixels. In the following example,
letter-spacing is declared only for the grouped headings:
/* Specify blanket rules for all elements */
body {
margin: 10px;
border: 1px solid #000;
padding:10px
font: 12px Verdana, Arial, Sans-serif;
line-height:150%;
}
h1, h2 {
letter-spacing:3px;
}
■Note It is worth noting that unlike line-height, here negative values are allowed, so something like
letter-spacing:-0.5em can be used to bunch up the characters if required. Using the em measurement
will ensure that the spacing scales if text sizes are increased. This approach is unlikely to aid legibility, however,
so use negative spacing with caution. Use letter-spacing only when absolutely necessary. Increasing the
whitespace between characters rarely makes text more legible, and only serves to make life more difficult for
those with reading difficulties.Many designers like to ensure many other font properties are set first, and leave line-height
and letter-spacing until last. It can be that final touch that sets a design apart, and shows that
the designer has a good approach to legibility and type design.

Css Tutorial :Body Declarations

The first task with any new web site is to consider what blanket rules can be declared in the
body selector. Remember that every element contained in the body element will inherit its
values unless you specify otherwise. For example, to avoid having to declare the font-family
and font-size for every element, some blanket rules can be applied from the outset.
The first selector to define in text.css is for body. Notice that margin, border, and padding
properties have been declared, but more importantly so have the font-family and font-size properties using shorthand. These are specified for the whole web page, and any others that
take their style from text.css:
/* Specify blanket rules for all elements */
body {
margin: 10px;
border: 1px solid #000;
padding:10px;
font: 12px Verdana, Arial, Sans-serif;
}With the font-size reduced to 12px, the display looks a little more professional, and
Verdana makes the text a little easier on the eye. Notice that the declared font-size has no
effect on the headings (<h1> and <h2>), which retain their default font sizes, as discussed earlier.

Css Tutorial : Be Careful with Fonts

Remember that the alternative fonts suggested here need to be used with caution. Ensure
sensible alternatives are listed in the font-family sequence, and remember that a font displayed
on a Mac can look significantly different, or even be unavailable, on a PC. It is advisable to edit
your font sequences at the testing stage to see how your alternative fonts work for you. For
example, Georgia may be your second-choice font, but be sure it suits your design should the
first choice be unavailable.

Css Tutorial : Web-Safe Fonts Alternative

It’s time to act like the rebellious tutor who throws course books out of the window and encourages
the group to “push the envelope.” In truth though, many designers are simply pursuing unsung
alternatives that can sensibly be added to the accepted list of available web fonts. The gamble
here is lessened thanks to the ability to suggest backup fonts should the preferred one be
unavailable. Learning CSS can be challenging enough without being forced to work within
difficult confines, so I encourage you to flirt with some of the following fonts, be they classic
or well established, or newcomers that have shown their true worth on screen.
We’ll look at the merits of each of the fonts displayed in Figure 4-4, followed by suggested
family names and generic names to ensure your design isn’t compromised when that font is
unavailable. Lucida Grande and Lucida Sans Unicode
Lucida Grande comes preinstalled on Mac OS X, and Lucida Sans Unicode comes with
Windows XP, the latter being a very close match to the former. These fonts are a brilliant
Verdana alternative, being supremely legible and extremely refreshing to eyes tired of typical
web fonts. Note that this author has used Lucida Grande on probably 60% of all the sites he
has built!
Suggested declaration: "Lucida Grande", "Lucida Sans Unicode", Verdana, sans-serif

Futura
Futura is a good contemporary-looking font, comes preinstalled on Mac OS X, and is included
with many Adobe applications. It is a great sans serif font that works particularly well at larger
font sizes (so great for headers).
Suggested declaration: Futura, Helvetica, Arial, sans-serif

Helvetica Neue
A redrawn version of Helvetica that has better separation between characters across its various
guises, Helvetica Neue (pronounced “noye-er”) comes preinstalled with most platforms and
software. It’s a very stylish font that looks great in any situation.
Suggested declaration: "Helvetica Neue", Helvetica, Arial, sans-serif

Gill Sans
Gill Sans is classified as a humanist sans serif, making it very legible and readable in text and
display work. The condensed, bold, and display versions are excellent for packaging or posters,
and this description translates very well to the screen. Gill Sans exudes a modern feel due to its
clear, generous, and original characters. It comes preinstalled on Mac OS X.
Suggested declaration: "Gill Sans", "Lucida Grande", "Lucida Sans Unicode", Arial,
sans-serif

Palatino
Preinstalled on Mac OS X and many Windows machines, Palatino is a typeface based on classical
Italian Renaissance forms, and is a rather nice serif font. It has become a modern classic
in itself, and is popular among professional graphic designers and amateurs alike. Palatino
works well for both text and display typography, and used carefully it can be a great web font.
Suggestion declaration: Palatino, Georgia, "Times New Roman", serif

Css Tutorial : Web-Safe Fonts

Always, always think about clarity and legibility here. Choose fonts that look good not only on
your chosen platform (most look fabulous on a Mac), but also on others. How legible is your
11-pixel Times text over on a colleague’s PC? What if that colleague has/hasn’t enabled
ClearType font smoothing? What if you need to render some information in italics? Does your
chosen font cope well in such circumstances? Certain fonts give very consistent results in a variety of situations. While some may consider
the likes of Verdana and Georgia as safe bets, this is unfair. Legibility generally overrules whimsy,
and certainly comes well before indulgence. Verdana, for example, is vilified in some circles as
the equivalent of a Queen Greatest Hits album—absolutely everywhere, incredibly familiar, but
not delivering anything new, i.e., boring. Take this raw source though, and give it a little remix,
and who knows what could be drawn from it? My point is that the possibilities with CSS are
endless, and the combination of background images, appropriate font properties, and careful
spacing can give an old font new room in which to shine.
There are, in all facets of web design, rules to be followed. Web standards exist for the
common good, but most rules can be treated as guidelines for good practice. Remember that
nobody is forcing you to do things their way, and the web is an open playing field. Use the font
you want to use, and do not be swayed by ill-considered opinion. Just make sure your visitors
can actually read it.
Let’s look at the web-safe fonts available at the time of writing, illustrated in Figure 4-3,
and consider why some might be more useful than others, and what kind of web sites they
might suit.

Verdana
Verdana is super-legible, and copes with smaller font sizes very well indeed. It is devoid of
extraneous curly bits (technically speaking this means it is a sans serif font—that is, without
serifs), and I’ll bet my laptop that 99.9% of web users will have it installed on their machine,
especially as it comes bundled with Internet Explorer. Use Verdana for a government or blue
chip business web site.

Georgia
Georgia is a serif typeface (so it does have curly bits). It is a web designer’s favorite because
unlike most serif fonts, Georgia has been designed especially for the screen. Its italics, which
are unusually clear and legible on screen, are a major bonus. Use Georgia for a historical information
site, such as a web site about World War II.

Times New Roman
If a font has Times in the name, you can assume it will give the impression of a newspaper typeface,
although this is not always a good thing on screen. Another serif font, Times New Roman is
intended for PC platforms, and is hinted especially for the screen. That said, many would argue
that Georgia supersedes it if you want a particularly legible serif font for your web site.

Times
Times is the Mac equivalent to Times New Roman, but is not designed specifically for screen.
However, Macs being Macs, it’ll still render pretty well on that platform. Use Times New Roman
or Times for a financial institution’s web site.

Arial
Like Verdana, Arial is another sans serif font that lends a more modern feel to web sites. It is
widely used, but doesn’t work very well at lower font sizes, and there isn’t much default space
between the characters. Use Arial for a pretentious museum’s web site.

Helvetica
The closest web font to Arial for the Mac, Helvetica is another sans serif font regarded as a
superstar typeface in the real world, but faring not so well on screen. Use Helvetica for a new
media company’s web site.

Tahoma
With so little choice for web designers, you’d think there would at least be some variety on the
menu, but sadly there isn’t. Tahoma resides on any computer that has Microsoft Office installed
on it, and is thus available to most of your visitors, but it bears an uncanny resemblance to
Verdana, and is therefore used rarely. Use Tahoma for a business web site, or alternatively just
use Verdana.

Comic Sans MS
You want variety? Well, this is pretty wild. I say wild, but really Comic Sans is a bit of a joke. It’s
rare that you’ll come across a design that demands its text be rendered in this very informal
typeface. It is unfortunate that any web sites using Comic Sans tend to have that “my first web
site” feel about them, and it is best reserved for unfunny jokes or printable party invitations.
Use it if you wish, but steer clear if you are looking to present even a tiny amount of professionalism.
Use Comic Sans for your little sister’s web site about ponies.

Trebuchet
A great screen font, designed originally at a smaller size than most fonts, which usually means
a font that has less unique subtleties. That said, Trebuchet manages to convey a contemporary
feel in a limited space, and is a stylish choice that ships with copies of Internet Explorer. Use
Trebuchet for your personal portfolio.

Courier
Courier is a monospaced font, meaning every character is the same width as you would find on
a traditional typewriter, so they line up vertically as well as horizontally. Courier New is the
most common monospaced font but can appear faint when anti-aliased at smaller sizes. Alternatives
are Lucida Console (Windows), American Typewriter (Mac), and Monaco (Mac). Use
Courier or its alternatives to display code or simulate typewriter text.


Css Tutorial : Available Fonts

Arguably the most annoying problem designers face with regard to the Web is the poor choice
of fonts available. This boils down to the fact that the only fonts that can be specified are the
ones certain to be installed on every computer used to view the web site. For example, just
because you have Sharktooth Italicized installed on your computer doesn’t mean everyone else
has. Specify it in your style sheet, and the text on your web site, as viewed on your computer, will be
rendered with it, but very few other users will have such an obscure font installed and available
to their browser. The more obscure the font you specify, the more likely it is that you’ll run
into trouble.
Thus, it is important to think about web-safe fonts. These are few, but they can be used
with confidence, as most of your visitors will have them installed.

Css Tutorial : Font Shorthand

You probably won’t be surprised to discover that there are some useful methods of shortening
font declarations, pulling several into one simple statement. Later you’ll combine four or five
declarations into one, but for now, let’s collate font-family and font-size.
As mentioned in previous chapters, the most important thing with shorthand is the order
in which declarations are stated. In this case, font-size precedes font-family.
/* Specify blanket rules for all elements */
body {
font: 12px "Lucida Grande", Arial, Sans-serif;
}
With this basic knowledge of the two most important CSS font properties in your arsenal,
you can now begin to experiment with new and interesting font choices. To get you started, in
the following section we’ll plough into the vast array of available web fonts of which there is an
exhaustive and unlimited choice. (I’m joking again.)

Monday, October 4, 2010

Css Tutorial : border-color

Remember that, unless you declare the color for the border, it will inherit color from the
element or parent element. As there is only one property for color (border-color), multiple
colors must be declared using shorthand:
/* Container for centering all our content */
#container {
width: 400px;
margin: 10px auto 10px auto;
padding: 20px;
border-style: dashed dotted solid ridge;
border-top-width: thin;
border-right-width: 20px;
border-bottom-width: medium;
border-left-width: thick;
border-color: #000 #999 #333 #CCC;
}
which gives us borders from black (top) to lightest gray (left)

Css Tutorial : border-width

border-width
More specifically, in this section we will look at border-width, border-top-width, border-rightwidth,
border-bottom-width, and border-left-width. These properties allow you to define the
width of the element’s edges one by one or all at once. Note that for a border-width value to be
applied, a border-style must first be declared.
Several keyword values are available here, as well as relative lengths. The keywords are
thin, medium, and thick, although caution is required as different browsers render these borders

in various ways. Exactly how many pixels each browser will use does vary. Note that if you
define a border-style but not a border-width, the default value is medium.
In the following example, the border-style values declared previously are combined with
specific border-width keywords and relative values:
/* Container for centering all our content */
#container {
width: 400px;
margin: 10px auto 10px auto;
padding: 20px;
border-style: dashed dotted solid ridge;
border-top-width: thin;
border-right-width: 20px;
border-bottom-width: medium;
border-left-width: thick;
}

Css Tutorial : Border,Border Properties

Border
 
Borders are a simple concept with a million possibilities. Any element can have a border placed
around it, and borders can be placed on all sides, or just the sides you desire. The border property
is particularly flexible as each border can be a specific width, color, or style. As a result of the
numerous values that can be applied, a greater number of shorthand declarations are available.


Border Properties

 
The default values are a border with a medium thickness, inheriting the text color of the parent
element. Only by applying further values can this default state be influenced. The full list of
border properties is
border-style
border-width
border-top-width
border-right-width
border-bottom-width
border-left-width
border-color
border
border-top
border-right
border-bottom
border-left
Controlling borders is relatively easy, but it is worth looking at each property in more detail.

Css tutorial : Centering with margin: auto

The best way to center an element with CSS is to use the auto value for left and right margins.
For modern browsers, all this requires is a set width rule (as without it the box would naturally
stretch to fit its container—in this case the browser window) and the left and right margins
given the auto value. Building upon the earlier example, we have the following rule:
/* Container for centering all our content */
#container {
width: 400px;
margin: 10px auto 10px auto;
padding: 20px;
border: 1px solid #000;
background: #CCC;
}
Most browsers are happy with this, although IE5/Win fails miserably, usually aligning the
element to the left. At the time of writing, most IE users are using IE6, and IE7 is on the way, but
the percentage of IE5 users is significant enough to warrant consideration.
There is a way of making it work for IE5/Win, and it’s quite simple. The trick is to make use
of the text-align property in the container’s parent element (in this case that is the <body>) to
center the container. The downside is that all child elements within <body> will now correctly
inherit that value and center all their content, which isn’t good. Therefore text-align: left is
applied to all main division elements to counter the centering:
/* Define default values for the whole site */
body {
text-align: center;
}
/* Container for centering all our content */
#container {
width: 400px;
margin: 10px auto 10px auto;
padding: 20px; border: 1px solid #000;
background: #CCC;
text-align: left;
}
This approach ensures that the container is centered horizontally in the browser window
whatever browser is used, and acts as the perfect basis for any centered design (see Figure 3-6).

Css Tutorial : Margin Shortcuts

A couple of easy shortcuts are available to reduce up to four margin:value declarations into one.
/* Basic container */
#container {
margin: 20px auto 1em auto;
}
The order of the values is very important here. The order is top (20px), right (auto), bottom
(1em), and right (auto). It helps to get used to the order by thinking of it like a compass—north,
east, south, and west, the first value always being north, or like a clock—12 o’clock, 3 o’clock,
6 o’clock, and 9 o’clock, the first value being 12 o’clock.
If all four values are the same, the declaration can be shortened even further:

/* Basic container */
#container {
margin: 20px;
}
These shortcuts will also work for padding and some other properties, which you’ll find out
more about later.

Css Tutorial : Margin Declarations

Margin
 
The margin property does exactly what it says on the tin. It is used to declare the margin between
an (X)HTML element and those elements outside of it. The margin can be set for the top, left,
right, and bottom of the given element. Note that margin values are not inherited from parent
elements. If they were, there would be chaos.

Margin Declarations
 
There are three choices of values for the margin property, which are length, percentage, or auto.
Note that if a value is 0, you do not need to add px.
Consider the following CSS for a container div. Note that it has a fixed width of 300px and
no margin properties.
/* Basic container */
#container {
width:300px;
border: 1px solid #000;
padding: 20px;
background: #CCC;
}

padding values declared in the <body> element.

/* Basic container */
#container {
width:300px;
margin-top: 20px;
margin-left: auto;
margin-right: auto;
margin-bottom: 1em;
border: 1px solid #000;
padding: 20px;
background: #CCC;
}

Css Tutorial : Adding Child Divs

The container div can act as parent to child divs. Here, an ID holds a reusable class called box:
<div id="container">
<p>This is our content area.</p>
<div class="box">
<p>I'm in a box!</p>
</div>
<div class="box">
<p>I'm also in a box!</p>
</div>
</div>
The CSS rules for the box class are almost the same as that for the parent container, except
for the background color, which will appear by default. Note that, as no set width is defined for
the box, it will stretch to fit whatever contains it, be that another div or the browser window:

/* Define styling of our reusable box */
.box {
margin: 10px;
padding: 20px;
border: 1px solid #000;
}

Css Tutorial : Adding a Div

You create a division element within the (X)HTML by placing the following in the body:
<div>
<p>This is our content area.</p>
</div>
The result is a hook to which CSS can be applied. In Chapter 2, you learned about using IDs
and classes to add identifiers to a standard (X)HTML element. The same formula is used for
divs by referencing the selector in the opening tag using id="name" or class="name". In this
case, we’ve used an ID named container to define the division:

<div id="container">
<p>This is our content area.</p>
</div>
Let’s apply some simple CSS to the container ID:
/* Container holds all visible page elements */
#container {
padding: 20px;
border: 1px solid #000;
background: #CCC;
}
With this CSS applied to our markup, the container will have a gray background with a
black border, and any elements it contains will be padded 20px from that border, as you can see
in Figure 3-1.

Css Tutorial : Contextual Selectors

Now there’s a horribly scary term. In the previous section, a heading containing emphasized
text was used to illustrate basic inheritance. Remember that the em selector was added to ensure the
<em> element in the (X)HTML would be rendered with red text. Here’s the CSS again:
/* Top-level heading */
h1 {
color:#333;
}
/* Make emphasized text shine brightly */
em {
color:#F00;
}
The downside of this is that all emphasized text across the whole web site would also
become red, regardless of its parent element. Assuming the rule is only meant to target the <em>
element when a child of <h1> headings, a simple adjustment can be made to put the emphasized
text into context:
/* Make emphasized text shine brightly ONLY when it’s the child of an h1 heading */
h1 em {
color:#F00;
}
Contextual selectors consist of two or more simple selectors separated by whitespace.
Here the contextual selector is constructed to show that the rule will only have an effect when
the last selector (em) is a direct descendent (be it a child, grandchild, great grandchild, or so on)
of the first selector (h1). If the browser does not find an exact match (i.e., it only finds <em> elements
outside of <h1> elements), it will not apply the styles dictated by the contextual selector to them.
Here’s similar markup to the original example, but with a paragraph acting as the parent
to a second <em> element:

<h1>This is the greatest heading <em>in the world</em></h1>
<p>I'm sorry but it simply is <em>not</em>, you fool.</p>
The <em> element owned by the <h1> element will be red, whereas the one owned by the
paragraph will not—it will inherit the default font color. To control the style of any emphasized
text that is out of context, simply define a new selector for em with the values you desire, or
maybe create a new contextual selector with p as the parent to your em.
In Chapter 6, contextual selectors will be used to gain tight control of nested elements
such as hierarchical lists and other problematic situations.

Css Tutorial : Inheritance

Inheritance describes situations where (X)HTML elements inherit stylistic properties from a
parent element. By not declaring a particular CSS value for the child element, that child element
may in some circumstances inherit the CSS value given to the parent element. Where CSS
cascades, so (X)HTML inherits.
Inheritance is both a blessing and a curse, and is another very powerful methodology that
is often misunderstood. It can cause confusion across multiple style sheets—especially when
debugging your CSS, and is something to be aware of from the start. On the plus side, it can be
embraced to minimize the size of style sheets and markup, and enable wholesale changes to
many CSS rules with minimal work. Generally, inheritance is always happening to your (X)HTML
elements, and in most cases intervention is only necessary to control the inherited values for a
specific reason.

Parents and Children
 
To understand inheritance, it pays to think of some (X)HTML elements as parents, and the
elements they contain as children. A parent owns a child, and passes what he or she knows
down to the child. In CSS, inheritance works in a similar way, except that it hands down style
values, and not advice about education or the opposite sex.
Moving through (X)HTML markup, it is clear that some child elements act as parents to
other child elements and so on, and thus a containment hierarchy develops. This containment
hierarchy is also referred to as the tree.

So How Does Inheritance Work?
 
To illustrate inheritance, let’s stay with headings. The <h1> heading in this example is styled
with the following very simple rule:
/* Top-level heading */
h1 {
color:#333;
}
The rule is pretty simple, and you are right to assume that the heading will be rendered
dark gray. Now let’s assume that in the (X)HTML the markup dictates that a few of the words
should be emphasized using <em>:
<h1>This is the greatest heading <em>in the world</em></h1>
At this stage, no CSS rule exists to manipulate the <em> element. Therefore, the text within
the <em> element will inherit the color from the h1 rule (its parent element) and will therefore
also be dark gray. To overrule this inherited color, simply define <em> in the style sheet:
/* Make emphasized text shine brightly */
em {
color:#F00;
}
Now all emphasized text sitewide will be rendered in red, and the element is no longer
inheriting its color from any parent element. Note that unless otherwise defined in your em
selector, other declarations will still be inherited. Thus, if you want just your emphasized text
to be twice the font-size it currently is, declare that specific font-size in your em rule. If you
defined this rule in the p rule, all the text would grow to twice the size.

Inheriting the Body

It is strongly recommended that all serious CSS designs begin with a <body> element declaration in
the main style sheet. The <body> element is more than just a requirement of a well-formatted
(X)HTML page, it is also the parent of every visible element in your template (i.e., not those
within the <head>, which concern meta information and other items not displayed by the browser),
and every element can inherit from it.
<body>
<h1>Absolutely everything else!</h1>
<p>Yep, every visible element is contained within the body.</p>
<p>And so on.</p>
</body>
Therefore it makes sense to define all default CSS using body as the first selector in the first
style sheet. Later in the book, the body selector will be used to define key elements such as a
margin for the page, the background color or tiled image, the default font and font color, and
so on, as in this example:
/* Define all main values for the web site */
body {
margin:10px;
font-family:Helvetica,Arial,sans-serif;
background:#CCC;
color:#000;
}
As a result of these declarations, every other rule in the CSS will inherit the values unless
specified otherwise. So all headers, paragraphs, lists, and other elements will be rendered with
black text (color: #000) using the first available font from the suggested options on the end
user’s machine (font-family: Helvetica, Arial, sans-serif;) unless the selector for each
child element specifies otherwise, or that child element is housed inside a more immediate
parent (such as a column or container) that contradicts the inherited values from body.
Note that some style properties are not inherited from the parent element—the background
property being the main example. The child element does not actually inherit the light
gray background (background: #CCC;), but rather the parent element’s background appears by
default. In other words, the child element can be thought of as having the inherited font color,
but it should not be assumed that it has the same background color.

**■Note It is worth remembering that all elements have a transparent background unless you specify otherwise.

A Word of Warning

Much like the cascade through multiple style sheets, inheritance can cause severe headaches if
you do not keep track of what is going on. Often, CSS is as much about what you don’t do as
what you do. The following is a classic example of how inheritance can cause confusion.
Let’s say you are using nested lists (that’s at least one list within another list). The markup
might be something like this three-level list:
<ul>
<li>Top level one</li>
<li>Top level two
<ul>
<li>Second level one</li>
<li>Second level two
<ul>
<li>Third level one</li>
<li>Third level two</li>
</ul>
</li>
<li>Second level three</li>
</ul>
</li>
<li>Top level three</li>
</ul>
You might then apply some basic CSS to that list to control font-size, as follows:
/* Font size for list elements */
li {
font-size:2em;
}
Your first thought might be that this selector would ensure that all text will be 2em in size,
but this is not the case. Each nested list will inherit the font-size value from the one above, and
as the em is a relative measurement (more on that later in the “Ems” section), this will result in the text size being doubled with every nested list. So, a list item within another list will inherit
the font-size rule and become twice as big as its parent.
In Chapter 6, you will learn all about lists and how to apply CSS correctly to avoid such a
hazard, but this is a fine example of how inheritance can cause problems. The flip side of this
is that there will be many times where such powerful inheritance can also be used to do the
work of several targeted selectors, and it’s all about knowing when it’s happening and when it
needs to be tamed.

**■Caution “Where on earth did that underline come from?” Inheritance can confuse anyone working
across multiple style sheets. Be very careful not to duplicate an existing selector unwittingly. If in doubt,
search your style sheets and ensure there won’t be a conflict. It pays to develop your own system of control
here, perhaps noting all ID/class names you’ve used in a separate text file or notepad.

Css Tutorial : Group Exceptions

What if you still wish to treat one of those headings slightly differently? Let’s say you’re happy
with the font, line-height, and color, but wish to render the <h1> heading in italicized text.
Luckily, there is a way to do that while still grouping h1 with the other selectors.
To create the italicized text, none of the existing declarations need to be altered. A new
declaration, font-style: italic, is required, however. If that were to be added to the group, all
headings would be italicized. Instead, a new style is added to the existing group: /* Heading styles */
h1, h2, h3 {
font-family:Helvetica,Arial,sans-serif;
line-height:140%;
color:#333;
}
/* Additionally, render all h1 headings in italics */
h1 {
font-style:italic;
}
Now the browser will collate both h1 selectors (one from the grouping and the stand-alone
selector) to render top-level headings as specified in the grouping and also in italics. Again, this
method can be applied to any selectors.

Css Tutorial : Indenting for Clarity

Further to sensible commenting and flags, responsible CSS developers can make their style
sheets even more legible. The following example makes use of comments and flags, but also
aims to make the layout even more legible using white space:
/* =p Default styling for paragraphs
-------------------------------------------------------- */
p {
color: #F00;
font-size: 12px;
}
/* =h1 Make all top-level headings gray and 16px high
--------------------------------------------------------------*/
h1 {
color: #333;
font-size: 16px;
}
For the purposes of this book, I’m using two-space indents for the selector and four-space
indents for the declarations. Indenting is used to provide clarity. In the real world (or your
favorite text editor, to be precise), many developers use one tab click to indent the selector, and
two tab clicks to indent the declarations and closing curly brace. This might seem arbitrary, but
such a layout makes regular searching for rules a whole lot easier. The eye can scan immediate
left for comments, next right for the selectors, and farthest right for the rules.
Embracing such methods at this stage is not essential, but doing so will certainly help
you stay focused and avoid confusion as your style sheets begin to grow. Very few developers
consider these methods as they begin to learn CSS, but thinking and coding logically from day
one are two of the things that separate a great CSS designer from an average one.

Friday, October 1, 2010

Conditional comments

Conditional comments are proprietary code that’s only understood by Microsoft browsers
from version 5 onward, but as they’re wrapped up in standard HTML comments, they
don’t affect other browsers, and they are also considered perfectly valid by the W3C’s validation
services. What conditional comments enable you to do is target either a specific
release of Internet Explorer or a group of releases by way of expressions. An example of a
conditional comment is shown in the following code block:
<!--[if IE 6]>
[specific instructions for Internet Explorer 6 go here]
<![endif]-->
Anything placed inside this comment will only be shown in Internet Explorer 6—all other
browsers ignore the content. This is most useful for adding IE-specific style sheets to a web
page, within which you can place overrides. For example, rather than using the box model
hack shown earlier in the chapter, you would have a clean style sheet, and then override
specific values in a separate style sheet for Internet Explorer 5.x, attached within a conditional
comment.
Generally, problems with Internet Explorer fall into the following camps: rare issues with
Internet Explorer 7, problems that affect versions 6 and below, and problems that specifically
affect version 5.x. With that in mind, I mostly add three IE-specific style sheets to my
web pages, with the newest release at the top. Conditional comments are generally added
after the “default,” or clean, style sheets (which in this case are the main style sheet
added using a style element, and a print style sheet added using a link element).
<style type="text/css" media="screen">
/* <![CDATA[ */
@import url(x.css);
/* ]]> */
</style>
<link rel="stylesheet" rev="stylesheet" href="x-print.css"
å type="text/css" media="print" />

<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="ie-7-hacks.css"
å media="screen" />
<![endif]-->
<!--[if lte IE 6]>
<link rel="stylesheet" type="text/css" href="ie-6lte-hacks.css"
å media="screen" />
<![endif]-->
<!--[if lt IE 6]>
<link rel="stylesheet" type="text/css" href="ie-5-hacks.css"
å media="screen" />
<![endif]-->
Within the comments, lte IE 6 means “less than or equal to Internet Explorer 6,” so anything
added to ie-6lte-hacks.css affects Internet Explorer 6 and below; lt IE 6 means
“less than Internet Explorer 6,” so anything added to ie-5-hacks.css affects versions of
Internet Explorer below 6. An alternate way of attaching a style sheet for Internet Explorer
5 would be to use the syntax if IE 5. Since the cascade still affects the rules within style
sheets attached inside conditional comments, it makes sense to fix things for Internet
Explorer 6 and below first, and then work backward to Internet Explorer 5.x to fix the few
remaining things that need sorting out.
Note that the preceding code block also includes a link to a print style sheet—print style
sheets are covered in Chapter 10.
Let’s now examine the example from earlier, which has the following code hack to deal
with the box model issues that affect versions of Internet Explorer below 6:
.box {
padding: 20px;
width: 340px;
voice-family: "\"}\"";
voice-family: inherit;
width: 300px;
}
When using conditional comments, you’d make the rule in the default style sheet clean,
with no hacks:

.box {
padding: 20px;
width: 300px;
}
You’d then add a rule to your style sheet that only Internet Explorer versions below 6 can
see (the one within the conditional comment that references lt IE 6 in the large code
block shown earlier).
.box {
width: 340px;
}
Compliant browsers read the rule in the clean style sheet. Internet Explorer versions below
6 then override the width value, thereby displaying the box as intended. Unlike when
using a CSS hack, however, the CSS hasn’t been compromised in any way. The majority of
problems detailed in the “Common fixes for Internet Explorer” sections later in the chapter
are to do with CSS, and therefore require conditional comments when they’re being
dealt with.