当前位置: 首页 > 图文教程 > 网站运营 > 建站经验 > 最常用和实用的CSS技巧

建站经验
网站运营:移动互联网手机阅读即将呈现井喷
电子商务网站流量分析的三个重要内容
网站运维:网站可用性
易名中国建议终止域名停放
开源CMS也有开源协议和适用范围
凤巢广告系统如何决定采用何种展示方案
Browser Size:网站的可用性和用户转化率参考
分享在国外域名注册的方法和技巧
你的网站在搜索引擎上能否表现优秀
B2C电子商务网站购物流程简化设计
建站经验分享:运营分类信息网站的经验
如何选择威客网站 好的威客网站具备的要素
危机面前不随意撤下友情链接 让我们共渡难关
免费电影网站主要盈利模式:在线广告联盟
做网站需要办理的各种许可证
软文写作:文章标题文章简介和文章内容
做一个网站需要在哪些方面投入
网页设计师设计网页时用方正字体需小心
当前形势下个人站长需要规避的一些风险
国内免费论坛为何变得落寞

建站经验 中的 最常用和实用的CSS技巧


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 50 ::
收藏到网摘: n/a

1.重置浏览器的字体大小
重置浏览器的默认值 ,然后重设浏览器的字体大小你可以使用雅虎的用户界面重置的CSS方案 ,如果你不想下载9MB的文件,代码如下:

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; }


其次,我们重设浏览器字体的大小为10像素,使用如下:

html {font-size: 62.5%;}


这个大小基本合适,然后您可以根据自己的需要调整大小,如 标题1为120像素:

h1 {font-size: 2em;}

 

2.设置水平居中
大多数的网站目前都是固定宽度的。CSS代码如下:

div#container {margin: 0 auto;}

 

3.控制位置:绝对位置,相对位置
假如有两个div

<div id='parent'>
<div id='son'></div>
</div>


div有left和top属性,是用来定位的.
如果内层的div的position属性是absolute.那他就是相对于文档的左上角的位置..
如果内层的div(id为son的那个)position属性为relative,那它的left和top值就是相对于外层的div的左上角的距离.


4.将重要元素放置在屏幕中央
如果你希望将您想要的东西放在最中央,可以使用以下CSS:

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


您必须明确的指定宽度和高度,再把top和left属性设为他们的一半,这样就可以是这个部分回到屏幕的中心。


5.可以重复利用的规则

.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;}


设置自己的CSS样式表,就可以在您需要的时候直接的添加标记即可。
 


6. 解决IE6 的浮动元素的双倍边距问题
对一个div设置了float:left 和 margin-left:100px 那么在IE6中,这个bug就会出现。您只需要多设置一个display即可,代码如下:

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

 


7.简单的导航菜单
在您的设计中预设一个导航栏是非常有益的。可以让别人对你网页的主要内容有一个大致的了解。第一次来的XHTML:

<div id=”navbar”>
<ul>
<li><a href=”http://www.peakflowdesign.com”>Peakflow Design</a></li>
<li><a href=”http://www.google.com”">Google</a></li>
<li><a 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的form表单
正如我们现在进行网站设计的table-free,把重点是放在使用DIVs上。不再对表的列和域进行约束,所以我们需要一些好用的CSS,在JeddHowden.com 发现

XHTML:
<form action=”form.php” method=”post”>
<fieldset>
<legend>Personal Information</legend>
<div>
<label for=”first_name”>First Name:</label>
<input type=”text” name=”first_name” id=”first_name” size=”10″ value=”" />
</div>
<div>
<label for=”last_name”>Last Name:</label>
<input type=”text” name=”last_name” id=”last_name” size=”10″ value=”" />
</div>
<div>
<label for=”postal”>Zip/Postal Code:</label>
<input type=”text” name=”postal” id=”postal” size=”10″ value=”" />
</div>
</fieldset>
</form>

 

CSS:
form div {clear:left;display:block;width:400px;zoom:1;margin:5px 0 0 0;padding:1px 3px;}
form div label {display:block;float:left;width:130px;padding:3px 5px;margin: 0 0 5px 0;text-align:right;}

 

9.让footer总是停留在页面的底部
在网页的底部总是保留着公司的版本信息,如何是这部分信息来实现呢?这是一个很古老的技术,这都要归功于The Man in Blue 。

XHTML:
<body>
<div id=”nonFooter”>
<div id=”content”> *Place all page content here* </div>
</div>
<div id=”footer”> *Place anything you want in your footer here*
</div>
</body>


CSS:
html, body { height: 100%; }
#nonFooter { position: relative; min-height: 100%; }
* html #nonFooter { height: 100%; }
#content { padding-bottom: 9em; }
#footer { position: relative; margin-top: -7.5em; }


10.在同一元素上使用多种类
随着有用的功能越来越多的,大多数的人都忽略了内部CSS的选择。一个元素可以套用很多的类,例如:

.red {color: red;}
.bold {font-weight: strong;}


我们可以运用它:

<p class=”red bold”>This text will be red yet also bold!</p>