August 14, 2006...11:25 am
CSS Best Practices
Last month at the UPHPU meeting, Wade Shearer presented on CSS best practices. He’s one of the few programmers in the group that’s a designer first, and a programmer second, so he has unique insight into web design. Here are my notes:
- Keep HTML free of presentational attributes
- Write clean, semantic HTML
- Use HTML tables semantically–for tabular data, not layout (generally)
- Create print-friendly version of all your pages using media=print
- For input buttons, use a 1px invisible GIF and then restyle the image with CSS:
- Do the same thing for image links, but for accessibility include link text overwritten by a style:
- Use comments in CSS to separate typography, headers, layout, forms
- Sometimes body styles don’t cascade into tables like they should so you need to repeat body styles on all tables
- begin with a few default styles:
- Restyling the horizontal rule (<hr>) with an image can be a beautiful addition to a web page
- Keep a library of helpful CSS classes:
- Use PHP to do browser sniffing and to include CSS files relevant to the section.
- For more best practices, take a peak at the stylesheets for Wade’s place of employment, Doba.com
<!-- HTML -->
<input type="image" src="1px.gif" class="next_button" />
// CSS
input.next_button
{
background-image: url(next_button.gif);
}
<!-- HTML -->
<a href="next_page.html" class="next_button" />Next Page</a>
// CSS
a.next_button
{
display: block;
background-image: url(next_button.gif);
text-indent: -99999px;
}
table, tr, td
{
margin:0;
padding:0;
border:0;
border-collapse:collapse;
vertical-align:top;
}
form
{
padding:0;
margin:0;
}
img
{
border:none;
padding:0;
margin:0;
}
.float_left
{
float:left;
}
.float_right
{
float:right;
}
.clear
{
clear:both;
}
.col2_left
{
float:left;
width:45%;
}
.col2_right
{
float:right;
width:45%;
}
If you want to be notified the next time I write something, sign up for email alerts or subscribe to the RSS feed. Thanks for reading.
26 Comments
August 14, 2006 at 11:51 am
…now, if only IE would play nice.
I always design with CSS, and my work time essentially doubles due to having to hack the CSS to make it look pretty in IE 6 and IE 7. A web designer’s world would be much better if M$ would figure out how to make a good browser…
May 10, 2007 at 6:24 am
May 10, 2007 at 2:42 pm
Your first two points state:
however, adding a class name float_right for elements that you want to float right means you are building the presentation into the markup and making your class names unsemantic.
If you wanted to change the design you would have to change the meaning of your classes, which would make it confusing to maintain, or you would have to change the markup losing the idea of separating presentation from content.Hi there,
I would also advise browser sniffing using conditional comments as Internet Explorer is the only one you really need to serve different stuff to and they are a safe method.
Apart from that, good tips, I really have to find myself a decent reset style sheet.
May 10, 2007 at 3:03 pm
May 10, 2007 at 3:07 pm
May 10, 2007 at 3:58 pm
As for CSS resetting, the two I know are Yahoo Reset and Eric Meyer’s.
May 10, 2007 at 4:20 pm
May 11, 2007 at 6:43 am
how to solve this cross browser compatibility.
is there any way to write 2 css for differnt browser.
what i mean is 1 css for mozilla and another css for ie.it will be time consuming but will solve my problem.
or just give me another idea so that it has same looks on all the browsers.
May 11, 2007 at 8:03 am
June 12, 2007 at 8:30 am
July 3, 2007 at 7:22 am
July 13, 2007 at 12:52 am
August 27, 2007 at 3:20 pm
August 29, 2007 at 5:03 pm
September 23, 2007 at 7:43 am
September 24, 2007 at 6:56 pm
October 12, 2007 at 1:34 am
October 26, 2007 at 5:00 am
November 10, 2007 at 5:52 pm
December 4, 2007 at 10:44 pm
January 2, 2008 at 8:34 am
January 16, 2008 at 10:06 pm
March 21, 2008 at 12:35 am
March 22, 2008 at 12:13 pm
eg. my text but i am not sure how, whether this is a good practice or not..though i raised a different issue here.. please let me know
March 22, 2008 at 12:35 pm
If you wanted to change the design you would have to change the meaning of your classes, which would make it confusing to maintain, or you would have to change the markup losing the idea of separating presentation from content.
i have not understood this..
please elaborate more on this..
May 6, 2008 at 6:47 am
Leave a Reply