updated: November 23rd, 2016 / Ross Johnson / 0 Comments

Most Used CSS Tricks

Earlier before I have written an article about current best CSS hacks which you can see here And now here’s the list of today’s most used CSS tricks – tips. I have added image examples for most of them because of critics on CSS hacks article. If you think I have missed any please let me know

1. Rounded corners without images
  1. <div id=“container”>
  2. <b class=“rtop”>
  3. <b class=“r1”></b> <b class=“r2”></b> <b class=“r3”></b> <b class=“r4”></b>
  4. </b>
  5. <!–content goes here –>
  6. <b class=“rbottom”>
  7. <b class=“r4”></b> <b class=“r3”></b> <b class=“r2”></b> <b class=“r1”></b>
  8. </b>
  9. </div>
  10. .rtop, .rbottom{display:block}
  11. .rtop *, .rbottom *{display: block; height: 1px; overflow: hidden}
  12. .r1{margin: 0 5px}
  13. .r2{margin: 0 3px}
  14. .r3{margin: 0 2px}
  15. .r4{margin: 0 1px; height: 2px}

Rounded corners without images

2. Style your order list
  1. <ol>
  2. <li>
  3. <p>This is line one</p>
  4. </li>
  5. <li>
  6. <p>Here is line two</p>
  7. </li>
  8. <li>
  9. <p>And last line</p>
  10. </li>
  11. </ol>
  12. ol {
  13. font: italic 1em Georgia, Times, serif;
  14. color: #999999;
  15. }
  16. ol p {
  17. font: normal .8em Arial, Helvetica, sans-serif;
  18. color: #000000;
  19. }

Style your order list

3. Tableless forms
  1. <form>
  2. <label for=“name”>Name</label>
  3. <input id=“name” name=“name”><br>
  4. <label for=“address”>Address</label>
  5. <input id=“address” name=“address”><br>
  6. <label for=“city”>City</label>
  7. <input id=“city” name=“city”><br>
  8. </form>
  9. label,input {
  10. display: block;
  11. width: 150px;
  12. float: left;
  13. margin-bottom: 10px;
  14. }
  15. label {
  16. text-align: right;
  17. width: 75px;
  18. padding-right: 20px;
  19. }
  20. br {
  21. clear: left;
  22. }

CSS Tableless forms

4. Double blockquote
  1. blockquote:first-letter {
  2. background: url(images/open-quote.gif) no-repeat left top;
  3. padding-left: 18px;
  4. font: italic 1.4em Georgia, “Times New Roman”, Times, serif;
  5. }

Double blockquote

5. Gradient text effect
  1. <h1>CSS Gradient Texth1>
  2. h1 {
  3. font: bold 330%/100% “Lucida Grande”;
  4. position: relative;
  5. color: #464646;
  6. }
  7. h1 span {
  8. background: url(gradient.png) repeat-x;
  9. position: absolute;
  10. display: block;
  11. width: 100%;
  12. height: 31px;
  13. }
  14. <!–[if lt IE 7]>
  15. <style>
  16. h1 span {
  17. background: none;
  18. filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=‘gradient.png’, sizingMethod=‘scale’);
  19. }
  20. </style>
  21. <![endif]–>

Gradient text effect

6. Vertical centering with line-height
  1. div{
  2. height:100px;
  3. }
  4. div *{
  5. margin:0;
  6. }
  7. div p{
  8. line-height:100px;
  9. }
  10. <p>Content here</p>

Vertical centering with line-height

7. Rounded corners with images
  1. <div class=“roundcont”>
  2. <div class=“roundtop”>
  3. <img src=“tl.gif” alt=“”
  4. width=“15” height=“15” class=“corner”
  5. style=“display: none” />
  6. </div>
  7. CONTENT
  8. <div class=“roundbottom”>
  9. <img src=“bl.gif” alt=“”
  10. width=“15” height=“15” class=“corner”
  11. style=“display: none” />
  12. </div>
  13. </div>
  14. .roundcont {
  15. width: 250px;
  16. background-color: #f90;
  17. color: #fff;
  18. }
  19. .roundcont p {
  20. margin: 0 10px;
  21. }
  22. .roundtop {
  23. background: url(tr.gif) no-repeat top right;
  24. }
  25. .roundbottom {
  26. background: url(br.gif) no-repeat top right;
  27. }
  28. img.corner {
  29. width: 15px;
  30. height: 15px;
  31. border: none;
  32. display: block !important;
  33. }

Rounded corners with images

8. Multiple class name
  1. <img src=“image.gif” class=“class1 class2” alt=“” />
  2. .class1 { border:2px solid #666; }
  3. .class2 {
  4. padding:2px;
  5. background:#ff0;
  6. }
9. Center horizontally
  1. <div id=“container”></div>
  2. #container {
  3. margin:0px auto;
  4. }

Center horizontally

10. CSS Drop Caps
  1. <p class=“introduction”> This paragraph has the class “introduction”. If your browser supports the pseudo-class “first-letter”, the first letter will be a drop-cap. </p>
  2. p.introduction:first-letter {
  3. font-size : 300%;
  4. font-weight : bold;
  5. float : left;
  6. width : 1em;
  7. }

CSS Drop Caps

11. Prevent line breaks in links, oversized content to brake
  1. a{
  2. white-space:nowrap;
  3. }
  4. #main{
  5. overflow:hidden;
  6. }
12. Show firefox scrollbar, remove textarea scrollbar in IE
  1. html{
  2. overflow:-moz-scrollbars-vertical;
  3. }
  4. textarea{
  5. overflow:auto;
  6. }