The 6 Most Important CSS Techniques You Need To Know
I thought I would give a quick tutorial, with examples, of the 6 most important CSS techniques that I think you need to know:
1. Get a Consistent Base Font Size
2. Get Consistent Margins
3. Set a Float to Clear a Float
4. Image Replacement
5. Faux Columns
6. CSS Sprites
1. Get a Consistent Base Font Size
body { font-size: 62.5%; }
Until IE finally supports the resizing of text in pixels, this is the best technique to gain full control over your font sizes. By setting the body font-size to 62.5%, that will set your font size to 10 pixels. That way, 1em is equal to 10 pixels.
Although I typically then set my container font-size to 1.2em, which in turn sets the font-size to 12 pixels. But still, then you know that 1em is equal to 12 pixels.
2. Get Consistent Margins
There are some great CSS resets out there, but I usually don’t need one that goes so far. I like to just remove the margin and padding from every element.
html, body, div, h1, h2, h3, h4, h5, h6, ul, ol, dl, li, dt, dd, p, blockquote, pre, form, fieldset, table, th, td { margin: 0; padding: 0; }
Then, you can set the margins that you want accordingly.
3. Set a Float to Clear a Float
Floating is probably one of the most important things to understand with CSS, but knowing how to clear floats is necessary too. All you need to remember is: Set a Float to Clear a Float.
I have created a simple page with two floating columns next to each other. You will notice in the example that the grey background does not contain the floating columns. So, the easiest thing to do is to set the containing element to float. But now, you will see that the container background doesn’t contain the content area.
Since the container has margin: 0 auto, we do not want to float it because it will move it to whichever side we float it. So another way to clear the float, is to insert a clearing element. In this case, I just use an empty div set to clear: both. Now, there are other ways to clear a float without markup, but I have noticed some inconsistencies with that technique, so I just sacrifice an empty div.
4. Image Replacement
Sure, there are a lot of different image replacement techniques. But, I think that you get to most benefits from this technique. I also discussed this technique in a previous article, Improved Navigation Image Replacement
The Markup
Company Name
The CSS
h1#logo {
height: 110px;
overflow: hidden;
position: relative;
width: 560px;
}
h1#logo span {
background: url(/wp-content/uploads/2008/03/logo.gif) no-repeat;
display: block;
height: 100%;
left: 0;
position: absolute;
top: 0;
width: 100%;
}
Basically, all we are doing is absolutely positioning an image over top of the HTML text.
The reason that I like this technique is because even when images are disabled, the text is still visible. Of course this means that you cannot use a transparent image to lay over top of the text, so it will not work in all situations.
5. Faux Columns
It is very common in layouts to have 2 columns next to each other, with one column having a background color, and the other column just being white. Since the columns will almost never have the same amount of content in them, the easiest way to fake this, is to have a 1px tall background image being repeated vertically in the containing element of the 2 columns.
The Markup
Primary Column
…
Secondary Column
…
The CSS
div#container {
background: url(/wp-content/uploads/2008/03/content-bg.gif) repeat-y top right;
padding: 10px 0;
width: 640px;
}
div#primaryContent { float: left; padding: 0 10px; width: 400px; }
div#secondaryContent { float: right; padding: 0 10px; width: 200px; }
Pretty simple: a containing element, 2 floating columns, and a clearing element so that the containing element will contain the floating columns (as noted in the Set a Float to Clear a Float technique above).
6. CSS Sprites
CSS sprites is the technique of combing images to lessen the number of calls that need to be made to the server. Then you just shift the position of the background image to view the correct part of the image. May sound complicated, but it just takes a little math. Note: In both of these examples, I am using the Image Replacement technique discussed above.
Example #1
For this example, we are just going to have one download link that we want to replace with a nice graphic. Then, on hover, we want to change the image.
The Markup
The CSS
a#download {
display: block;
height: 75px;
overflow: hidden;
position: relative;
width: 150px;
}
a#download span {
background: url(/wp-content/uploads/2008/03/download.gif) no-repeat;
display: block;
height: 100%;
left: 0;
position: absolute;
top: 0;
width: 100%;
}
a#download:hover span { background-position: 0 -75px; }
As you can see from the code, on hover, we shift the position of the background image to view a different part of the image.
Oh, and also, IE6 and 7 suck, so here are the styles we are serving to them:
Styles to IE6 and IE7
a#download { cursor: pointer; }
I realize that we could just put that in the regular stylesheet since it has no affect on other browsers, but I prefer to move stuff like that to the IE only stylesheets.
Styles to IE6 Only
a#download:hover { background-position: 0 0; }
This is some weird bug where the images shift when you hover, but then when you mouse-out, the images don’t shift back.
See the example »
Example #2
For the second example, we are going to use the CSS sprites technique, but for the entire navigation. This way, only one call needs to be made to the server to load the navigation. We are basically creating a CSS sprites matrix.
The Markup
The CSS
ul#nav { background: #91c6d5; float:left; list-style: none; width: 510px; }
ul#nav li { float: left; }
ul#nav a { color: #000; display: block; font-size: 1.5em; height: 38px; text-align: center; position: relative; }
ul#nav span { background: url(/wp-content/uploads/2008/03/nav.gif) no-repeat; display: block; height: 100%; left: 0; position: absolute; top: 0; width: 100%; }
ul#nav li#home a { width: 102px; }
ul#nav li#home a:hover span { background-position: 0 -38px; }
ul#nav li#about a { width: 106px; }
ul#nav li#about span { background-position: -102px 0; }
ul#nav li#about a:hover span { background-position: -102px -38px; }
ul#nav li#work a { width: 92px; }
ul#nav li#work span { background-position: -208px 0; }
ul#nav li#work a:hover span { background-position: -208px -38px; }
ul#nav li#play a { width: 79px; }
ul#nav li#play span { background-position: -300px 0; }
ul#nav li#play a:hover span { background-position: -300px -38px; }
ul#nav li#contact a { width: 131px; }
ul#nav li#contact span { background-position: -379px 0; }
ul#nav li#contact a:hover span { background-position: -379px -38px; }
That may seem crazy, but it all makes sense if you take the time to think about it.
Again, we have to serve some similar styles to IE6 and 7 from the previous example:
Styles to IE6 and IE7
ul#nav a { cursor: pointer; }
Styles to IE6 Only
ul#nav a:hover { background-position: 0 0; }
In Conclusion
This list is definitely not exhaustive; they are just the 6 that I think are extremely important. What other techniques do you think are important to know
-
Google to launch its own Nexus One phone next year, maybe (Updates) [/caption] Google staff are trying out a new unbranded phone that could be launched next year and sold directly to... -
best free 100 ipod touch apps - weekly ratings #9 [/caption] Top 100 Free iPhone Apps 1 :: iGraveDigger Price: $0.00 Released: 2009-12-21 in the Games category Details: :: Download... -
Tech Review of the iPhone 4 [/caption] By Jacqui Cheng The iPhone 4 is Apple's "biggest leap since the original iPhone," at least according to Steven... -
20th Anniversary of the Hubble Space Telescope [/caption] The Hubble telescope, launched on 24 April 1990, has had a chequered history, requiring major repairs early in its... -
Tour de France - Facts and Figures [/caption] The Tour de France is an annual bicycle race that covers approximately 3,600 kilometres (2,200 mi) throughout France and...
-
6 Things To Avoid "Thesis" Look For Your Blog I am bored. Yes, I am bored of looking blogs with same "thesis"-ly look. I have no idea why people... -
Better Than Barbie - Positive Dolls for Girls I previously posted the measurements of the average woman compared to Barbie's. This spurred comments regarding Barbie as a poor...
-
Why Auto Responders are an Important Part of a Home Business If you own your own business, then you should have a website to expand your business. With a website you... -
Simple Rotating Headers Using CSS Sprites and CSS Image Replacement While I was creating my 2nd WordPress theme (still in the works...) I thought of using rotating image headers. I... -
Fallene Cotz SPF 58 Water Resistant UVB/UVA Sunscreen for Sensitive Skin, 2.5-Ounce Tube User Reviews Send this to a friend Fallene Cotz SPF 58 Water Resistant UVB/UVA Sunscreen for Sensitive Skin, 2.5-Ounce Tube...
