One of the main advantages of using CSS is the large reduction in web page download time. To style text, you used to have to use the <font> tag over and over again. You probably also laid out your site with tables, nested tables and spacer gifs. Now all that presentational information can be placed in one CSS document, with each command listed just once.
But why stop there? By using CSS shorthand properties you can reduce the size of your CSS document even more.
Use:
font: 1em/1.5em bold italic serif
…instead of
font-size: 1em;
line-height: 1.5em;
font-weight: bold;
font-style: italic;
font-family: serif
This CSS shorthand property will only work if you’re specifying both the font-size and the font-family – omit either and the CSS rule will be completely ignored. Also, if you don’t specify the font-weight, font-style, or font-varient then these values will automatically default to a value of normal, so do bear this in mind too.
Use:
background: #fff url(image.gif) no-repeat top left
…instead of
background-color: #fff;
background-image: url(image.gif);
background-repeat: no-repeat;
background-position: top left;
Omit any of these commands from the background CSS shorthand property, and the browser will use the default values. If you leave out the background-position command then any background image will be place in the top-left of the container and then repeated both horizontally and vertically.
Use:
list-style: disc outside url(image.gif)
…instead of
list-style: #fff;
list-style-type: disc;
list-style-position: outside;
list-style-image: url(image.gif)
Leave out any of these CSS commands from the shorthand rule, and the browser will use the default values for each, namely disc, outside and none (i.e. no images) respectively.
There are a number of different CSS shorthand commands for margin and padding, depending on how many of the sides of the containing element have the same margin or padding values:
Use:
margin: 2px 1px 3px 4px (top, right, bottom, left)
…instead of
margin-top: 2px;
margin-right: 1px;
margin-bottom: 3px;
margin-left: 4px
Use:
margin: 5em 1em 3em (top, right and left, bottom)
…instead of
margin-top: 5em;
margin-right: 1em;
margin-bottom: 3em;
margin-left: 1em
Use:
margin: 5% 1% (top and bottom, right and left)
…instead of
margin-top: 5%;
margin-right: 1%;
margin-bottom: 5%;
margin-left: 1%
Use:
margin: 0 (top, bottom, right and left)
…instead of
margin-top: 0;
margin-right: 0;
margin-bottom: 0;
margin-left: 0
The above rules also apply to padding and border (see below for more on border).
Use:
border: 1px black solid
…instead of
border-width: 1px;
border-color: black;
border-style: solid
Use:
border-right: 1px black solid
…instead of
border-right-width: 1px;
border-right-color: black;
border-right-style: solid
(You can substitute right with top, bottom or left.)
The above CSS shorthand rules can be conveniently combined with the shorthand rules used by margin and padding. Take a look at the following box:

