Sam Lin

一天一个脚印,一步一步向前走
Nothing in the world can take the place persistence!
posts - 145, comments - 149, trackbacks - 1, articles - 2
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

10种有用的CSS技巧

Posted on 2008-08-09 23:52 Sam Lin 阅读(3750) 评论(7)  编辑 收藏 所属分类: Web2.0

 

  1. 重置浏览器的默认设置

          首先将以下代码加到你的全局css文件中:

/*1.Reset browser defaults then reset the browser font size*/
            body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,
            blockquote,th,td 
{margin:0; padding:0; }
            table 
{ border-collapse:collapse; border-spacing:0; }
            fieldset,img 
{ border:0; }
            address,caption,cite,code,dfn,em,strong,th,var 
{ font-style:normal; font-weight:normal; }
            ol,ul 
{ list-style:none; }
            caption,th 
{ text-align:left; }
            h1,h2,h3,h4,h5,h6 
{ font-size:100%; font-weight:normal; }
            q:before,q:after 
{ content:; }
            abbr,acronym 
{ border:0; }
            html 
{font-size: 62.5%;}

 

然后,我们把字体大小font-size设为10px如:

 

html {font-size: 62.5%;}

 

这样我们就比较容易的通过em单位来设置页面字体的大小了,e.g

 

Code

 

     2.水平居中(仅限固定宽度)

          

     这个我想大家都会知道的,如:

    

div#container {margin: 0 auto;}

 

 

     3.充分利用position中的absolute和relatively

 

     4.居中,还是居中(纵横通杀)

 

     

div.popup { height:400px; width:500px; position: absolute; top: 50%; left: 50%;}
div.popup 
{ margin-top: -200px; margin-left: -250px;}

 

     Memo:

               ·必须指定width和height的固定值;

               ·position值为absolute;

               ·分别指定top与left为50%;

               ·设置margin-top和margin-left分别height和width的负值的一半,也可以简写为:margin:-200px 0 0 -250px;

 

     5.CSS也能复用

 

      建立你的global css rules,充分在任何可以用到的页面用它吧,e.g

     

.left {float: left;}
.right 
{float: right;}
img .left 
{ border:2px solid #aaaaaa; margin: 0 10px 0 0;}
img .right 
{ border:2px solid #aaaaaa; margin: 0 0 0 10px; padding: 1px;}

 

 

     6.IE6的margin bug

     

     当你设置div元素的float属性并指定了margin-left属性,在IE6或以下版本浏览的时候你可以会太跌眼镜,IE6怎么给你愉愉地加了margin的值呢?

     解决方法:把div设为内联的就行了 e.g

 

     

div {float:left;margin:40px;display:inline;}

 

 

     7.简单的导航菜单

     xhtml:

     

 

<div id="navbar">
        
<ul>
        
<li><href=http://www.peakflowdesign.com>Peakflow Design</a></li>
        
<li><href=http://www.google.com>Google</a></li>
        
<li><href="http://zenhabits.net/">Zen Habits</a></li>
        
</ul>
        
</div>

 

     CSS:

          

            #navbar ul li {display:inline;margin:0 10px 0 0;}
            #navbar ul li a 
{color: #333;display:block;float:left;padding:5px;}
            #navbar ul li a:hover 
{background:#eee;color:black;}

 

 

     8. 尽量减少使用table来布局

 

     9.页面元素可以有多个class, e.g

 

     

<div class="regColor bigSize">I am a reg color of font width big size!</div>

 

 

     orginal article:http://www.peakflowdesign.com/design/10-useful-css-tricks-to-conquer-the-world/

 

 

转载请注明出处[http://samlin.cnblogs.com/



Feedback

#1楼   回复  引用  查看    

2008-08-10 07:12 by 金色海洋(jyk)      
弱弱的问一下,talbe标签里面有一个 rules="all" 的属性,把他改成css的形式,要怎么写呀。

在做数据列表的时候,我想完全通过css来控制table。

#2楼   回复  引用  查看    

2008-08-10 10:24 by 火无极      
翻译的很不错,好像漏了一个

9.让页脚连接到浏览器的底部
直接贴原文啊!~呵呵
This CSS hook allows you to ensure your footer stays at the bottom of the browser window. Here is the XHTML:
1<body>
2<div id=”nonFooter”>
3<div id=”content”> *Place all page content here* </div>
4</div>
5<div id=”footer”> *Place anything you want in your footer here*
6</div>
7</body>

Followed by the CSS:
1html, body { height: 100%; }
2#nonFooter { position: relative; min-height: 100%; }
3* html #nonFooter { height: 100%; }
4#content { padding-bottom: 9em; }
5#footer { position: relative; margin-top: -7.5em; }

#3楼   回复  引用  查看    

2008-08-10 10:52 by Myhsg      
MARK下
用到时候再看:-)

#4楼[楼主]   回复  引用  查看    

2008-08-10 11:38 by Sam Lin      
@金色海洋(jyk)
通常我这样写的
<style type="text/css">
.rule,.rule td
{
border:1px solid #6595d6;
margin:0;padding:0;
border-collapse:collapse;
}
</style>

<table width="200" class="rule" >
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>

#5楼[楼主]   回复  引用  查看    

2008-08-10 13:08 by Sam Lin      
@火无极
是的,THX!^_^

#6楼   回复  引用  查看    

2008-10-27 17:05 by lostmyself      
Mark~~!
tanks

#7楼[楼主]   回复  引用  查看    

2008-10-27 22:06 by Sam Lin      
@lostmyself
:)
发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 1264491


相关文章:

相关链接: