A Quick Selection of CSS Tips
January 24th, 2009I have compiled a quick list of some useful CSS hints and tips that I use in nearly every website I create. A lot of my time has been saved by using these CSS techniques. Read, learn and use them in your projects.
Clearfix
Ok, let’s start with one tip that I use in every web project of mine. Clearfix will become your best friend as soon as you start to use it. It has saved me many times and I only wish I had known about this eariler. So what does clearfix do? When you float something inside a containing div the div’s height will not work as you might think. The float does not seem to be contained within the div at all. Clearfix sorts this out by forcing some content after the div and forcing the div to be the height of the floating element. It’s very easy to use. Simply add a class of clearfix to any element containing a floated element and add the following lines of CSS to your stylesheet.
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.clearfix {
display: inline-block;
}
html[xmlns] .clearfix {
display: block;
}
* html .clearfix {
height: 1%;
}
Style the horizontal rule (hr)
HTML should not be used for presentation but sometimes you’ve just got to use a horizontal rule. Get rid o the default formatting using the following css or similar:
hr {
color: #000;
background-color: #000;
height: 4px;
border: none;
}
CSS Hack for IE
Sometimes Internet Explorer just won’t render your CSS the way you expect. In those cases you can use a dirty little CSS trick which will only be recognised by IE. Use an underscore before any CSS property to hide it from other browsers.
.class {
width:100px;
_width:110px;
}
Remove the outline from links
I always use an h1 tag to display a company logo on a website and use an image replacement technique to replace the h1 text with an image of a logo. Doing this causes two visual problems. Let’s start with the image replacement html and CSS:
HTML
<h1 id="logo"><a href="index.html">Company Name</a></h1>
CSS
h1#logo a {
display:block;
width:250px;
height:80px;
background:url(logo.gif) no-repeat;
text-indent:-999999px;
}
Ok, so using this technique forces the text way off to the left of the page and uses a background image to display the logo. The two problems to fix here are the dotted line around the link (this just looks sloppy) and the fact that this goes way off to the left of the page. two simple lines of CSS added to the logo anchor tag’s CSS will solve this:
overflow:hidden;
outline:none;
The first line ensures that the content is kept within the boundaries set and the second removed the horrible dotted line. Simple!
Double float margin in IE6
For a long time I simply thought that IE 6 simply couldn’t count until I came across this bug. When you have a floated element with a margin defined in the same direction of the float IE 6 will double the margin.
#sidebar {
float: left;
width: 300px;
margin-left: 10px;
}
IE6 will force the margin to be double it’s actual value, in this case to 20px - very annoying. Fortunately there is an easy fix. Simply add this CSS rule to the floated element:
display: inline;
Z-Index
z-index is something that positions elements on the page in the z-axis. It’s a stacking order from front to back which will allow you to place an element in front or behind another. Just make sure your position is not static for the elements you are indexing, make them positioned relatively or absolutely on the page. Then use a number to order the elements on the page. The lower the number the further to the back the element goes. The following code shows an example of this in action.
#topElement {
position:absolute;
top:0;
left:0;
z-index:100;
}
#bottomElement {
position:absolute;
top:0;
left:0;
z-index:0;
}
Transparency
Unfortunately transparency is not something that works with one CSS statement. It is possible, however in almost all modern browsers using the following CSS:
.transparent {
filter:alpha(opacity=50); /* IE's proprietary filter for transparency */
-moz-opacity:0.5; /* For use with older Mozilla browsers like Netscape Navigator */
-khtml-opacity: 0.5; /* Used for old versions of Safari */
opacity: 0.5; /* The current standard. Supported by Firefox, Safari, and Opera */
}




January 25th, 2009 at 7:59 pm
Hi thank you for the article. Instead of clearfix I use this that I think is more simple: overflow: auto for all browsers, and for IE just state the width of the div.
January 26th, 2009 at 5:39 am
Styling hr directly with the css, works different in different browsers.
Depending on what you want to do with your hr (background-image? border styles?), it’s sometimes better to wrap the hr in a div and then hide the hr
like:
Then you can style the div:
div.hr { styles: go here; }
div.hr hr { display: none; }
for more info check
http://www.sovavsiti.cz/css/hr.html
It’s a bit hackish but gives good results in all browsers i’ve tested in
January 27th, 2009 at 11:35 pm
Great! Thank you!
I always wanted to write in my site something like that. Can I take part of your post to my blog?
Of course, I will add backlink?
Sincerely, Reader
February 1st, 2009 at 6:01 pm
[...] described in an earlier tutorial covering CSS tips and tricks we can create content in a page element and style that element to be semi transparent using a [...]
February 14th, 2009 at 12:01 am
I don
February 16th, 2009 at 11:54 am
Hello, and thanks for the article. Just wanted to leave some more information on the solution that comment #1 had. The Clearfix solution that you wrote about in the post is the “old” solution to fix the problem with floats and containers not expanding. The new solution is much simpler and works in the same browsers as Clearfix does. For a nice article on the technique take a look at the following article.
http://www.quirksmode.org/css/clearing.html
March 14th, 2009 at 12:53 pm
This is the first time I comment here and I should say you give genuine, and quality information for bloggers! Good job.
p.s. You have an awesome template for your blog. Where did you find it?
April 24th, 2009 at 10:58 am
If you ever want to see a reader’s feedback
, I rate this article for 4/5. Decent info, but I have to go to that damn yahoo to find the missed bits. Thanks, anyway!
June 26th, 2009 at 8:24 am
Great site and the tutorials and handy for reference.
Although you mention that you always use the H1 tag for the company logo .. semantically speaking, shouldn’t this tag always be used to describe the content of that page?
The BBC do a great job of giving guidelines:
http://www.bbc.co.uk/guidelines/futuremedia/technical/semantic_markup.shtml
September 2nd, 2009 at 6:19 am
This is indeed a great article. CSS always plays a vital role in web design and it always gives you opportunity to make your website Google friendly. I have compiled few factors ‘why Google loves CSS’ that you can check at http://www.bestpsdtohtml.com/get-to-know-why-css-is-good-for-google/
January 27th, 2010 at 9:04 pm
Well, the blog post is really the freshest on this topic. I agree with what you have written and look forward to more of such updates. A big for the phenomenal lucidity in your writing. I will definitely grab your rss feed to stay tuned of your updates.