25 Incredibly Useful CSS Tricks You Should Know

Posted by Muhammad Shiraz Kamboh Monday, 4 June 2012 0 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.
  1. ::selection{ /* Safari and Opera */  
  2.     background:#c3effd;  
  3.     color:#000;  
  4. }  
  5. ::-moz-selection{ /* Firefox */  
  6.     background:#c3effd;  
  7.     color:#000;  
  8. }  

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.
  1. 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.
  1. .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.
  1. .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.
  1. .header{  
  2.     text-indent:-9999px;  
  3.     background:url('someimage.jpg'no-repeat;  
  4.     height100px/*dimensions equal to image size*/  
  5.     width:500px;  
  6. }  

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.
  1. #container{  
  2.     height:auto !important;/*all browsers except ie6 will respect the !important flag*/  
  3.     min-height:500px;  
  4.     height:500px;/*Should have the same value as the min height above*/  
  5. }  

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.
  1. a[target="_blank"]:before,  
  2. a[target="new"]:before {  
  3.     margin:0 5px 0 0;  
  4.     padding:1px;  
  5.     outline:1px solid #333;  
  6.     color:#333;  
  7.     background:#ff9;  
  8.     font:12px "Zapf Dingbats";  
  9.     content"\279C";  
  10. }  

8. Style Your Ordered List

Style Ordered List
Style numbers of an ordered list in different way than the content of each list item.
  1. ol {  
  2.     fontitalic 1em Georgia, Times, serif;  
  3.     color#999999;  
  4. }  
  5. ol p {  
  6.     fontnormal .8em ArialHelveticasans-serif;  
  7.     color#000000;  
  8. }  

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-letterpseudo element.
  1. p:first-letter{  
  2. display:block;  
  3. margin:5px 0 0 5px;  
  4. float:left;  
  5. color:#FF3366;  
  6. font-size:3.0em;  
  7. font-family:Georgia;  
  8. }  

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.
  1. .transparent_class {  
  2.     filter:alpha(opacity=50);  
  3.     -moz-opacity:0.5;  
  4.     -khtml-opacity: 0.5;  
  5.     opacity: 0.5;  
  6. }  

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.
  1. 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.
  1. body{  
  2.     width:1000px;  
  3.     margin:0 auto;  
  4. }  

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.
  1. textarea{  
  2.     overflow:auto;  
  3. }  

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.
  1. 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.
  1. a[href$='.doc'] {  
  2.     padding:0 20px 0 0;  
  3.     background:transparent url(/graphics/icons/doc.gif) no-repeat center rightright;  
  4. }  

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.
  1. 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.
  1. text-transformcapitalize;  

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.
  1. 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.
  1. input[type=text]:focus, input[type=password]:focus{  
  2.     border:2px solid #000;  
  3. }  

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.
  1. 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.
  1. <form method="post" action="#" >  
  2.     <p><label for="username" >Username</label>  
  3.         <input type="text" id="username" name="username" />  
  4.     </p>  
  5.     <p><label for="password" >Username</label>  
  6.         <input type="password" id="password" name="pass" />  
  7.     </p>  
  8.     <p><input type="submit" value="Submit" /></p>  
  9. </form>  
  1. p label{  
  2.     width:100px;  
  3.     float:left;  
  4.     margin-right:10px;  
  5.     text-align:rightright;  
  6. }  

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.
  1. 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.
  1. acronym, abbr{  
  2.     border-bottom:1px dotted #333;  
  3.     cursor:help;  
  4. }  

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.
  1. html, body, div, span, applet, object, iframe,  
  2. h1, h2, h3, h4, h5, h6, p, blockquote, pre,  
  3. a, abbr, acronym, address, big, cite, code,  
  4. del, dfn, em, font, img, ins, kbd, q, s, samp,  
  5. small, strike, strong, sub, sup, tt, var,  
  6. b, u, i, center,  
  7. dl, dt, dd, ol, ul, li,  
  8. fieldset, form, label, legend,  
  9. table, caption, tbody, tfoot, thead, tr, th, td {  
  10. margin: 0;  
  11. padding: 0;  
  12. border: 0;  
  13. outline: 0;  
  14. font-size: 100%;  
  15. vertical-alignbaselinebaseline;  
  16. backgroundtransparent;  
  17. }  
  18. body {  
  19. line-height: 1;  
  20. }  
  21. ol, ul {  
  22. list-stylenone;  
  23. }  
  24. blockquote, q {  
  25. quotesnone;  
  26. }  
  27. blockquote:before, blockquote:after,  
  28. q:before, q:after {  
  29. content'';  
  30. contentnone;  
  31. }  
  32.   
  33. /* remember to define focus styles! */  
  34. :focus {  
  35. outline: 0;  
  36. }  
  37.   
  38. /* remember to highlight inserts somehow! */  
  39. ins {  
  40. text-decorationnone;  
  41. }  
  42. del {  
  43. text-decorationline-through;  
  44. }  
  45.   
  46. /* tables still need 'cellspacing="0"' in the markup */  
  47. table {  
  48. border-collapsecollapse;  
  49. border-spacing: 0;  

Popular Posts

About Me

My photo
I create this Blog for learn the all kind of tutorial about web developing | HTML, Java, PHP, Graphic designing, Corel Draw, Photoshop, Micromedia Flash, Swish and many more related software and internet programming tutorials.

Followers

Blog Archive