My Top Ten CSS Tricks Article
CSS can be complex, and as each new browser version is released, you may well find yourself struggling to keep up with the latest tips and hacks. But those tips and hacks will save your sanity! Here, I’ve put together the ten tips that I find most helpful, to save you the hassle of scrounging around the Web for solutions when time is tight.
1. Block vs. Inline Level Elements
Nearly all HTML elements are either block or inline elements. The characteristics of block elements include:
- Always begin on a new line
- Height, line-height and top and bottom margins can be manipulated
- Width defaults to 100% of their containing element, unless a width is specified
Examples of block elements include
<li>. The characteristics of inline elements, on the other hand, are the opposite of block elements:
- Begin on the same line
- Height, line-height and top and bottom margins can’t be changed
- Width is as long as the text/image and can’t be manipulated
Examples of inline elements include
To change an element’s status, you can use
display: inline or
display: block. But what’s the point of changing an element from being block to inline, or vice-versa? Well, at first it may seem like you might hardly ever use this trick, but in actual fact, this is a very powerful technique, which you can use whenever you want to:
- Have an inline element start on a new line
- Have a block element start on the same line
- Control the width of an inline element (particularly useful for navigation links)
- Manipulate the height of an inline element
- Set a background colour as wide as the text for block elements, without having to specify a width
2. Another Box Model Hack Alternative
The box model hack is used to fix a rendering problem in pre-IE 6 browsers on PC, whereby the border and padding are included in, rather than added onto, the width of an element. A number of CSS-based solutions have been put forward to remedy this; here’s another one that I really like:
border: 1em solid green;
The first width command is read by all browsers; the second by all browsers except IE5.x on PC. Because the second command comes second, it takes precedence over the first: any command that comes second will always override a preceding command. So, how does all this work?
By placing empty comment tags (
/**/) before the colons, we instruct IE5.0 to ignore the command. Likewise, if we place empty comment tags after the colon, IE5.5 will ignore the command. Using these two rules in conjunction with each other, we can hide the command from all of IE5.x browsers.
3. Minimum Width for a Page
A very handy CSS command that exists is the min-width command, whereby you can specify a minimum width for any element. This can be particularly useful for specifying a minimum width for a page.
Unfortunately, IE doesn’t understand this command, so we’ll need to come up with a new way of making this functionality work in this browser. First, we’ll insert a
<div> under the
<body> tag, as we can’t assign a minimum width to the
Next, we create our CSS commands, to create a minimum width of 600px:
width:expression(document.body.clientWidth < 600? "600px": "auto" );
You might also want to combine this minimum width with a maximum width:
width:expression(document.body.clientWidth < 600? "600px" : document.body.clientWidth > 1200? "1200px" : "auto");
4. IE and Width and Height Issues
IE has a rather strange way of doing things. It doesn’t understand the min-width and min-height commands, but instead interprets width and height as min-width and min-height — go figure!
This can cause problems, because we may need boxes to be resizable should we need to fit more text into them, or should the user resize the text. If we use only the width and height commands on a box, non-IE browsers won’t allow the box to resize. If we only use the min-width and min-height commands, though, we can’t control the width or height in IE!
This can be especially problematic when using background images. If you’re using a background image that’s 80px wide and 35px high, you’ll want to make sure that the default size for a box using this image is exactly 80 x 35px. However, if users resize the text, the box size will need to expand gracefully.
To resolve this problem, you can use the following code for a box with
All browsers will read through the first CSS rule, but IE will ignore the second rule because it makes use of the child selector command. Non-IE browsers will read through the second one, which will override the values from the first rule, because this CSS rule is more specific, and CSS rules that are more specific always override those that are less specific.
5. Text-transform Command
One of the lesser known, but really useful CSS commands is the
text-transform command. Three of the more common values for this rule are:
text-transform: lowercase and
text-transform: capitalize. The first rule turns all characters into capital letters, the second turns them all into small letters, and the third makes the first letter of each word a capital letter.
This command is incredibly useful to help ensure consistency in style across an entire Website, particularly if it has a number of content editors. Say for example your style guide dictates that words in headings must always begin with capital letters. To ensure that this is always the case, use text-transform: capitalize. Even if site editors forget about the capitalisation, their mistake won’t show up on the Website.
It’s also preferable to use text-transform: uppercase to capitalise words, as screen readers may pronounce shorter words in capital letters as acronyms. A great example of this is ‘CONTACT US’, which is pronounced as ‘contact U S’ by some screen readers.
6. Disappearing Text or Images in IE?
IE exhibits a very strange bug whereby text or background images sometimes disappear from sight. These items are still actually present and, if you highlight everything on screen or hit refresh, they’ll often re-appear. Kind of strange, huh?
This problem mostly occurs on background images and on text positioned next to a floated element. To remedy the problem, simply insert position: relative into the CSS command for the disappearing element, and for some bizarre reason, that’ll usually fix the problem. If this doesn’t work (and sometimes, it doesn’t), assign a width to the offending element in the CSS — that should fix the problem.
7. Invisible Text
Sometimes, you may actually want to make text invisible. Invisible text can be especially useful for screen reader users, perhaps to assign a label to a form item, or insert a heading ahead of a section. Don’t want to change the visual appearance by inserting these elements? Make them invisible, and no one using a visual browser will know they’re there.
You may also want to make text invisible if using a print or handheld CSS file, as some information may not need to be displayed on either of these mediums (see below for more on this).
To make text invisible, you can use
display: none — easy! This works fine for hiding text from handhelds (if CSS is supported) and printed Web pages, but isn’t so great for many screen readers. Screen readers are now becoming too clever for their own good, and some will actually ignore any text that has the rule
display: none assigned to it.
Therefore, for screen readers users, a new approach is needed:
position: absolute; left: -9000px. This basically takes the text and positions it 9000px to the left of the left edge of the screen, essentially making it invisible.
8. CSS Document for Handhelds
A separate CSS document can be created for PDAs and mobile phones, and activated only when one of these devices is being used to access your site. More and more Websites are creating separate CSS documents for printing, so Web pages automatically become print-friendly when users choose to print them. You can do the same for handheld devices.
The following command is used to call up the CSS document for handhelds:
<link type="text/css" rel="stylesheet" href="handheldstyle.css" media="handheld" />
CSS commands in the handheld CSS file override any equivalent commands in the main CSS document. So, what commands should you place in this file?
Ideally, you want users of handheld devices to avoid having to scroll horizontally.
To test this, open up your Website in a regular browser window and resize it to 150px in width. Then, open up your main CSS file and insert some new commands at the very bottom of the document. The commands you place here should adjust the layout of the Website so that it doesn’t require horizontal scrolling at a width of 150px. Then, open up a new document, cut and paste these new commands over, and save it as handheldstyle.css (or whatever name you want to give it).
Your Websites offering to users of handheld devices should be quite different to its offering to traditional Web browsers, as the user experience is quite different on a handheld device. For further information, a book such as Handheld Usability, by S.W. Weiss, is a great read.
9. 3-d Push Button Effect
The main CSS commands you’ll need are:
border: 1px solid;
border-color: #aaa #000 #000 #aaa;
border-color: #000 #aaa #aaa #000;
Aside from these commands, you can insert other commands to achieve the desired presentation effect — the only limit is your imagination!
10. Same Navigation Code on Every Page
Most Websites highlight the navigation item relating to each user’s location within the Website, to help users orientate themselves. This is a fundamental requirement for basic usability, but it can be a pain: we need to tweak the HTML code behind the navigation for each and every page. Can we have the best of both worlds? Is it possible to have the navigation highlighted on every page, without having to tweak the HTML code on every page? Of course it is!
First of all, you’ll need to assign a class to each navigation item:
<li><a href="#" class="home">Home</a></li>
<li><a href="#" class="about">About us</a></li>
<li><a href="#" class="contact">Contact us</a></li>
You’ll then need to insert an
id into the
<body> tag. The id should be representative of where users are located in the site, and should change when users move to a different site section. When on the ‘Home’ page, it should read
<body id="home">, in ‘About Us’, it should read
<body id="about">, and in ‘Contact Us’,
Next, you create a new CSS rule:
#home .home, #about .about, #about .about, #contact .contact
commands for highlighted navigation go here
This basically creates a rule that only takes effect when
class="home" is contained within
id="home", and when
class="about" is in
class="contact" is in
id="contact". These situations will only occur when the user is in the appropriate section of the site, seamlessly creating our highlighted navigation item.