最实用的12条css技巧

非原创,来源网络,英文版原文: http://stylizedweb.com/2008/03/12/most-used-css-tricks/

1:Rounded corners without images

效果图——
web标准设计
Rounded corners without images
<div id=”container”>
<b class=”rtop”>
<b class=”r1″></b> <b class=”r2″></b> <b class=”r3″></b> <b class=”r4″></b>
</b>
<!–content goes here –>
<b class=”rbottom”>
<b class=”r4″></b> <b class=”r3″></b> <b class=”r2″></b> <b class=”r1″></b>
</b>
</div>

.rtop, .rbottom
{display:block}
.rtop *, .rbottom *
{display: block; height: 1px; overflow: hidden}
.r1
{margin: 0 5px}
.r2
{margin: 0 3px}
.r3
{margin: 0 2px}
.r4
{margin: 0 1px; height: 2px} 

2:Style your order list

效果图——
web标准设计
Style your order list
<ol>
<li>
<p>This is line one</p>
</li>
<li>
<p>Here is line two</p>
</li>
<li>
<p>And last line</p>
</li>
</ol>

ol 
{
font
: italic 1em Georgia, Times, serif;
color
: #999999;
}
ol p 
{
font
: normal .8em Arial, Helvetica, sans-serif;
color
: #000000;
} 

3:Tableless forms

效果图——
web标准设计
Tableless forms
<form>
<label for=”name”>Name</label>
<input id=”name” name=”name”><br>
<label for=”address”>Address</label>
<input id=”address” name=”address”><br>
<label for=”city”>City</label>
<input id=”city” name=”city”><br>
</form>

label,input 
{
display
: block;
width
: 150px;
float
: left;
margin-bottom
: 10px;
}

label 
{
text-align
: right;
width
: 75px;
padding-right
: 20px;
}

br 
{
clear
: left;
} 

4:Double blockquote

效果图——
web标准设计
Double blockquote
blockquote:first-letter {
background
: url(images/open-quote.gif) no-repeat left top;
padding-left
: 18px;
font
: italic 1.4em Georgia, “Times New Roman”, Times, serif;
} 

5:Gradient text effect

效果图——
web标准设计
Double blockquote
<h1><span></span>CSS Gradient Text</h1>

h1 
{
font
: bold 330%/100% “Lucida Grande”;
position
: relative;
color
: #464646;
}
h1 span 
{
background
: url(gradient.png) repeat-x;
position
: absolute;
display
: block;
width
: 100%;
height
: 31px;
}

<!–[if lt IE 7]>
<style>
h1 span 
{
background
: none;
filter
: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=’gradient.png’, sizingMethod=’scale’);
}
</style>

6:Vertical centering with line-height

效果图——
web标准设计
Vertical centering with line-height
div{
height
:100px;
}
div *
{
margin
:0;
}
div p
{
line-height
:100px;
}
<p>Content here</p> 

7:Rounded corners with images

效果图——
web标准设计
Rounded corners with images
<div class=”roundcont”>
<div class=”roundtop”>
<img src=”tl.gif” alt=”"
width=”15″ height=”15″ class=”corner”
style=”display: none” />
</div>

CONTENT

<div class=”roundbottom”>
<img src=”bl.gif” alt=”"
width=”15″ height=”15″ class=”corner”
style=”display: none” />
</div>
</div>

.roundcont 
{
width
: 250px;
background-color
: #f90;
color
: #fff;
}

.roundcont p 
{
margin
: 0 10px;
}

.roundtop 
{
background
: url(tr.gif) no-repeat top right;
}

.roundbottom 
{
background
: url(br.gif) no-repeat top right;
}

img.corner 
{
width
: 15px;
height
: 15px;
border
: none;
display
: block !important;
}

8:Multiple class name

<img src="image.gif" class="class1 class2" alt="" />

