Next, we create our CSS commands, to create a minimum width of 600px:
#container { min-width: 600px; width:expression(document.body.clientWidth < 600? "600px": "auto" );
10/13/2006 9:40 PM
My Top Ten CSS Tricks
3 of 7
http://www.sitepoint.com/print/top-ten-css-tricks
} The first command is the regular minimum width command; the second is a short JavaScript [5] command that only IE understands. Do note, though, that this command will cause your CSS document to become invalid; you may prefer to insert it into the head of each HTML document to get around this. You might also want to combine this minimum width with a maximum width:
#container { min-width: 600px; max-width: 1200px; 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 class="box":
.box { width: 80px; height: 35px; } html>body .box { width: auto; height: auto; min-width: 80px; min-height: 35px;
10/13/2006 9:40 PM
My Top Ten CSS Tricks
4 of 7
http://www.sitepoint.com/print/top-ten-css-tricks
} 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 [6]. 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: uppercase, 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).
10/13/2006 9:40 PM
My Top Ten CSS Tricks
5 of 7
http://www.sitepoint.com/print/top-ten-css-tricks
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 [7], 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:
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 [8], by S.W. Weiss, is a great read.
9. 3-d Push Button Effect Back in the early days of the Web, 3-d buttons that appeared to become pushed in when moused over were all the rage. At that time, this could only be achieved through images and JavaScript, but now, with the advent of CSS, we can go retro and re-create this 3-d effect [9]. The main CSS commands you'll need are:
a { 10/13/2006 9:40 PM
My Top Ten CSS Tricks
6 of 7
http://www.sitepoint.com/print/top-ten-css-tricks
display: block; border: 1px solid; border-color: #aaa #000 #000 #aaa; width: 8em; background: #fc0; } a:hover { position: relative; top: 1px; left: 1px; 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:
You'll then need to insert an id into the 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 , in 'About Us', it should read , and in 'Contact Us',
id="contact">. 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 id="about" and 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. Back to SitePoint.com [1] /glossary.php?q=C#term_8 [2] /glossary.php?q=H#term_75 [3] http://tantek.com/CSS/Examples/boxmodelhack.html [4] /glossary.php?q=I#term_30 [5] /glossary.php?q=J#term_9 [6] http://meyerweb.com/eric/articles/webrev/200006b.html [7] http://www.alistapart.com/articles/goingtoprint/ [8] http://www.amazon.co.uk/exec/obidos/ASIN/0470844469/ref=pd_ecc_rvi_3/026-2336786-5677265 [9] http://www.sitepoint.com/examples/csstricks/csstricks.htm
Easy-to-understand books for web professionals The CSS Anthology: 101 Essential Tips, Tricks & Hacks The Search Engine Marketing Kit Build Your Own Web Site The Right Way Using HTML & CSS » Page 1