25 CSS Snippets for Some of the Most Common and Frustrating Tasks

In this post we have 25 CSS snippets and hacks that will solve many of the most frequently used and, at times, frustrating CSS development tasks. Why reinvent the wheel when there are already plenty of time-saving pre-written CSS code snippets?

As well as some classic and timeless CSS hacks you will also find many CSS3 snippets, like box-shadow, border-radius,linear-gradient and much more.

Adding shadow to text – text-shadow

Helps make your text stand out from the rest.

.text-shadow {
text-shadow: 2px 2px 4px #666;
}

Adding an image-based border – border-image

You can create any kind of border you want for any object(s) on your website using this. Just supply your own border.png image, and the use the border-image property on a object to add that image-based border to it.

Adding rounded corners – border-radius

Self-explanatory. Add rounded corners to your CSS3-based elements, like a border or button. You can change the radius to increase or decrease the rounding of the corners. This is a blanket rounding of each corner; the next code snippet lets you individually round each of the 4 corners.

Adding a gradient – linear-gradient

With CSS3, you no longer need a background image for gradients – you can use CSS to add a gradient to the background of something. Change the color hex values (“74b8d7” and “437fbc”) to the color gradient you want.

Adding print page breaks – page-break

This adds nice page breaks to articles and other long-form content on your website when your visitor decides to print it. Yeah, less and less people are printing on paper, but they’re using their web browser’s print function to create digital copies (PDFs) for future reading or archiving. This snippet makes it easier for them to do so and read afterwards.

.page-break{
page-break-before:always;
}

Quickly create pull-quotes

A pull-quote is exactly what it sounds like: a differently-formatted quote from an article that stands out and is meant to highlight a key statement or other text. This makes it easy for you to create pull-quotes, rather than having to format the text each time you want to include a pull-quote. Change the font and font color by replacing Georgia, "Times New Roman", Times, seri and the ff0000 accordingly.

Center your website horizontally

In the days of high-resolution widescreen displays, it’s annoying to visit websites that are formatted like it’s 1999 ie. aligned all the way to the left. Make sure your website doesn’t suffer the same fate by forcing it to center horizontally.

.wrapper {
width:960px;
margin:auto;
}

Center certain content vertically

Say that three times fast. But seriously, this will vertically center content that’s in a container (like in a cell of an HTML table).

Add a “loading image” to, well, loading images

Useful for those with high-resolution image-heavy websites, like portfolio or gallery sites. Instead of having visitors stare at a blank section of your page wondering if they need to refresh or something, you can have a animated GIF image of a “loading circle” giving visual confirmation that everything’s fine and the image is loading.

img {
background: url(loader.gif) no−repeat 50% 50%;
}

Replacing the title text with a logo – text-indent – text-indent

This code is for when you need to hide the title text in the header of your website and replace it with your logo image instead – but still maintains the text of your website’s title for SEO (search engine optimization) purposes.

File format-dependent link styles

This snippet shows small icons next to a link that’s of a certain file format. The following has file format-dependent link styles for email, pdf, and zip file links. You can add more: just copy/paste one chunk and add your own extension (ex. “.mp3”).

/* external links
The ^= specifies that we want to match links that begin with the http://
*/
a[href^="http://"]{
padding-right: 20px;
background: url(external.gif) no-repeat center right;
}
/* emails
The ^= specifies that we want to match links that begin with the mailto:
*/
a[href^="mailto:"]{
padding-right: 20px;
background: url(email.png) no-repeat center right;
}
/* pdfs
The $= specifies that we want to match links whose hrefs end with ".pdf".
*/
a[href$=".pdf"]{
padding-right: 20px;
background: url(pdf.png) no-repeat center right;
}
/* zip
Same as above but for zip files and it adds an icon at the right of the link. Therefore the :after
*/
a[href$=".zip"]:after{
content: url(icons/zip.png);
}

Resize your background image

Lets you do just what the title says. Simply change the size values and bam: resized background image.

Having multiple columns