.class1 
{ border:2px solid #666; }
.class2 
{
padding
:2px;
background
:#ff0;
} 

9:Center horizontally

效果图——
web标准设计
Center horizontally
<div id=”container”></div>
#container 
{
margin
:0px auto;
} 

10:CSS Drop Caps

效果图——
web标准设计
CSS Drop Caps
<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>

p.introduction:first-letter 
{
font-size 
: 300%;
font-weight 
: bold;
float 
: left;
width 
: 1em;
} 

11:Prevent line breaks in links, oversized content to brake

a{
white-space
:nowrap;
}

#main
{
overflow
:hidden;
}

12:Show firefox scrollbar, remove textarea scrollbar in IE

html{
overflow
:-moz-scrollbars-vertical;
}

textarea
{
overflow
:auto;
} 
posted @ 2008-03-28 16:13 阿一(杨正祎) 阅读(1793) 评论(14)  编辑 收藏 所属分类: Web标准

  回复  引用  查看    
#1楼 2008-03-28 18:30 | Colin Han      
Double blockquote 和 Gradient text effect似乎反了。
-------------
另外,这些都是兼用所有现代浏览器的吗?
  回复  引用  查看    
#2楼 2008-03-28 19:20 | Clark Zheng      
真棒!非常有用,马上就使,哈哈
  回复  引用  查看    
#3楼 2008-03-28 20:21 | 一岩一道      
非常棒!收藏了
  回复  引用  查看    
#4楼 2008-03-28 21:14 | 早班火车      
it's cool!
  回复  引用  查看    
#5楼 [楼主]2008-03-28 21:21 | 杨正祎(阿一)      
@Colin Han
谢谢提醒,已经修正。

另:这些都是基本的CSS应用,兼容性没有问题的。
  回复  引用  查看    
#6楼 [楼主]2008-03-28 21:25 | 杨正祎(阿一)      
@Clark Zheng
@一岩一道
@早班火车
的确,有些虽然不能算是技巧,但是却是非常实用的。
  回复  引用    
#7楼 2008-03-29 08:38 | 雕刻寂寞 [未注册用户]
cool!收藏
  回复  引用    
#8楼 2008-03-30 13:44 | cyg [未注册用户]
挺不错~~
  回复  引用    
#9楼 2008-03-30 21:00 | aresli [未注册用户]
第一条似乎就有问题
  回复  引用  查看    
#10楼 2008-04-02 16:23 | 李战      
这些CSS技巧非常不错。
俺会选择性地使用,有些有不能用了。比如第一条技巧,俺是坚决不用的!

为啥这么固执呢?

嗯,俺承认俺有洁癖,不愿意为了布局或效果,而在内容中引入额外的<b>标签。

俺就喜欢内容就是内容,干干净净的。式样表就是式样表,不去给内容添加麻烦。如果为了某些所谓的特效,整得内容和式样表串味了,网页也就不好吃了噻。

不串味的技巧,俺通通吃下。
  回复  引用  查看    
#11楼 2008-04-23 12:35 | 1-2-3      
收藏了。
  回复  引用  查看    
#12楼 2008-05-12 08:14 | 小哈      
收藏
  回复  引用  查看    
#13楼 2008-06-09 11:27 | 李婷      
--引用--------------------------------------------------
李战: <img src="http://www.cnblogs.com/Emoticons/qface/055243523.gif" alt="" />这些CSS技巧非常不错。<br />
俺会选择性地使用,有些有不能用了。比如第一条技巧,俺是坚决不用的!<br />
<br />
为啥这么固执呢?<br />
<br />
嗯,俺承认俺有洁癖,不愿意为了布局或效果,而在内容中引入额外的&lt;b&gt;标签。<br />
<br />
俺就喜欢内容就是内容,干干净净的。式样表就是式样表,不去给内容添加麻烦。如果为了某些所谓的特效,整得内容和式样表串味了,网页也就不好吃了噻。<br />
<br />
不串味的技巧,俺通通吃下。<img src="http://www.cnblogs.com/Emoticons/QQ/laf.gif" alt="" />

--------------------------------------------------------
同意!!
  回复  引用  查看    
#14楼 2008-07-21 17:20 | qin213      
不错~! 博客园真是个好地方, 大家都能分享一下就好了~
看不及了 要下班了 明天继续
51count统计