25 Incredibly Useful CSS Tricks You Should Know

22 comments
Here are 25 incredibly useful CSS tricks that will help you design great web interfaces. You might be aware of some or all of these techniques, but this can be your handy resource to nifty CSS tricks that you should know.

1. Change Text Highlight Color

You might not have known this before! With CSS you can control the colors of selected test at least for standards compliant cutting edge browsers like Safari or Firefox.
::selection{ /* Safari and Opera */
 background:#c3effd;
 color:#000;
}
::-moz-selection{ /* Firefox */
 background:#c3effd;
 color:#000;
}

2. Prevent Firefox Scrollbar Jump

Firefox usually hides the vertical scrollbar if size of the content is less than the visible window but you can fix that using this simple CSS trick.
html{ overflow-y:scroll; }

3. Print Page Breaks

While most of the internet users prefer to read content online but some of your users might like to print your article. With CSS you can control the page breaks within content just add this CSS class to your stylesheet and add this class to any tag which you would like to print on next page.
.page-break{ page-break-before:always; }

4. Using !important

Experienced CSS programmers are usually aware of this but beginners do miss out on this !important CSS rule. By adding !important to your CSS rule, you can increase its precedence over other subsequent rules. e.g. in below code, background-color will be blue and not red due to !important.
.page { background-color:blue !important;   background-color:red;}

5. Replace Text With Image

This is a nice SEO trick that lets you show a nice fancy image instead of simple boring text to your visitors but search engines will see only the text.
.header{
 text-indent:-9999px;
 background:url('someimage.jpg') no-repeat;
 height: 100px; /*dimensions equal to image size*/
 width:500px;
}

6. Cross Browser Minimum Height

Internet Explorer does not understand the min-height property but here’s the CSS trick to accomplish that in IE.
view plaincopy to clipboardprint?
#container{
 height:auto !important;/*all browsers except ie6 will respect the !important flag*/
 min-height:500px;
 height:500px;/*Should have the same value as the min height above*/
}

7. Highlight links that open in a new window

This piece of CSS code will highlight links that open in a new window so that user knows before hand that link will pop open in a new tab or window.
a[target="_blank"]:before,
a[target="new"]:before {
 margin:0 5px 0 0;
 padding:1px;
 outline:1px solid #333;
 color:#333;
 background:#ff9;
 font:12px "Zapf Dingbats";
 content: "\279C";
}

8. Style Your Ordered List

Style Ordered List
Style numbers of an ordered list in different way than the content of each list item.
ol {
 font: italic 1em Georgia, Times, serif;
 color: #999999;
}
ol p {
 font: normal .8em Arial, Helvetica, sans-serif;
 color: #000000;
}

9. Drop Caps Using CSS

Drop Caps using CSS
You can create a drop caps effect like those in newspapers or magazines using the :first-letter pseudo element.
p:first-letter{
display:block;
margin:5px 0 0 5px;
float:left;
color:#FF3366;
font-size:3.0em;
font-family:Georgia;
}

10. Cross Browser Opacity

Though CSS3 standard includes the opacity property, but not every browser supports it, here’s the CSS trick for cross browser transparency.
.transparent_class {
 filter:alpha(opacity=50);
 -moz-opacity:0.5;
 -khtml-opacity: 0.5;
 opacity: 0.5;
}

11. Vertical centering with line-height

If you are using fixed height container and need to vertically center the text, use the line-height property to do that perfectly.
line-height:30px;

12. Center Fixed Width layout

If you use fixed width layout, you should center the layout so that if somebody views it on larger screen, the page displays in the center of the screen and not on left side.
body{
 width:1000px;
 margin:0 auto;
}

13. Remove vertical textarea scrollbar in IE

IE adds a vertical scrollbar to textarea input fields regardless of the height of content in it. You can fix that with this simple CSS trick.
textarea{
 overflow:auto;
}

14. Remove active link borders

Some browsers like Firefox and IE add a dotted outline border over the link user clicked. It is a useful accessibility feature that lets user know which link he clicked or is in focus. But sometimes you need to get rid of this, here’s the CSS you need to use.
a:active, a:focus{ outline:none; }

15. Prevent Elements from disappearing in IE

Sometimes IE behaves in a peculiar way and makes some elements disappear, which appear when you try to make a selection. It is due to some IE issues with float elements. This can be fixed by adding position:relative; to elements that disappears.

16. Attribute-Specific Icons

CSS Attribute selectors are very powerful giving you many options to control styles of different elements e.g. you can add an icon based on the href attribute of the a tag to let the user know whether link points to an image, pdf, doc file etc.
a[href$='.doc'] {
 padding:0 20px 0 0;
 background:transparent url(/graphics/icons/doc.gif) no-repeat center right;
}

17. CSS Pointer Cursors

This trend has caught up lately. All user interface elements on a web page that can be clicked by user have their cursor similar to that of hyperlink. Here’s the CSS trick to do that.
input[type=submit],label,select,.pointer { cursor:pointer; }

18. Capitalize Text

This trick is especially useful for displaying title of an article on a web page with all its words starting with capital letter.
text-transform: capitalize;

