Useful CSS Tips.

Filed in Web 3
Google Buzz

1. Avoid CSS hacks, use future proof method

We should avoid using hacks unless there are no other ways to fix it. Because, we will not able to know when those hacks will stop working. The most common way to tackle with different version of IEs is using the if else statement:

2. Use group selector

Using group selector is a good practise to minimize your coding effort and also save a few bytes out of your stylesheet. We can group the selector to avoid repeating declaring the same properties for different elements

h1, h2, h3, h4, h5, h6 {
font-family:arial;
margin:0.5em 0;
padding:0;
}

3. Center elements

It’s easy to center an element, for firefox and safari, we only need to specify the width and margin left and right set to auto. However, you will need to specify text-align to center in the parent element for IE.

body {
text-align:center; /* for ie */
}

#container {
width:800px;
margin:0 auto;
text-align:left;
}

body {
text-align:center; /* for ie */
}

#container {
width:800px;
margin:0 auto;
text-align:left;
}

4. CSS Positioning

This is something I’ve just discovered it few weeks ago. Set the abolute position within a container. #item will appear inside the #container exactly 200px from the left and 50px from the top.

#container {
position: relative;
width:500; height:300;
}

#item {
position: absolute;
left: 200px;
top: 50px;
}

#container {
position: relative;
width:500; height:300;
}

#item {
position: absolute;
left: 200px;
top: 50px;
}

5. Text transform

This is something I discovered long time ago, CSS has the ability to transform text to lowercase, uppercase and capitalized the first character of a word. w3schools CSS – Text transform

h1 {
text-transform:uppercase;
}
h2 {
text-transform:capitalize;
}
p {
text-transform:lowercase;
}

h1 {
text-transform:uppercase;
}
h2 {
text-transform:capitalize;
}
p {
text-transform:lowercase;
}

6. Remove links or textbox hightlight border

When you click on textbox or link, you will able to see a border highlighting the element. It’s even more obvious if you are using mac os. Luckily, you can solve it using outline property. I used it when I set the indentation of a link text to -999em and also when I’m building a textfield with rounded border.

a, input {
outline:none;
}

a, input {
outline:none;
}

7. Hidden text

I think the correct way to do it is using text-indent. And also, you’d want to apply outline:none to hide the border. We use hidden text when we’re using images to replace text but we want to make sure search engine can crawl the keyword.

a {
text-indent:-999em;
outline:none;

background: url(button.jpg) no-repeat 0 0;
width:100px; height:50px;
display:block;
}

a {
text-indent:-999em;
outline:none;

background: url(button.jpg) no-repeat 0 0;
width:100px; height:50px;
display:block;
}

8. Keep consistent with SUP and SUB

I have a chance to work on one website that uses ® and ™ massively (bad… bad experience). The problem I was facing is, the sup and sub being rendered differently in different browsers, so, I found this and it solved my problem. Adobe Advisor / CSS

sup,
sub {
height: 0;
line-height: 1;
vertical-align: baseline;
_vertical-align: bottom;
position: relative;

}

sup {
bottom: 1ex;
}

sub {
top: .5ex;
}

sup,
sub {
height: 0;
line-height: 1;
vertical-align: baseline;
_vertical-align: bottom;
position: relative;

}

sup {
bottom: 1ex;
}

sub {
top: .5ex;
}

9. CSS Transparency Setting for all browsers

Yes, I can never able to remember it, so I guess it’s a must to list it out here for future reference.

.transparent_class {
filter:alpha(opacity=50); /* ie */
-moz-opacity:0.5; /* old mozilla browser like netscape */
-khtml-opacity: 0.5; /* for really really old safari */
opacity: 0.5; /* css standard, currently it works in most modern browsers like firefox, */
}

.transparent_class {
filter:alpha(opacity=50); /* ie */
-moz-opacity:0.5; /* old mozilla browser like netscape */
-khtml-opacity: 0.5; /* for really really old safari */
opacity: 0.5; /* css standard, currently it works in most modern browsers like firefox, */
}

10. PNG Fix for IE6

Yes, this is the best thing ever to fix ie6 annoying short coming (it doesn’t work with background-position). However, if you want a better solution to could fix all the png images in your css files, you can try this IE PNG Fix from twinhelix and the new version support background position!

.png_hack{
background-image: url(../img/the_image.png) !important;
background-image: none;
filter: none !important;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=’img/the_image.png’);
}

Posted by Bibek   @   8 November 2009 3 comments
Tags : ,

Share This Post

RSS Digg Twitter StumbleUpon Delicious Technorati
blog comments powered by Disqus
Previous Post
«
Next Post
»
Get Adobe Flash playerPlugin by wpburn.com wordpress themes
G-scale designed by Make Your Own Website In conjunction with Web Hosting   |   Watch Movies Online   |   Computer Repair
All right reserved Bibek Pokharel