Home > WordPress > 如何给你的博客提速(2):优化css
2009June . 2nd

如何给你的博客提速(2):优化css

精简你的css文件
css效果图.jpg请先看图片,这是一个class为test的div元素。
一般我们都会用dreamweaver来编辑网页。用dreamweaver作出右图的效果,原始的css代码是这样的:

.test {
	margin: 2px;
	padding: 2px;
	height: 300px;
	width: 400px;
	border-top-style: dotted;
	border-right-style: double;
	border-bottom-style: groove;
	border-left-style: dashed;
	border-top-color: #F00;
	border-right-color: #F00;
	border-bottom-color: #F00;
	border-left-color: #F00;
}

上面的css代码,每设定一个样式,就会使用一个css标记。其实这样是很浪费的。
先看css标记的结构,是这样的:父样式-子样式-子子样式-……。其实在样式不太复杂的情况下,只需要按照一定的格式定义父样式,相应的效果就会赋予到对应的子样式上。是不是说得很差劲呢?举个例子。
一下是一个class为test2的元素的css

.test2 {
	border-top-width: 1px;
	border-right-width: 1px;
	border-bottom-width: 1px;
	border-left-width: 1px;
	border-top-style: dotted;
	border-right-style: dotted;
	border-bottom-style: dotted;
	border-left-style: dotted;
}

其实我们可以精简成这样

.test2 {
	border-top-width: 1px dotted;
}

border-top-width、border-right-width等字样式都整合进父样式border里了,是不是简单了很多呢?还不了解的童鞋可以把下面的文本保存成html格式,然后替换.test部分看一下。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
<!--
.test2 {
	border-top-width: 1px;
	border-right-width: 1px;
	border-bottom-width: 1px;
	border-left-width: 1px;
	border-top-style: dotted;
	border-right-style: dotted;
	border-bottom-style: dotted;
	border-left-style: dotted;
}
-->
</style>
</head>
 
<body>
<div class="test2">此处显示  class "test2" 的内容</div>
</body>
</html>

嗯,现在可以说回图片的例子了,刚才那段css是不是看着都头晕呢?我们可以改成这样。

.test {
	margin: 2px;
	padding: 2px;
	height: 300px;
	width: 400px;
	border-style: dotted double groove dashed;
	border:#F00;
}

这里css比较复杂,因此要分开定义border的线性和颜色。我们再变一下,把css改成下面这样子,看会有什么效果。

.test {
	margin: 2px;
	padding: 2px;
	height: 300px;
	width: 400px;
	border-style: dotted double groove dashed;
	border-color:#F00 #0F0 #00F #000;
}

css效果图2.jpg
你是否已经弄清楚定义四条边的顺序?其实定义ul、li、a等块状元素都是差不多的,举一反三吧。

忘了说主题。根据我的观察,相当多一部分修改过css的博主是没有精简过css,这样css文件就徒增了不少体积,页面打开就慢了。把能精简的都精简掉,简洁不简单,才是技术。

  1. 2009June . 2nd - 2:35 PM

    我来了,沙发是我的。

  2. 2009June . 2nd - 2:38 PM

    你可以做个专题,针对常用的分类,ul li em dd dt div 这些用的多。

  3. 2009June . 2nd - 2:59 PM

    @LAONB
    其实都是差不多的,都是那几句css

  4. 2009June . 2nd - 4:00 PM

    关键是在DW里 如果设置css就自动成这样了。
    以前知道一个小工具,可以使css横向排列!

  5. 2009June . 2nd - 4:13 PM

    CSS没多大
    关键是图片

  6. 2009June . 2nd - 5:30 PM

    精简一下还是好,毕竟多了的话,还是可以提高些速度的,虽然依然比较小。

  7. 2009June . 2nd - 5:56 PM

    :shock: 说白了,关键要让css多合一,才能快 =。= 不信可以试试

  8. 2009June . 2nd - 6:04 PM

    一说CSS就头晕。嗯。。

  9. 2009June . 2nd - 6:13 PM

    以最少的语句完成最多的效果.
    哈哈..貌似最近软件测试看多了^!^

  10. 2009June . 2nd - 7:12 PM

    我看了之后很迷失……@_@

  11. 2009June . 2nd - 8:15 PM

    看不懂哦看不懂。

  12. 2009June . 2nd - 10:07 PM

    看了还真有点头晕啊。要不我把我的css文件发给你,你帮我改一下?哈哈哈哈

  13. 2009June . 2nd - 10:38 PM

    没见谁分开写的,都是大小样式颜色等等写在一起的呀,这个一般人都懂吧

  14. 2009June . 2nd - 10:39 PM

    呃,一走进来发现菜单栏错位了,博主肯定还在修改吧

  15. 2009June . 2nd - 10:41 PM

    border:1px dotted #000;
    这样的更简单呀

  16. 2009June . 2nd - 11:02 PM

    @kerby
    没有错位,是我正在做的新效果
    @wulinfo
    有四种不同的线性,各自还有不同的颜色呢,混起来写的话好像会显示不出应有的效果

  17. 2009June . 2nd - 11:46 PM

    @seri CSS合并可以提高速度,是真的吗?

  18. 2009June . 3rd - 12:07 AM

    @我想想
    真的啊,用Yslow测试的结果大多都在多个css文件加载速度不一致,而导致网页速度慢,
    如果合并了,就可以一起加载,提高一倍啊以上!
    (再就减少js的输出,控制图片大小。完毕!=w= :shock:

  19. 2009June . 3rd - 9:01 AM

    还有上次bolo指出我的主题图片没有整合也是一个影响载入速度的因素哦~~

  20. 2009June . 3rd - 12:45 PM

    @bolo那就补充 border-top right left bottom

  21. 2009June . 3rd - 1:05 PM

    下载 速度加快!!

  22. 2009June . 4th - 10:33 PM

    多折腾,呵呵!!

  23. 2009June . 16th - 11:03 AM

    回一个,真的不错

  24. 2009June . 16th - 12:38 PM

Subscriber selector

Close