19. Small Caps Text

This is one of the lesser used but useful CSS property. It capitalizes all the letters of text but the size of letters following the first letter of each word is smaller than the first letter.
font-variant:small-caps;

20. Highlight Text Input Fields

This CSS trick lets you highlight the input field currently in focus. This trick does not work in IE though.
input[type=text]:focus, input[type=password]:focus{
 border:2px solid #000;
}

21. Remove Image Border

Image hyperlinks usually get a ugly blue border that makes your image hyperlinks look horrible. It’s better to remove border on all hyperlinked images and add individually to those you want using CSS classes.
a img{ border:none; }

22. Tableless Forms Using labels

Tableless Forms using labels and CSS
Gone are the days when tables were used to layout Forms. CSS lets you make accessible forms with table like layout using label tags. Using label tags makes sure your forms are more accessible. Here’s sample html and css code for tableless form using labels.
p label{
 width:100px;
 float:left;
 margin-right:10px;
 text-align:right;
}

23. Set a Consistent Base Font Size

Setting the font-size property to 62.5% in body tag makes 1em equal to 10px. This lets you use em easily as you can find out the font-size in pixels from em values.
body{ font-size:62.5%; }

24. Highlight Acronym and Abbr Tags

acronym and abbr tags provide useful information to users, browsers and search engines about acronyms and abbreviations, but most of the browsers except Firefox do not highlight them. Here’s the CSS trick to highlight acronym and abbr tags within your web page.
acronym, abbr{
 border-bottom:1px dotted #333;
 cursor:help;
}

25. CSS Reset by Eric Meyer

This piece of CSS code resets all the browser default styles preventing any browser inconsistencies in your CSS styles.
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
vertical-align: baseline;
background: transparent;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}

/* remember to define focus styles! */
:focus {
outline: 0;
}

/* remember to highlight inserts somehow! */
ins {
text-decoration: none;
}
del {
text-decoration: line-through;
}

/* tables still need 'cellspacing="0"' in the markup */
table {
border-collapse: collapse;
border-spacing: 0;
}

22 comments

Useful and handy tricks for any level

Great post!
I've just used the selection color in my site.
Thanks!

This comment has been removed by the author.

Very useful tricks, thanks!

Some remarks :
– “5. Replace Text With Image” : it has accessibility issues when you do not download images (ex. : slow connections), and when you print ;
– “12. Center Fixed Width layout” : 990px would be better ;
– “14. Remove active link borders” : this should not be done, link borders are necessary accessibility features ;
– “15. Prevent Elements from disappearing in IE” : “zoom: 1;” would also work I think (most of the time) ;
– “22. Tableless Forms Using labels” : an alternative would be to use “display: inline-block;”, it prevents some glitch with floats.

Great post anyway, thanks a lot !

Article is very nice..

Creating rounded corners with CSS.
No need to design the rounded corners for curved boxes.

I have tried my self. It reduced my work

http://varadesigns.com/css-tricks/create-rounded-corners-with-css.html

-vara

Useful and handy tricks, thanks dude

once please visit my website:
http://www.addcolours.com

Wow.........wonderful tricks.I love to design using CSS.They are very easy.Thanks for sharing.


Magento Themes

Thanks dude..

I am visiting your site for a past one month. I feel happy by reading these blogs. These blogs are very much useful for me to gather more experience. Keep on posting...

Its great and very effective and knowledgeable post to me. Thanks.

Just wanted to convey my appreciation for what I learned from this article. I look forward to learning more on this website.

Wow.........wonderful tricks.I love to design using CSS.They are very easy.Thanks for sharing.

Hi Bharadwaj...
Thanks for the tutorial, very useful...
But if you have provided example OUTPUT under each one that would have been awesome and easy to understand for the beginners, how exactly that css works.

I'm curious to find out what blog system you have been working with? I'm having some minor security problems with my latest website and I'd like to find something more safe. Do you have any recommendations?
Here is my page ; bangaldeshi searh engine

Aw, this was a really nice post. Finding the time and actual effort to create a top
notch article… but what can I say… I put things off a lot
and don't seem to get anything done.
Feel free to surf my weblog bangla song mila

Howdy! Someone in my Myspace group shared this site with us so I came to check
it out. I'm definitely enjoying the information. I'm book-marking and will be tweeting this to my followers! Wonderful blog and fantastic style and design.
My web-site : ppc advertising networks

Excellent blog here! Also your web site rather a lot up fast!
What host are you using? Can I am getting your affiliate hyperlink on your host?
I desire my web site loaded up as quickly as yours lol
my webpage :: auto captcha

Heya i'm for the first time here. I came across this board and I to find It truly useful & it helped me out much. I'm hoping to give something again and aid others such as you aided me.
Also visit my weblog captcha breaker

Not long ago i came accross your website and also have been reading along. I thought I would leave my initial comment. Nice blog. I will keep visiting this web site very frequently. Many thanks

Web Design training courses web-designing offer instructions in the fundamental techniques involved in the creation of effective web pages. The courses equip the student with the technical knowledge, as well as an understanding of the mechanical and artistic components of modern web site design.

We would love to hear from you...

back to top