This lets you create multiple columns for your content without needing to go through the formatting hassle of creating separate paragraphs or whatnot. Change the number of columns to however many you want. Vertical grey separator lines are included, which you can change the color or remove altogether.

Basic Link Rollover as CSS Sprite

Save bandwidth and the hassle of creating a separate “hover” version of your button by using this sprite. It moves the background image of your button down when hovering over it, creating the hover effect.

Adding Google fonts

Use this to add the Google font API. This lets you use some new cool-looking fonts on your website that all browsers and computers will display properly – so you don’t have to mess with installing fonts separately for your website or hoping your visitors have the appropriate fonts (which no self-respecting designer would do anyway).

Eric Meyers CSS reset

A CSS reset does what it sounds like: resets any inconsistencies in your stylesheet, so that your website will look consistent across all browsers. Things like margins, font sizes, default line heights, and so forth. The following CSS reset is by Eric Meyer, which has become a standard of sorts, hence the “ultimate” tag.

Reset all colors and change link color

This resets all text and background colors so you’re back at square one with black text. Then it changes the color and style of links to distinguish from regular text and make them apparent that they’re links – change the color and styling to whatever you want.

Paul is the founder and editor of Speckyboy Design Magazine. He has many years experience within the web design industry and a passion for the latest web technologies and design trends. He lives in the small town of Inverness in the north of Scotland. Follow him on Twitter.

Comments

There is however one thing you should think about when you use vendor-prefix in css and that is to always include the non-prefixed property after the vendor-prefixed versions.

Why? When a browser implements the standard version of a property, as specified in the relevant CSS3 specification, you most likely want it to use that implementation instead of the experimental, browser-specific version (which it will likely still support to be backwards-compatible).

Putting the non-prefixed property last should ensure that it will override the vendor-prefixed implementation. :)

I see some of these CSS snippets are CSS3, I don’t get it. Some of those styles won’t even work on IE8. Are we moving to fast into new technologies and leaving regular users behind, people who are supposed to be visiting our sites?. Maybe I’m too new in this web design/development industry and veterans don’t care about the regular user.

I always find when working with floats to give the parent tag and overflow:auto to avoid issues with height and background-color properties. A break tag with a clear under the elements in the HTML does the same thing but I find this can be alot quicker. You can run into problems sometimes when you need floated child elements to overlap the outline of the parent object.

IE8 users get flat color, make a message appear for all IE users warning them about their insecure, vulnerable, slow, and ugly web browser, then give them a link to chrome and tell them what they’re missing out on. IE is holding back the future.

A lot of things are here about the CSS and these are very useful… thanks for sharing…

geordieal

Nice round up of CSS tips and tricks, however the color and background reset may be a bit of overkill as you would then have to style every element that you didn’t want a white background / black foreground on ( divs tables, lists, paragraphs, headings etc ) and the use of !improtant would force you to have to use !important on any element you then wanted to style differently. A better approach would be :
* {
color: black;
background:none;
}

This would still have the desired effect of reseting back to a level playing field, but without the problems.

$1914876

Thanks for taking the trouble to combine these excellent and practical CSS samples, brilliant!

Siddikshaik

Good one..!!!

But IE8 will not support many things(Rounded Corners etc)

Atofoni

Nice! some very useful thins in there, bookmarked for sure!
One thing I would do though is use RGBA instead of opacity, using opacity makes everything inside that div the same opacity, so if you want a semi-transparent box with text, the text will be semi-transparent as well. Using RGBA for background color fixes this, the RGB stands for RED,GREEN,BLUE the A stands for ALPHA :)

Rob

Great Article. I am not a fan of having to write out css for webkit, moz, and IE. BUT you have provided some great snippets to assist in all this monotony . I am going to add these to my dreamweaver snippets area.
Thanks SOOO Much. People like you help remind me that the Open Source Community is still alive and thriving.

Please note that “Center certain content vertically” is in fact wrong. While the “source” page linked to *says* min-height is used, in reality their demo uses the height property. The reason for this is simple: in all major browsers, anything using the “table-cell” display will treat height like min-height, and min-height will not work at all.