Nothing is for sale here. Freewill tips keep the site running. Want to help? → Tip via Paypal

Five Must-Know CSS Tips

Below are 5 “must know” CSS tips. Two are about avoiding unwanted results and three are practical, how-to knowledge. All will help you get better results from your coding experience.

Avoid Unwanted Results

1. The Problem with Padding

A lot of folks find things go awry when they use the CSS padding property in situations where precise measurements are crucial. The problem often is that they don’t account for the padding in their measurements. Padding is added to the width of an element. So this:

div {
  width: 500px;
  padding: 22px;
}

…means the division will be 544 pixels wide unless something else is constraining it. If it must be 500 pixels to fit your design, the padding could blow it up. To have a division 500 pixels wide with 22 pixels of padding we have to subtract the padding:

div {
  width: 456px;
  padding: 22px;
}

Do you understand why we subtracted 44 pixels from the width of the division instead of the 22 pixels of padding we want? It’s because there are 22 pixels of padding on both sides, for a total of 44 pixels. That little "oops" blows up a lot of designs too—but not yours!

2. Hiding Text with CSS

In the “old days” of the Internet, people use to cram keywords into their pages to try and get better rankings, but would hide them from viewers by making the text the same color as the background. The search engines caught onto that and started penalizing sites for it.

With the advancement of CSS, some folks are doing basically the same thing, only they’re moving the text off the page by using negative margins or setting the display property to none.

Don’t do that! The search engines know about it. Search engine raters are trained to look for that and use a browser plugin to disable CSS. All they have to do is click a button and all your hidden text is revealed.

The first time a rater views your page and the hidden text is revealed . . . BOOM! The site is busted and penalized.

I can hear the arguments. But, but, they can't check all the sites. What are my chances of getting caught?

Well, probably pretty good. Don't think they haven't developed automated methods to flag sites for the reviewers to check. And if your site is in a competitive niche, don't think a competitor won't try to find something to report.

Practical Knowledge

3. Relatively Absolute Positioning

When we position an element using an absolute position, it’s there. The trouble is, while having that element precisely where we want, many times “precisely where we want it” is relative to the current content.

Suppose we update the page later and add three paragraphs of text at the top. All the content is moved down the page, but the absolutely positioned element stays in the same place. With the text that surrounds the element changed, it may no longer be positioned where you want.

Furthermore, suppose you want to position something way down the page. It can take a lot of guessing and correcting to get it where you want.

There is an easy fix to both of these problems, just place the element that’s absolutely positioned inside a relative positioned element.

<div style="position: relative;
top: 20px; left: 222px;">
<img src="images/donkey.png"
style="position: absolute;
top: 10px; left: 20px;"> </div>

Now the donkey image in that code example will move down the page with the relatively positioned division, keeping the image with the text it applies to.

4. Chaining Classes

You can assign more than one class to an HTML element. This can be useful for minimizing code.

Suppose you really, really, really wanted to emphasize a certain word like I just did witht he third really. You can code it all into an inline style:

<span style="color: red; text-decoration: underline; font-style: italic; font-weight: bold;">really</span>

Or, you could create a class for each of those styles in your remote style sheet and achieve the same result with this:

<span class="red i b u>Emphasized Text<:/span>

Put all the classes into the same class declaration and seperate them with a space. Easy, fast, time-saving, repeatable, and low in saturated fat!

5. Extend the Float Beyond the Floated Element

NOTE: This tip doesn't apply to small screens viewing a responsive layout. The browsers shifts responsive content around to fit the viewport.

float demo When we float an element the surrounding text will flow around it, like it’s doing here with the text flowing around the image of rusty metal plate. If the text passage is long enough it will then flow under the element once it gets beyond the end of the floated element.

You can see how the text in this paragraph has cleared the floated image and flows under the image now. That’s often what we want to do, but there are also times when we don’t want the text to flow under the element. That’s where the overflow property comes in handy.

float demo
Here’s the same image, only if you glance ahead you can see that the text doesn’t wrap back under the image this time, even though the text has cleared the image and the image is coded to float to one side exactly like it was in the previous example.

That’s because this text is inside a division tag with the overflow property set to hidden.

Just start the division with the overflow property after your floated element and your text won’t flow under the image until the division is canceled. I’ll cancel the division after this paragraph, and as you can see…

…the text has now gone back to the normal page margin. Note that I used images because it was an easy-to-do example, but this works with other elements. You will need to set the width of the floated element for it to work with any element that has a 100% width by default, such as paragraphs or divisions.

Here’s the inline code for that division:

<div style="overflow: hidden;">
Content here... </div>

This technique could be very useful any time you want to display an image and provide descriptions while keeping things more neat and professional looking.

It could also be useful for something as simple as a family picture gallery that includes descriptions for your family and friends. If the text doesn't go past the bottom of the picture you'll need to clear the float or the next image could be beside the first instead of under it. Here's a CSS float tutorial if you need it.

This tutorial is over, but here are some pictures using this technique just for fun.

Just for Fun

float demo
When we asked cousin Ellie to "say cheese"; she thought we said "Go jump in the mud and then make a funny face." She never was the sharpest cheddar in the cheese bin.

Ellie is a cousin on my neighbors side of the family. My neighbor and I aren't related. Do you understand?


float demo
This is my uncle Albert. He was often confused with Admiral Halsey, thanks to Paul and Linda McCartney.

I believe I'm going to rain.


float demo
Ah . . . the happy couple. Not only did Fred and Freda meet in clown school, but they got married in their clown costumes in the back of a polka-dot VW Beetle with 57 people in attendance.

You might think it was a little crowded in that car with that many people inside, but clowns have a way of fitting into tight spaces. That’s one of the tricky things they teach in clown school.

Speaking of clown school, did you know that they have very strict admission requirements? For example:

  • You can't have a criminal record.
  • You have to be funny.
  • Your name can't be Samson. It's just not funny.

There's more, but I'm tired of making stuff up.


float demo
Reginald was so vain. He was always tooting his own horn.

As you can see, this technique works well whether you have a lot of text or a little. It beats putting everything inside a table.

And that, as they say, is that! Scroll down if you want the code for one of these images. All you have to do is copy and paste the code into your page for however many pictures you want to have. Then just make the needed changes (image path/name, size, etc.) and you're good to go.

<img src="images/image-name.jpg" 
style="float: left; 
max-width: 320px;  /* "max" is for responsive sites */
max-height: 213px; /* use normal width and height if your site is not responsive */
margin-right: 20px;" 
alt="demo">
<div style="overflow: hidden; 
font-size: 1.3vw; 
border-top: 1px solid orange; 
padding-top: 9px;">
Text that flows beside the image goes here.
<p>
Add paragraphs as needed.
</p>
</div>
<br style="clear: left;">