CSS学起来并不难,但在大型项目中,就变得难以管理,特别是不同的人在CSS书写风格上稍有不同,团队上就更加难以沟通,为此总结了一些如何实现高效整洁的CSS代码原则:

 

1. 使用Reset但并非全局Reset

  不同浏览器元素的默认属性有所不同,使用Reset可重置浏览器元素的一些默认属性,以达到浏览器的兼容。但需要注意的是,请不要使用全局Reset:

*{ margin:0; padding:0; }

  这不仅仅因为它是缓慢和低效率的方法,而且还会导致一些不必要的元素也重置了外边距和内边距。在此建议参考YUI ResetEric Meyer的做法。我跟Eric Meyer的观点相同,Reset并不是一成不变的,具体还需要根据项目的不同需求做适当的修改,以达到浏览器的兼容和操作上的便利性。我使用的Reset如下:

 

/** 清除内外边距 **/
body, h1, h2, h3, h4, h5, h6, hr, p,
blockquote, /* structural elements 结构元素 */
dl, dt, dd, ul, ol, li, /* list elements 列表元素 */
pre, /* text formatting elements 文本格式元素 */
form, fieldset, legend, button, input, textarea, /* form elements 表单元素 */
th, td, /* table elements 表格元素 */
img/* img elements 图片元素 */{
  border:medium none;
  margin: 0;
  padding: 0;
}
/** 设置默认字体 **/
body,button, input, select, textarea {
  font: 12px/1.5 '宋体',tahoma, Srial, helvetica, sans-serif;
}
h1, h2, h3, h4, h5, h6 { font-size: 100%; }
em{font-style:normal;}
/** 重置列表元素 **/
ul, ol { list-style: none; }
/** 重置超链接元素 **/
a { text-decoration: none; color:#333;}
a:hover { text-decoration: underline; color:#F40; }
/** 重置图片元素 **/
img{ border:0px;}
/** 重置表格元素 **/
table { border-collapse: collapse; border-spacing: 0; }

 

2. 良好的命名习惯

  无疑乱七八糟或者无语义命名的代码,谁看了都会抓狂。就像这样的代码:

.aaabb{margin:2px;color:red;}

  我想即使是初学者,也不至于会在实际项目中如此命名一个class,但有没有想过这样的代码同样是很有问题的:

<h1>My name is <span class="red blod">Wiky</span></h1>

  问题在于如果你需要把所有原本红色的字体改成蓝色,那修改后就样式就会变成:

.red{color:bule;}

  这样的命名就会很让人费解,同样的命名为.leftBar的侧边栏如果需要修改成右侧边栏也会很麻烦。所以,请不要使用元素的特性(颜色,位置,大小等)来命名一个class或id,您可以选择意义的命名如:#navigation{...},.sidebar{...},.postwrap{...}

  这样,无论你如何修改定义这些class或id的样式,都不影响它跟HTML元素间的联系。

  另外还有一种情况,一些固定的样式,定义后就不会修改的了,那你命名时就不用担忧刚刚说的那种情况,如

.alignleft{float:left;margin-right:20px;}
.alignright{float:right;text-align:right;margin-left:20px;}
.clear{clear:both;text-indent:-9999px;}

  那么对于这样一个段落

<p class="alignleft">我是一个段落!</p>

  如果需要把这个段落由原先的左对齐修改为右对齐,那么只需要修改它的className就为alignright就可以了。

 

3. 代码缩写

  CSS代码缩写可以提高你写代码的速度,精简你的代码量。在CSS里面有不少可以缩写的属性,包括margin,padding,border,font,background和颜色值等,如果您学会了代码缩写,原本这样的代码:

 

li{
    font-family:Arial, Helvetica, sans-serif;
    font-size: 1.2em;
    line-height: 1.4em;
    padding-top:5px;
    padding-bottom:10px;
    padding-left:5px;
}

 

就可以缩写为:

li{
    font: 1.2em/1.4em Arial, Helvetica, sans-serif;
    padding:5px 0 10px 5px;
}

  如果您想更了解这些属性要怎么缩写,可以参考《常用CSS缩写语法总结》或者下载CSS-Shorthand-Cheat-Sheet.pdf 。

 

4. 利用CSS继承

  如果页面中父元素的多个子元素使用相同的样式,那最好把他们相同的样式定义在其父元素上,让它们继承这些CSS样式。这样你可以很好的维护你的代码,并且还可以减少代码量。那么本来这样的代码:

#container li{ font-family:Georgia, serif; }
#container p{ font-family:Georgia, serif; }
#container h1{font-family:Georgia, serif; }

就可以简写成:

#container{ font-family:Georgia, serif; }

 

5. 使用多重选择器

  你可以合并多个CSS选择器为一个,如果他们有共同的样式的话。这样做不但代码简洁且可为你节省时间和空间。如:

h1{ font-family:Arial, Helvetica, sans-serif; font-weight:normal; }
h2{ font-family:Arial, Helvetica, sans-serif; font-weight:normal; }
h3{ font-family:Arial, Helvetica, sans-serif; font-weight:normal; }

可以合并为

h1, h2, h3{ font-family:Arial, Helvetica, sans-serif; font-weight:normal; }

 

6. 适当的代码注释

代码注释可以让别人更容易读懂你的代码,且合理的组织代码注释,可使得结构更加清晰。你可以选择做的样式表的开始添加目录:

/*------------------------------------
    1. Reset
    2. Header
    3. Content
    4. SideBar
    5. Footer
  ----------------------------------- */

  如此你代码的结构就一目了然,你可以容易的查找和修改代码。

  而对于代码的主内容,也应适当的加以划分,甚至在有必要的地方在对代码加以注释说明,这样也有利于团队开发:

/***	Header	***/ 
#header{ height:145px; position:relative; }
#header h1{ width:324px; margin:45px 0 0 20px; float:left;  height:72px;}

/***	Content	***/
#content{ background:#fff; width:650px; float:left; min-height:600px; overflow:hidden;}
#content h1{color:#F00}/* 设置字体颜色 */
#content .posts{ overflow:hidden; }
#content .recent{ margin-bottom:20px; border-bottom:1px solid #f3f3f3; position:relative; overflow:hidden; }

/***	Footer	***/
#footer{ clear:both; padding:50px 5px 0; overflow:hidden;}
#footer h4{ color:#b99d7f; font-family:Arial, Helvetica, sans-serif; font-size:1.1em; }

 

7. 给你的CSS代码排序

如果代码中的属性都能按照字母排序,那查找修改的时候就能更加快速:

/*** 样式属性按字母排序 ***/
div{
    background-color:#3399cc;
    color:#666;
    font:1.2em/1.4em Arial, Helvetica, sans-serif;
    height:300px;
    margin:10px 5px;
    padding:5px 0 10px 5px;
    width:30%;
    z-index:10;
}

 

8. 保持CSS的可读性

  书写可读的CSS将会使得更容易查找和修改样式。对于以下两种情况,哪种可读性更高,我想不言而明。

/*** 每个样式属性写一行 ***/
div{
    background-color:#3399cc;
    color:#666;
    font: 1.2em/1.4em Arial, Helvetica, sans-serif;
    height:300px;
    margin:10px 5px;
    padding:5px 0 10px 5px;
    width:30%;
    z-index:10;
}

/*** 所有的样式属性写在同一行 ***/
div{ background-color:#3399cc; color:#666; font: 1.2em/1.4em Arial, Helvetica, sans-serif;  height:300px; margin:10px 5px; padding:5px 0 10px 5px; width:30%; z-index:10; }

当对于一些样式属性较少的选择器,我会写到一行:

 

/*** 选择器属性少的写在同一行 ***/
div{ background-color:#3399cc; color:#666;}

 

对于这个规则并非硬性规定,但无论您采用哪种写法,我的建议是始终保持代码一致。属性多的分行写,属性少于3个可以写一行。

 

9. 选择更优的样式属性值

  CSS中有些属性采用不同的属性值,虽然达到的效果差不多,当性能上却存在着差异,如

  区别在于border:0把border设为0px,虽然在页面上看不见,但按border默认值理解,浏览器依然对border-width/border-color进行了渲染,即已经占用了内存值。
  而border:none把border设为“none”即没有,浏览器解析“none”时将不作出渲染动作,即不会消耗内存值。所以建议使用border:none;

  同样的,display:none隐藏对象浏览器不作渲染,不占用内存。而visibility:hidden则会。

 

10. 使用<link>代替@import

首先,@import不属于XHTML标签,也不是Web标准的一部分,它对于较早期的浏览器兼容也不高,并且对于网站的性能有某些负面的影响。具体可以参考《高性能网站设计:不要使用@import》。所以,请避免使用@import

 

11. 使用外部样式表

  这个原则始终是一个很好的设计实践。不单可以更易于维护修改,更重要的是使用外部文件可以提高页面速度,因为CSS文件都能在浏览器中产生缓存。内置在HTML文档中的CSS则会在每次请求中随HTML文档重新下载。所以,在实际应用中,没有必要把CSS代码内置在HTML文档中:

<style type="text/css" >
    #container{ .. }
    #sidebar{ .. }
</style>

或

<li style="font-family:Arial, helvetica, sans-serif; color:#666; " >

而是使用<link>导入外部样式表:

<link rel="stylesheet" type="text/css" href="css/styles.css" />

 

12. 避免使用CSS表达式(Expression)

  CSS表达式是动态设置CSS属性的强大(但危险)方法。Internet Explorer从第5个版本开始支持CSS表达式。下面的例子中,使用CSS表达式可以实现隔一个小时切换一次背景颜色:

background-color: expression( (new Date()).getHours()%2 ? "#B8D4FF" : "#F08A00" );

  如上所示,expression中使用了JavaScript表达式。CSS属性根据JavaScript表达式的计算结果来设置。

  表达式的问题就在于它的计算频率要比我们想象的多。不仅仅是在页面显示和缩放时,就是在页面滚动、乃至移动鼠标时都会要重新计算一次。给CSS表达式增加一个计数器可以跟踪表达式的计算频率。在页面中随便移动鼠标都可以轻松达到10000次以上的计算量。

  如果必须使用CSS表达式,一定要记住它们要计算成千上万次并且可能会对你页面的性能产生影响。所以,在非不得已,请避免使用CSS表达式。

 

13. 代码压缩

  当你决定把网站项目部署到网络上,那你就要考虑对CSS进行压缩,出去注释和空格,以使得网页加载得更快。压缩您的代码,可以采用一些工具,如YUI Compressor

利用它可精简CSS代码,减少文件大小,以获得更高的加载速度。

 

14. 总结

  在本文中,我力图更详尽的总结书写更高效的CSS代码的原则,但鉴于本人见识和精力有限,我还是希望这些原则能帮助您更好的书写和管理您的CSS代码,不知您又是如何书写CSS的,是否也有一些想要分享的技巧?给我留言吧谢谢~

本文地址:http://www.cnblogs.com/wiky/articles/better-css-code.html

PS:本文由维奇总结,如有转载请注明出处,谢谢!

 

参考资料:

WRITE BETTER CSS WITH BEST PRACTICES

10 Tips for Writing Better CSS

5 Ways to Instantly Write Better CSS

 
译文地址:http://coolshell.cn/articles/1245.html
这个网页(http://haslayout.net/css/index)上例举了所有的IE和CSS相关的BUG。如果你在开发网页的时候,你需要看看。

目前,这个网站上包含了 28 个“普通的Bug”4 个“布局方面的Bug”6 个“可以绕开的Bug” 以及 1 个“IE崩溃的Bug”,所有的这些Bug有39个指南和48个解决方法。这个列表目前更新到 2009年8月11日,19:50:22

下面是所有的bug列表,你可以点击每个BUG名的链接查看更详细的说明。

普通Bug

这部分 IE 的 bug 是比较普通的无法归到其它种类,或是同时属于多个种类的Bug。

名称 IE的版本 描述
Hover White Background Ignore Bug IE7 background 不会因为 :hover而改变
IE7 Child Selector Comment Bug IE7 一个 selector 包含了一个子的selector,如果后面跟着一个注释,则会被完全忽略。
Star HTML Bug IE6 * html selector 在 IE6 中没有被忽略
IE6 !important Ignore Bug IE6 !important 关键字会忽略,如果有相同的属性被设置了
PNG Image and Background Color Mismatch IE8 及以下版本 背景颜色和指定的图片的颜色不一致。而他们本来是一致的。IE认为这是他一个Feature。太可笑了。
No Auto Margin Center Pseudo-Bug IE8 及以下版本 如果把margins 设置成 `auto` ,IE不会把组件放置在中间的位置。所有的浏览器都会,只有IE不会。
:first-line !important Rule Ignore Bug IE8 如果在伪class :first-line 内使用!important,那么其所有定义会被忽略。
:first-letter Ignore Bug IE6 整个:first-letter 的属性定义会被除数完全忽略。
:first-letter !important Rule Ignore Bug IE8 如果在伪class :first-letter内使用!important,那么其所有定义会被忽略。
Partial Click Bug v2 IE8以 设置了整个区域是可以点击的,但在IE中只有文本可以点击。
Staircase Bug below IE8 浮动的元素排序起来就像一个楼梯。
Disappearing List Background Bug IE6 B <li>, <dt>, <dd> 没有背景。
noscript Ghost Bug IE8 and below <noscript> 标识中只有 borders/background 才有用。
No Transparency Click Bug IE8 and below 背景透明的图片在作为链接时,并且其“filter”被设置成了PNG透明,但其背景还是不可点击。
List Drop Shift Bug IE8 在<li>中的内容被换行了。
No Increase on <ol> Numbers Bug below IE8 <ol> 中的 <li> 列表序号不会增加。
No Bullets on <ul> and <ol> Bug below IE8 在<ul> 和 <ol> 中看不到列表序号/数字了。
No line-height Vertical Center on Images Bug IE8以下版 图片使用line-height 方法不能垂直居中
No Background Image Bug IE8及以下版 在IE中使用background无法定义背景图
Custom Cursor Bug IE8及以下版 自定义鼠标不工作
Leaking Background Bug IE6 背景从一个元件的内部溢出到外部
Expanding Height Bug IE6 元件的高度比指定的要长得多。
Expanding Width Bug IE6 元件的宽度比指定的要长得多。
Double Margin Bug IE6 float元件的左和右的空白(margins)被加倍了。
Negative Margin Bug IE8以下版 如果使用负数来指定页白(margins)里面的元件会被外面的元件所遮挡。
Italics Float Bug IE6 float的元件中的字体会被设置成倾斜。
3px Gap Bug aka Text Jog Bug IE6 下一个float的元件不是有一个3px的空隙,就是被换行了。
Text-Align Bug IE8以下版 text-align属性会影响整个元件内的所有内容。

布局类 Bug


名称 IE的版本 描述
Border Chaos Bug IE6 连框显示是混乱的
Sub-Hover Bug IE6 一些selectors 如 a:hover foo{} 无法正常工作
Partial Click Bug IE6 在定义了display: block的链接中(<a>) 只有文本是可以点的。
Disappearing Content Bug IE6 当我们滚动窗口的时候,或是最大化最小化窗品的时候,有一些内容会重复显示。

不支持的功能

名称 IE的版本 描述
No Child Selector Support Workaround IE6 子 selector 无效
Max-Height Workaround IE6 max-height 无效
Max-Width Workaround IE6 max-width 无效
Opacity IE8及以下版 opacity 属性无效
Min-Width Workaround IE6 min-width 属性无效
Min-Height Workaround IE6 min-height 属性无效

程序崩溃 Bug

这个BUG可以导致整个 IE 崩溃。

名称 IE的版本 描述
Hover Crash Bug IE6 当你把鼠标移上 :hover 的链接时,浏览器会崩溃

(全文完)

 

Alright, HTML5 has been released for quite a while now, and its capabilities never cease to amaze me. Here, I have got 10 more HTML and Javascript demonstrations that show you what they can do. Back to few years ago, implementation of these kind of animations are simply impossible and only can be done with Flash, but now, look at them, impressive animations and effects!

I have done a few posts before this, just in case you've missed the previous display of the awesomeness of HTML5 and Javascript, here is the lists, make sure you check them out:

Personally, I like Blob, keylight and noise field a lot, what do you think?

  • Breathing Galaxies
    Breathing Galaxies
    Hypotrochoid with dynamically changing color and diameter. Use the keyboard to change shapes mid-stream, or move the mouse to create a new shape.
  • Noise Field
    Noise Field
    Particle trails via Perlin noise. Move mouse to change particle motion. Click to randomize parameters.
  • Keylight
    Keylight
    A playhead travels between keys which resonate in different sounds depending on where they are placed.
  • Swirling Tentacles
    Swirling Tentacles
    Pulsing 3d wires with color cycling and camera tweening, in 1k of JavaScript.
  • FlowerPower
    FlowerPower
    Another experiment inspired by nature - simple drawing tool with flowers as brushes using bezier curves.
  • Blob
    Blob
    A soft blob that can be thrown around the screen so that it bounces against the browser window. The blob can also be split (double click) into many blobs, the smaller blobs will merge into a single bigger blob if they collide.
  • Rotating Spiral
    Rotating Spiral
    You are getting sleepy, very sleepy.... A resolution independent rotating spiral – simple, but visually compelling. Click to start, stop, and reverse the rotation.
  • Magnetic
    Magnetic
    Magnetic points are used to control a flow of particles. If there are many particles orbiting a magnet, it will radiate color and increase in size.
  • Trail
    Trail
    Colorful particles follow and rotate around the mouse position to generate an organic trail which then fades out slowly.
  • Graph Layout
    Graph Layout
    An interactive force-directed graph layout.

原文:10 Jaw Dropping HTML5 and Javascript Effects
 

Home Blog Delicious Github Flickr About Contact

© Miclle.Zheng . Powered by Forest Chalet