These borders can be achieved with the following CSS command:
border: 8px solid #336;
border-left: 10px solid #ccf;
border-top: 10px solid #ccf
You can achieve exactly the same effect by using:
border: 8px solid #336;
border-width: 10px 8px 8px 10px
border-color: #ccf #336 #336 #ccf
CSS shorthand properties are great! They’re a great way to reduce the amount of code contained in a CSS document, allowing for faster download times and easier editing. Now who can argue with that?
—
This article was written by Trenton Moss. Trenton’s crazy about web usability and accessibility – so crazy that he went and started his own web usability and accessibility consultancy to help make the Internet a better place for everyone. He knows an awful lot about the Disability Discrimination Act and spends much of his time working on the world’s most accessible CMS.
04 Sep
Posted by ProCOM
on September 4, 2007 – 1:08 am - 1,132 views
When styling fonts with CSS you may be doing this:
font-weight: bold;
font-style: italic;
font-variant: small-caps;
font-size: 1em;
line-height: 1.5em;
font-family: verdana,sans-serif
There’s no need though as you can use this CSS shorthand property:
font: bold italic small-caps 1em/1.5em verdana,sans-serif
Much better! Just a few of words of warning: This CSS shorthand version will only work if you’re specifying both the font-size and the font-family. The font-family command must always be at the very end of this shorthand command, and font-size must come directly before this. Also, if you don’t specify the font-weight, font-style, or font-variant then these values will automatically default to a value of normal, so do bear this in mind too.
Usually attributes are assigned just one class, but this doesn’t mean that that’s all you’re allowed. In reality, you can assign as many classes as you like! For example:
<p class="text side">...</p>
Using these two classes together (separated by a space, not with a comma) means that the paragraph calls up the rules assigned to both text and side. If any rules overlap between the two classes then the class which is below the other in the CSS document will take precedence.
When writing a border rule you’ll usually specify the colour, width and style (in any order). For example, border: 3px solid #000 will give you a black solid border, 3px thick. However the only required value here is the border style.
If you were to write just border: solid then the defaults for that border will be used. But what defaults? Well, the default width for a border is medium (equivalent to about 3 to 4px) and the default colour is that of the text colour within that border. If either of these are what you want for the border then you can leave them out of the CSS rule!
Lots of web pages have a link to a print-friendly version. What many of them don’t realise is that there’s no need because you can set up a second CSS document to be called up when a user prints the page.
So, your page header should contains links to two CSS documents, one for the screen, and one for printing:
<link type="text/css" rel="stylesheet" href="stylesheet.css" media="screen" />
<link type="text/css" rel="stylesheet" href="printstyle.css" media="print" />
The first line of code calls up the CSS for the screen (notice the inclusion of media="screen") and the second line calls up the CSS for the printable version (using media="print").
So, what commands should you put in this second CSS document? To work it out, open a blank document and save it as printstyle.css. Next, point the screen CSS command to this document so that the command reads: <link type="text/css" rel="stylesheet" href="printstyle.css" media="screen" />.
Now just keep entering CSS commands until the display on the screen matches how you want the printed version to look. You’ll certainly want to make use of the display: none command for navigation, decorative images and non-essential items. For more advice on this, read Print Different, which also mentions the other media for which you can specify CSS files.
It’s always advisable to use regular HTML markup to display text, as opposed to an image. Doing so allows for a faster download speed and has accessibility benefits. However, if you’ve absolutely got your heart set on using a certain font and your site visitors are unlikely to have that font on their computers, then really you’ve got no choice but to use an image.
Say for example, you wanted the top heading of each page to be ‘Buy widgets’, as you’re a widget seller and you’d like to be found for this phrase in the search engines. You’re pretty set on it being an obscure font so you need to use an image:
<h1><img src="widget-image.gif" alt="Buy widgets" /></h1>
This is OK but there’s strong evidence to suggest that search engines don’t assign as much importance to alt text as they do real text (because so many webmasters use the alt text to cram in keywords). So, an alternative would be:
<h1>Buy widgets</h1>
Now, this obviously won’t use your obscure font. To fix this problem place these commands in your CSS document:
h1
{
background: url(widget-image.gif) no-repeat;
height: image height
text-indent: -2000px
}
Be sure to change “image height” to whatever the height of the image is (e.g. 85px)! The image, with your fancy font, will now display and the regular text will be safely out of the way, positioned 2000px to the left of the screen thanks to our CSS rule. Please note, this can cause accessibility issues as any user with images turned off won’t be able to see the text.
The box model hack is used to fix a rendering problem in pre-IE 6 browsers on PC, where by the border and padding are included in the width of an element, as opposed to added on. For example, when specifying the dimensions of a container you might use the following CSS rule:
#box
{
width: 100px;
border: 5px;
padding: 20px
}
This CSS rule would be applied to:
<div id="box">...</div>
This means that the total width of the box is 150px (100px width + two 5px borders + two 20px paddings) in all browsers except pre-IE 6 versions on PC. In these browsers the total width would be just 100px, with the padding and border widths being incorporated into this width. The box model hack can be used to fix this, but this can get really messy.
A simple alternative is to use this CSS:
#box
{
width: 150px
}
#box div
{
border: 5px;
padding: 20px
}
And the new HTML would be:
<div id="box"><div>...</div></div>
Perfect! Now the box width will always be 150px, regardless of the browser!
Say you wanted to have a fixed width layout website, and the content floated in the middle of the screen. You can use the following CSS command:
#content
{
width: 700px;
margin: 0 auto
}
You would then enclose <div id="content"> around every item in the body of the HTML document and it’ll be given an automatic margin on both its left and right, ensuring that it’s always placed in the centre of the screen. Simple… well not quite – we’ve still got the pre-IE 6 versions on PC to worry about, as these browsers won’t centre align the element with this CSS command. You’ll have to change the CSS rules:
body
{
text-align: center
}
#content
{
text-align: left;
width: 700px;
margin: 0 auto
}
This will then centre align the main content, but it’ll also centre align the text! To offset the second, probably undesired, effect we inserted text-align: left into the content div.
Vertically aligning with tables was a doddle. To make cell content line up in the middle of a cell you would use vertical-align: middle. This doesn’t really work with a CSS layout. Say you have a navigation menu item whose height is assigned 2em and you insert this vertical align command into the CSS rule. It basically won’t make a difference and the text will be pushed to the top of the box.
Hmmm… not the desired effect. The solution? Specify the line height to be the same as the height of the box itself in the CSS. In this instance, the box is 2em high, so we would insert line-height: 2em into the CSS rule and the text now floats in the middle of the box – perfect!
One of the best things about CSS is that you can position an object absolutely anywhere you want in the document. It’s also possible (and often desirable) to position objects within a container. It’s simple to do too. Simply assign the following CSS rule to the container:
#container
{
position: relative
}
Now any element within this container will be positioned relative to it. Say you had this HTML structure:
<div id="container"><div id="navigation">...</div></div>
To position the navigation exactly 30px from the left and 5px from the top of the container box, you could use these CSS commands:
#navigation
{
position: absolute;
left: 30px;
top: 5px
}
Perfect! In this particular example, you could of course also use margin: 5px 0 0 30px, but there are some cases where it’s preferable to use positioning.
One of the disadvantages of CSS is its inability to be controlled vertically, causing one particular problem which a table layout doesn’t suffer from. Say you have a column running down the left side of the page, which contains site navigation. The page has a white background, but you want this left column to have a blue background. Simple, you assign it the appropriate CSS rule:
#navigation
{
background: blue;
width: 150px
}
Just one problem though: Because the navigation items don’t continue all the way to the bottom of the screen, neither does the background colour. The blue background colour is being cut off half way down the page, ruining your great design. What can you do!?
Unfortunately one of the only solutions to this is to cheat, and assign the body a background image of exactly the same colour and width as the left column. You would use this CSS command:
body
{
background: url(blue-image.gif) 0 0 repeat-y
}
This image that you place in the background should be exactly 150px wide and the same blue colour as the background of the left column. The disadvantage of using this method is that you can’t express the left column in terms of em, as if the user resizes text and the column expands, it’s background colour won’t.
Using this method the left column will have to be expressed in px if you want it to have a different background colour to the rest of the page.
—
This article was written by Trenton Moss. Trenton’s crazy about web usability and accessibility – so crazy that he went and started his own web usability and accessibility consultancy to help make the Internet a better place for everyone. He knows an awful lot about the Disability Discrimination Act and spends much of his time working on the world’s most accessible CMS.
04 Sep
Posted by ProCOM
on September 4, 2007 – 12:47 am - 2,389 views
A print stylesheet formats a web page so when printed, it automatically prints in a user-friendly format. Print stylesheets have been around for a number of years and have been written about a lot. Yet so few websites implement them, meaning we’re left with web pages that frustratingly don’t properly print on to paper.
It’s remarkable that so few websites use print stylesheets as:
Some websites do offer a link to a print-friendly version of the page, but this of course needs to be set up and maintained. It also requires that users notice this link on the screen, and then use it ahead of the regular way they print pages (e.g. by selecting the print button at the top of the screen). Print-friendly versions are however useful when printing a number of web pages at the same time such as an article that spans on to several web pages.
How to set up your print stylesheet
A print stylesheet works in much the same way as a regular stylesheet, except it only gets called up when the page is printed. To make it work, the following needs to be inserted into the top of every web page:
<link rel="stylesheet" href="print.css" type="text/css" media="print" />
The file, print.css is the print stylesheet, and the media="print" command means that this CSS file only gets called up when web pages are printed. (There are many different media you can use for stylesheets, such as for handheld, TV, projection etc. – see a full list of media types for more.)
What to put in your print stylesheet
The CSS commands in the print stylesheet essentially override the CSS commands in the main stylesheet. As such, the only commands you need to put in the print stylesheet are ones to override the CSS commands in the main stylesheet. This means you don’t need to repeat any colour or branding CSS commands as they’ll already be taken from the main stylesheet.
Generally speaking, you’ll want your print stylesheet to make the following happen when users hit that print button:
Remove unwanted items
Usually it’s just your organisation logo and page content that you’ll want to appear on the printed version of the web page. You’ll normally want to remove the header, left column and right column. You may also want to remove the footer (or some of it) from the printed version, unless it contains your contact details.
There may be certain isolated items you’d prefer weren’t printed so you can simply assign these class="noprint" in the HTML. To get rid of these items, along with the header and navigation (assuming these are assigned <div id="header"> and <div id="nav">) use the display: none command:
#header, #nav, .noprint {display: none;}
You may also want to remove certain images and adverts, especially animated images as these won’t make sense when printed.
Format the page
There’s nothing worse than printing off a web page to find the last few words of each line cut off. It’s also annoying (and a waste of paper) when the left and right columns are left in, leaving a very narrow space for the content so the web page prints on to 15 pieces of paper.
Generally speaking, the three CSS commands you’ll need are:
width: 100%; margin: 0; float: none;
These commands should be applied to any containing elements (<div>s for a CSS layout and <table>s for table layouts) to ensure the content spans the full width of the paper. So, the full CSS command would perhaps be something like:
#container, #container2, #content {width: 100%; margin: 0; float: none;}
Change the font?
Some print stylesheets do change the font size (often to 12pt) but this isn’t generally a very good idea. If users increase text size on the screen then the text will print in this larger font size… unless you specify a fixed font size in the print stylesheet.
Other print stylesheets change the font family to a serif font (such as Times New Roman) as this is slightly easier to read from print. Whether you choose to do this or not is up to you as users may be a bit surprised to see a different font printed out.
Do also bear in mind that background images and colours don’t print out by default. As such, you may wish to change the colour of text in a light colour so it has a reasonable colour contrast without its background.
Links
Print-outs are often in black and white so do make sure that links have a decent colour contrast. If not, assign links a slightly darker colour in the print out. For example:
a:link, a:visited {color: #781351}
For bonus usability you could include a footnote on the page listing all the URLs from that page, with each link referencing its URL underneath with a number. It’s otherwise impossible to know where a link is pointing to when reading a print out from a web page. See this working example and find out how to do this by reading this Improving link display for print article.
Making the print stylesheet
When making the print stylesheet place the print CSS commands into the bottom of your main CSS file. As you keep adding more commands check how your web pages look on the computer screen (don’t do this on a live website!). Keep adding commands until you’re happy with the appearance, then cut these commands out of the main CSS file and paste into the print stylesheet.
To summarise, your print stylesheet may look similar to this:
/* Remove unwanted elements */
#header, #nav, .noprint
{
display: none;
}
/* Ensure the content spans the full width */
#container, #container2, #content
{
width: 100%; margin: 0; float: none;
}
/* Change text colour to black (useful for light text on a dark background) */
.lighttext
{
color: #000
}
/* Improve colour contrast of links */
a:link, a:visited
{
color: #781351
}
You’ve now got a print stylesheet! For something this quick and easy to set up that improves usability as much as it does, you’d be mad not to use one!
—
This article was written by Trenton Moss. Trenton’s crazy about web usability and accessibility – so crazy that he went and started his own web usability and accessibility consultancy to help make the Internet a better place for everyone. He knows an awful lot about the Disability Discrimination Act and spends much of his time working on the world’s most accessible CMS.
More and more web developers are ditching tables and coming round to the idea of using CSS to control the layout of their site. With the many benefits of using CSS, such as quicker download time, improved accessibility and easier site management, why not?
The problem with CSS
Historically the main problem with using CSS has been lack of browser support. This is no longer the case as version 5 browsers, which all have good support for CSS, now account for over 99% of browsers in use.
Instead, the problem is that browsers can sometimes interpret CSS commands in different ways, causing developers to throw their arms up in the air and switch back to pixel-perfect table layouts. Fear not though, as you learn more about CSS you’ll gradually start to understand the different browser interpretations and realise that there aren’t really that many.
How browser detection using CSS hacks works
The way browser detection using CSS hacks works is to send one CSS rule to the browser(s) you’re trying to trick, and then send a second CSS rule to the other browsers, overriding this first command. If you have two CSS rules with identical selectors then the second CSS rule will always take precedence.
Say for example you wanted the space between your header area and the content to have a gap of 25px in Internet Explorer, or IE as it’s affectionately known. This gap looks good in IE but in Firefox, Opera and Safari the gap is huge and a 10px gap looks far better. To achieve this perfect look in all these browsers you would need the following two CSS rules:
#header {margin-bottom: 25px;}
#header {margin-bottom: 10px;}
The first command is intended for IE, the second for all other browsers. How does this work? Well, it won’t at the moment because all browsers can understand both CSS rules so will use the second CSS rule because it comes after the first one.
By inserting a CSS hack we can perform our browser detection by hiding the second CSS rule from IE. This means that IE won’t even know it exists and will therefore use the first CSS rule. How do we do this? Read on and find out!
Browser detection for Internet Explorer
To send different CSS rules to IE, we can use the child selector command which IE can’t understand. The child selector command involves two elements, one of which is the child of the other. So, html>body refers to the child, body, contained within the parent, html.
Using the example of the header margin, our CSS command would be:
#header {margin-bottom: 3em;}
html>body #header {margin-bottom: 1em;}
IE can’t understand the second CSS rule due to the html>body CSS command so will ignore it and use the first rule. All other browsers will use the second rule.
Browser detection for Internet Explorer 5
It may seem strange at first to send different CSS rules to different versions of a browser, but in the case of IE5 it’s very necessary. This is due to IE5’s misinterpretation of the box model. When specifying the width of an element in CSS, padding and borders aren’t included in this value. IE5 however, incoporates these values into the width value causing element widths to become smaller in this browser.
The following CSS rule would result in a width of 10em for all browsers, except IE5 which would give it a width of just 5em (IE5 would incorporate two sets of padding and border, on both the left and right, when calculating the width):
#header {padding: 2em; border: 0.5em; width: 10em;}
The solution to this problem? Perform browser detection and send a different CSS rule to IE5:
#header {padding: 2em; border: 0.5em; width: 15em; width/**/:/**/ 10em;}
IE5 will use the first width value of 15em, 5em of which will be taken up by the two sets of padding and border (one each for the left and for the right). This would ultimately give the element a width of 10em in IE5.
The 15em value will then be overridden by the second width value of 10em by all browsers except IE5, which for some reason can’t understand CSS commands with empty comment tags either side of the colons. It doesn’t look pretty but it does work!
Browser detection for Internet Explorer on the Mac
Quite simply, IE on the Mac does strange things with CSS. The browser’s become somewhat obsolete as Microsoft aren’t going to be bringing out an updated version. As such, many web developers code their CSS-driven sites so that the site works in IE/Mac, although it may not offer the same level of advanced functionality or design. Provided IE/Mac users can access all areas of the site this is seen as a suitable way of doing things.
To hide a command using the IE/Mac CSS hack is simple, and involves wrapping a set of dashes and stars around as many CSS rules as you like:
/* Hide from IE-Mac \*/
#header {margin-bottom: 3em;}
#footer {margin-top: 1.5em;}
/* End hide */
IE/Mac will simply ignore all these commands. This CSS hack can actually be quite useful if there’s a certain area of the site not working properly in IE/Mac. If that section isn’t fundamental to being able to use the site, you can simply hide it from IE/Mac like so:
#noiemac {display: none}
/* Hide from IE-Mac \*/
#noiemac {display: block;}
/* End hide */
The first CSS rule hides the entire section assigned the noiemac id (i.e. <div id="noiemac">). The second CSS rule, which IE/Mac can’t see, displays this section.
Browser detection for IE 4 and Netscape 4
Version 4 browsers have limited and somewhat erratic support for CSS. Making a CSS layout in these browsers, whose market share has now slipped well below 1%, can be extremely challenging. It’s become common practice nowadays to completely hide the CSS file from version 4 and earlier browsers. This can be achieved using the @import directive to call up the CSS document:
<style type="text/css">@import "styles.css";</style>
Version 4 (and earlier) browsers will display a non-styled version of the site as they can’t understand this @import directive.
Checking your site in the different browsers
At the time of writing, the major Internet browsers include IE5, IE6, Firefox, Opera and Safari. (Check out TheCounter.com for up-to-date browser statistics.) You can download all these browsers, and a number of more obscure ones, at the Evolt browser archive.
Ideally you should check your site in all these browsers, on both PC and Mac (except IE6 and Safari, which are only available on PC and Mac respectively). If you don’t have access to a Mac you can get a screenshot of your site on Safari by running it through Dan Vine’s iCapture or you can pay to use Browsercam which offers a more extensive screen capturing service.
Conclusion
On the whole, modern browsers have very good support for CSS – certainly good enough for you to be using CSS to control layout and presentation. Sometimes however, certain page elements will appear differently in different browsers. Don’t worry too much if you don’t know the reason why – if you can fix it up with these CSS hacks then your web pages should look great across all browsers!
—
This article was written by Trenton Moss. Trenton’s crazy about web usability and accessibility – so crazy that he went and started his own web usability and accessibility consultancy to help make the Internet a better place for everyone. He knows an awful lot about the Disability Discrimination Act and spends much of his time working on the world’s most accessible CMS.
Specs, primers, validators, stuff like that
The CSShark Answers FAQs — Martina Kosloff has compiled a pretty good FAQ on CSS. Worth your time.
css/edge — From the mind of Eric Meyer comes this great little site pushing CSS to the edge. It is, in his words: “intended, first and foremost, to be as relentlessly creative with CSS as we have been practical all these years. It does not exist to present or explain safe cross-browser techniques; in fact, almost the opposite. The goal here is to find ways to make CSS live up to its fullest potential, with only minimal regard to browser limitations.”
websitetips.com CSS section — Literally a ton of links to CSS resources from all over. A better set of links than this one by far.
Style Sheet Reference Guide from webreview.com. — Eric Meyer’s excellent resource, including the justly famous browser compatibility charts. It’s a bit dated (I wish it included Opera 5 and more of CSS2) but still an excellent resource.
CSS School from w3schools.com. — A whole lot of information, references, and examples.
Cascading Style Sheets, level 1 Recommendation from the Web Design Group. — This is the spec (really a rec) folks; can be very helpful if you learn how to read it.
W3C CSS Validation Service — Download a CSS validating app, give it a URL, or cut and paste your CSS here for the ultimate in CSS Validation services.
The Layout Reservoir from BlueRobot. — A resource much like this page with beautiful CSS layouts for you to steal.
Agitprop — Todd Fahrner’s collection of CSS writings, tests, and treatises. Good stuff.
CSS Pointers Group — A great resource full of information and links to other resources on CSS. Some of it is a bit dated (but what isn’t in today’s modern age?), and they are all down on multi column layouts, but still a must visit.
Eric Meyer’s CSS work — The author of the O’Reilly CSS Definitive Guide, Eric has done an amazing amount of writing and work on CSS. One highlight: his frame set views of the W3C’s CSS references.
CSS layout techniques and the sites that use them.
3 Columns, The Holy Grail of page layouts — The most elegant technique and perhaps the most sought after layout: a 3 column page with a fluid center column. Easy to understand, easy to implement. I first saw this layout at dynamic ribbon device and have since learned that the sweet CSS came from Rob Chandanais of BlueRobot. Owen also made a very nice tutorial using this layout technique.
2 Columns, ALA Style — Famously chronicled by Jeffrey Zeldman in his ALA article A Web Designer’s Journey, this is an extremely easy layout to implement requiring only a simple float:left declaration.
4 Columns, All Fluid — This technique can actually be used to provide as many columns on a page as you like. Drawback #1) it gets difficult quickly if you want to make any of the columns a fixed width. Drawback #2) it relies heavily on percentages, which the various browsers all calculate differently, so you can’t place your columns very precisely. Still, a very useful technique, especially if you don’t want borders and different background colors for your columns.
3 Columns, All Fluid — A much simpler and potentially more useful technique that then 4 column technique above. It uses float:left, suffers from needing percentage widths for each column, and from potential column wrapping when the browser window is narrowed.
Static Width and Centered — 3 columns all with static widths, and contained in a parent DIV which remains centered in the window. One rather serious limitation of this particular technique is that if any of the three content DIVs contains an image or a really long word that is longer than the width of the DIV, it totally breaks the layout. Each browser breaks it differently.
Nested Float — A very simple layout that features a nested, floated menu in the upper right. Easily reversed. A variation of this technique is in use on this very page.
30 Aug
Posted by ProCOM
on August 30, 2007 – 2:43 pm - 595 views
When multiple style sheets are used, the style sheets may fight over control of a particular selector. In these situations, there must be rules as to which style sheet’s rule will win out. The following characteristics will determine the outcome of contradictory style sheets.
BODY { background: url(bar.gif) white;
background-repeat: repeat-x ! important }
Authors should be wary of using ! important rules since they will override any of the user’s ! important rules. A user may, for example, require large font sizes or special colors due to vision problems, and such a user would likely declare certain style rules to be ! important, since some styles are vital for the user to be able to read a page. Any ! important rules will override normal rules, so authors are advised to use normal rules almost exclusively to ensure that users with special style needs are able to read the page.
Finally, write the three numbers in exact order with no spaces or commas to obtain a three digit number. (Note, you may need to convert the numbers to a larger base to end up with three digits.) The final list of numbers corresponding to selectors will easily determine specificity with the higher numbers winning out over lower numbers. Following is a list of selectors sorted by specificity:
#id1 {xxx} /* a=1 b=0 c=0 --> specificity = 100 */
UL UL LI.red {xxx} /* a=0 b=1 c=3 --> specificity = 013 */
LI.red {xxx} /* a=0 b=1 c=1 --> specificity = 011 */
LI {xxx} /* a=0 b=0 c=1 --> specificity = 001 */
30 Aug
Posted by ProCOM
on August 30, 2007 – 2:43 pm - 375 views
Pseudo-classes and pseudo-elements are special “classes” and “elements” that are automatically recognized by CSS-supporting browsers. Pseudo-classes distinguish among different element types (e.g., visited links and active links represent two types of anchors). Pseudo-elements refer to sub-parts of elements, such as the first letter of a paragraph.
Rules with pseudo-classes or pseudo-elements take the form
selector:pseudo-class { property: value }
or
selector:pseudo-element { property: value }
Pseudo-classes and pseudo-elements should not be specified with HTML’s CLASS attribute. Normal classes may be used with pseudo-classes and pseudo-elements as follows:
selector.class:pseudo-class { property: value }
or
selector.class:pseudo-element { property: value }
Pseudo-classes can be assigned to the A element to display links, visited links and active links differently. The anchor element can give the pseudo-classes link, visited, or active. A visited link could be defined to render in a different color and even a different font size and style.
An interesting effect could be to have a currently selected (or “active”) link display in a slightly larger font size with a different color. Then, when the page is re-selected the visited link could display in a smaller font size with a different color. The sample style sheet might look like this:
A:link { color: red }
A:active { color: blue; font-size: 125% }
A:visited { color: green; font-size: 85% }
Often in newspaper articles, such as those in the Wall Street Journal, the first line of text in an article is displayed in bold lettering and all capitals. CSS1 has included this capability as a pseudo-element. A first-line pseudo-element may be used in any block-level element (such as P, H1, etc.). An example of a first-line pseudo-element would be:
P:first-line {
font-variant: small-caps;
font-weight: bold }
The first-letter pseudo-element is used to create drop caps and other effects. The first letter of text within an assigned selector will be rendered according to the value assigned. A first-letter pseudo-element may be used in any block-level element. For example:
P:first-letter { font-size: 300%; float: left }
would create a drop cap three times the normal font size.
30 Aug
Posted by ProCOM
on August 30, 2007 – 2:34 pm - 390 views
Any HTML element is a possible CSS1 selector. The selector is simply the element that is linked to a particular style. For example, the selector in
P { text-indent: 3em }
is P.
A simple selector can have different classes, thus allowing the same element to have different styles. For example, an author may wish to display code in a different color depending on its language:
code.html { color: #191970 }
code.css { color: #4b0082 }
The above example has created two classes, css and html for use with HTML’s CODE element. The CLASS attribute is used in HTML to indicate the class of an element, e.g.,
<P CLASS=warning>Only one class is allowed per selector.
For example, code.html.proprietary is invalid.</p>
Classes may also be declared without an associated element:
.note { font-size: small }
In this case, the note class may be used with any element.
A good practice is to name classes according to their function rather than their appearance. The note class in the above example could have been named small, but this name would become meaningless if the author decided to change the style of the class so that it no longer had a small font size.
ID selectors are individually assigned for the purpose of defining on a per-element basis. This selector type should only be used sparingly due to its inherent limitations. An ID selector is assigned by using the indicator “#” to precede a name. For example, an ID selector could be assigned as such:
#svp94O { text-indent: 3em }
This would be referenced in HTML by the ID attribute:
<P ID=svp94O>Text indented 3em</P>
Contextual selectors are merely strings of two or more simple selectors separated by white space. These selectors can be assigned normal properties and, due to the rules of cascading order, they will take precedence over simple selectors. For example, the contextual selector in
P EM { background: yellow }
is P EM. This rule says that emphasized text within a paragraph should have a yellow background; emphasized text in a heading would be unaffected.
A property is assigned to a selector in order to manipulate its style. Examples of properties include color, margin, and font.
The declaration value is an assignment that a property receives. For example, the property color could receive the value red.
In order to decrease repetitious statements within style sheets, grouping of selectors and declarations is allowed. For example, all of the headings in a document could be given identical declarations through a grouping:
H1, H2, H3, H4, H5, H6 {
color: red;
font-family: sans-serif }
Virtually all selectors which are nested within selectors will inherit the property values assigned to the outer selector unless otherwise modified. For example, a color defined for the BODY will also be applied to text in a paragraph.
There are some cases where the inner selector does not inherit the surrounding selector’s values, but these should stand out logically. For example, the margin-top property is not inherited; intuitively, a paragraph would not have the same top margin as the document body.
Comments are denoted within style sheets with the same conventions that are used in C programming. A sample CSS1 comment would be in the format:
/* COMMENTS CANNOT BE NESTED */
Here are some major color selections using the hex-color code for css background elements. Here are the tables for text-based colors and rgb based colors.
| 000000 | 000033 | 000066 | 000099 | |||||||
| 0000CC | 0000FF | 003300 | 003333 | |||||||
| 003366 | 003399 | 0033CC | 0033FF | |||||||
| 006600 | 006633 | 006666 | 006699 | |||||||
| 0066CC | 0066FF | 009900 | 009933 | |||||||
| 009966 | 009999 | 0099CC | 0099FF | |||||||
| 00CC00 | 00CC33 | 00CC66 | 00CC99 | |||||||
| 00CCCC | 00CCFF | 00FF00 | 00FF33 | |||||||
| 00FF66 | 00FF99 | 00FFCC | 00FFFF | |||||||
| 330000 | 330033 | 330066 | 330099 | |||||||
| 3300CC | 3300FF | 333300 | 333333 | |||||||
| 333366 | 333399 | 3333CC | 3333FF | |||||||
| 336600 | 336633 | 336666 | 336699 | |||||||
| 3366CC | 3366FF | 339900 | 339933 | |||||||
| 339966 | 339999 | 3399CC | 3399FF | |||||||
| 33CC00 | 33CC33 | 33CC66 | 33CC99 | |||||||
| 33CCCC | 33CCFF | 33FF00 | 33FF33 | |||||||
| 33FF66 | 33FF99 | 33FFCC | 33FFFF | |||||||
| 660000 | 660033 | 660066 | 660099 | |||||||
| 6600CC | 6600FF | 663300 | 663333 | |||||||
| 663366 | 663399 | 6633CC | 6633FF | |||||||
| 666600 | 666633 | 666666 | 666699 | |||||||
| 6666CC | 6666FF | 669900 | 669933 | |||||||
| 669966 | 669999 | 6699CC | 6699FF | |||||||
| 66CC00 | 66CC33 | 66CC66 | 66CC99 | |||||||
| 66CCCC | 66CCFF | 66FF00 | 66FF33 | |||||||
| 66FF66 | 66FF99 | 66FFCC | 66FFFF | |||||||
| 990000 | 990033 | 990066 | 990099 | |||||||
| 9900CC | 9900FF | 993300 | 993333 | |||||||
| 993366 | 993399 | 9933CC | 9933FF | |||||||
| 996600 | 996633 | 996666 | 996699 | |||||||
| 9966CC | 9966FF | 999900 | 999933 | |||||||
| 999966 | 999999 | 9999CC | 9999FF | |||||||
| 99CC00 | 99CC33 | 99CC66 | 99CC99 | |||||||
| 99CCCC | 99CCFF | 99FF00 | 99FF33 | |||||||
| 99FF66 | 99FF99 | 99FFCC | 99FFFF | |||||||
| CC0000 | CC0033 | CC0066 | CC0099 | |||||||
| CC00CC | CC00FF | CC3300 | CC3333 | |||||||
| CC3366 | CC3399 | CC33CC | CC33FF | |||||||
| CC6600 | CC6633 | CC6666 | CC6699 | |||||||
| CC66CC | CC66FF | CC9900 | CC9933 | |||||||
| CC9966 | CC9999 | CC99CC | CC99FF | |||||||
| CCCC00 | CCCC33 | CCCC66 | CCCC99 | |||||||
| CCCCCC | CCCCFF | CCFF00 | CCFF33 | |||||||
| CCFF66 | CCFF99 | CCFFCC | CCFFFF | |||||||
| FF0000 | FF0033 | FF0066 | FF0099 | |||||||
| FF00CC | FF00FF | FF3300 | FF3333 | |||||||
| FF3366 | FF3399 | FF33CC | FF33FF | |||||||
| FF6600 | FF6633 | FF6666 | FF6699 | |||||||
| FF66CC | FF66FF | FF9900 | FF9933 | |||||||
| FF9966 | FF9999 | FF99CC | FF99FF | |||||||
| FFCC00 | FFCC33 | FFCC66 | FFCC99 | |||||||
| FFCCCC | FFCCFF | FFFF00 | FFFF33 | |||||||
| FFFF66 | FFFF99 | FFFFCC | FFFFFF | |||||||
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| By N2H | |||||