I used to write the worst code in the world.
It was sloppy, bloated and choked search engine crawlers like a boney chicken sandwich.
Then I learned the right way to use CSS. I learned it from Andy Budd, Simon Collison and Cameron Moll in a phenomenal book called CSS Mastery that should be on every front-end developer’s bookshelf.
That’s my copy looking well-worn from years of use.
Here’s the thing: search engines love clean code. When you give it to them your websites tend to rank better.
But if you learned how to build websites with tables and <font> tags chances are your code isn’t clean. Your pages are probably overloaded with extraneous crap that search crawlers don’t need or want.
Using CSS properly gets all of your presentational code out of the way so search crawlers can focus on what they want: your content. It’s also from an SEO standpoint in various other ways.
Here are seven ways you can use CSS to improve your search rankings.
1. Clean up your code
When code gets bloated the signal to noise ratio gets out of control. The result is code that search crawlers can’t make sense of (not to mention developers trying to get maintenance/updates done).
The beauty of CSS summed up: it keeps your styles separate from your content.
From an SEO standpoint this is great – search crawlers get straight to the content with none of the code that sets colors/fonts/sizes getting in the way. And your design can stay glorious. Everyone’s happy.
2. CSS-driven drop-down menus
Drop-down menus are nifty and add a level of usability to your website – especially when your site architecture requires multiple levels of links.
But building them from scratch sucks. So most of us grab scripts from around the web and frankenstein them into our designs to speed up development.
Here are some SEO-friendly CSS drop-down scripts to consider using for your next project:
- Son of Suckerfish Dropdowns from HTML Dog
- CSS Express Drop-Down Menus from Project VII
- Chrome CSS Drop Down Menu from DynamicDrive
3. Hidden/expandable DIVs
Sometimes a design calls for having a portion of the page “expand” to reveal more text or other content when a user rolls over with their mouse or clicks a link/button.
Using CSS you can create a hidden <div> (which is a generic container), set it to be “invisible” and trigger it to appear when a user mouses over or otherwise activates it – and you can do it in a way that keeps all of the code visible to search engines.
There’s a great example of this at RandomSnippets.com (complete with the code that drives the action).
4. Use images for headings without sacrificing the text
Often times your design guidelines require you to use a certain font for your headings and other elements. But only a handful of fonts are “web safe” – meaning they’re standard to most operating systems and browsers so your users will see them.
The normal approach to this problem is to use an image instead of text to achieve the look you’re going for.
The problem: search engines can’t read words in images.
CSS provides a pretty painless solution – use image replacement to hack the text back into your heading.In other words, you get to keep your heading image AND your text (so search engines can read it).
Here are two methods to do this that are simple and quick:
5. Use images for links without sacrificing the text
Sometimes you can’t get that top navigation bar looking just right without using images for the buttons. But anchor text is important.
You could use ALT text to indicate the text in the images – and this will give the search engines a hint. But if you’re using background images with CSS for your links you won’t have this option.
Setting the “title=” attribute in your links will also give some indication – but it pales in comparison to including actual anchor text.
With CSS you can use images for your links while still including anchor text in the code.
One easy way: set your “text-indent” property to -9999px;. This essentially pushes the text off the page, leaving the image viewable. This is the same method as the Phark method listed above.
Note: don’t get funny with this and try stuffing keywords into your links since they won’t be viewable. If you get a manual review from someone on a search engine’s spam team your site will be smacked.
6. Speed up load times
Load times are a concern for search engines. Crawlers will only spend so much time parsing your pages (depending on how important they think the pages are). That means if you have pages that contain a lot of content and features chances are search crawlers won’t bother with the whole thing.
Not to mention the usability issues you create with slow page loads.
Another thing to keep in mind: Google Webmaster Tools shows you how much time Googlebot spends downloading pages at your website. Think about it: why would Google store this information if page load time was not a concern for their crawler? Do you think Google wants to send their users to pages that take 5-6 seconds to load?
Using CSS properly can chop page load times significantly. It frees your site of <font> tags, <table> tags and many of the other extraneous code that drags down your site performance.
7. Control code order
With most CSS-driven designs you can actually change the order of elements in your source code without effecting the design. This is because the presentation (the position, size, layout, color, etc) of elements is entirely separate from the code.
Why is this useful for SEO?
There’s a little issue with the way Google crawls links on to your internal pages. They only count the first anchor text they find.
That means if you link to your Services page with the text “Services” in your main navigation, but then link with anchor text “Widget Repair Services” somewhere in the body of your home page only the first anchor text will count.
So tweaking your footer links and body links won’t help if the same pages are linked to in your header.
CSS gives you a unique opportunity: to visually place an element independent of its position in the source code.
For example: if your left navigation column occurs first in the source code, followed by your body, or right, column, you can’t use internal links in the body to optimize those lower-level pages (because they’re already linked to in the navigation). With CSS you can swap the columns in the source code without effecting the visual design. So when Googlebot comes along it’ll first scan the links in your body copy, count the anchor text there and ignore the anchor text it sees later when it scans down to your navigation.
These are just a few of the ways working with CSS can improve SEO. If anyone knows of some other CSS tricks that help with SEO please feel free to drop them into the comments.