Home  >  Article  >  Web Front-end  >  DIV CSS layout page rendering speed faster writing method_html/css_WEB-ITnose

DIV CSS layout page rendering speed faster writing method_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 12:28:431216browse

1、*{} #zishu *{} 尽量避开

由于不同浏览器对HTML标签的解释有差异,所以最终的网页效果在不同的浏览器中可能是不一样的,为了消除这方面的风险,设计者通常会在CSS的一个始就把所有标签的默认属性全部去除,

以达到所有签标属性值都统一的效果。所以就有了*通配符。*会遍历所有的标签;

*{margin:0; padding:0}
如果这样写,页面中所有的标签的margin全是0;padding也是0;

#zishu *{margin:0; padding:0}
如果这样写,在id等于zishu下边的所有标签的margin全是0;padding也是0;

这样写的问题是:

遍历会消耗很多的时间,如果你的HTML代码写的不规范或是某一签标没有必合,这个时间可能还会更长;
很多的标签本来就没有这个属性或属性本身就是统一的,那么更给设置一次,也有时间的开消;
建议的的解决办法:

不要去使用生僻的标签,因为这些标签往往在不同浏览器中解释出来的效果不一样;所以你要尽可能的去使用那些常用的标签;
不要使用*;而是把你常用到的这些标签进行处理;例如:body,li,p,h1{margin:0; padding:0}
2、滤镜的一些东西不要去用

IE的一些滤镜在FIREFOX中不支持,往往写一些效果时你还是使用CSS HACK;而滤镜是一个非常毫资源的东西;特别是一些羽化、阴影和一个前透明的效果;

例如一个阴影效果:


test




建议的解决办法:

能不使用就不要使用,一方面兼容问题;很多效果只能在IE中使用;
就本例而言,如果非要这样在的效果,建议用图片作背景;(只说优化速度,实际应用还是可以小部分用,有人可能会说,用图片还多一个HTTP请求呢,呵呵……)
一个非常好的例子,就是在今年512大地震时,很多网站一夜之间全部变成了灰色,他们只用了一行CSS代码:

body{filter: gray;}

但,你会看会看到这些网页非常的慢,打开后你的CPU也会飙升,不夸张的说,如果你的电脑配置差,干死你也不为过。

3、一个页面上少用绝对定位

绝对定位(position:absolute )是网页布局中很常用到的,特别是作一些浮动效果时,也会让页面看起来非常的酷。但网页中如果使用过多的绝对定位,会让你的网页变得非常的慢,这一

点上边FIREFOX表现要比IE还要差。

例如:



  • 001

  • 001

  • 001

  • ……

建议的解决办法:

尽可能少用,这个少用的值是多少,也没有一个非常好的值来说明;还要看绝定定位这个标签里边的内容的多少;在这里我只能说,这样写会有性能问题,少用。
如果能用变通实现同样的效果,就用变通的办法
4、background 背景图片的平铺

有些网页的背景或页面中某块的背景通常要用到图片的平铺,平铺后就会有平铺次数的问题,如果是单次还好,如果是多次,就废了。

举个简单的例子:

例一:滚动一下你的页面,看速度怎么样?


例二:同样效果,再试一下这个!


Note: Test the two effects above. The worse your computer is, the more obvious it will be. If your computer configuration is very good, you can try changing the 8000px to 9000000px. If it still doesn’t work, change it to a larger one. Some, don’t scold me when you’re in trouble!

Suggested approach:

Pictures with few colors should be made into gif pictures;
The tiled pictures should be as large as possible. If it is a GIF picture with few colors, the picture should be larger. The size will not be much larger; the two examples above prove it very well. The first picture has very few pictures, and the second picture is larger; but the speed is very different

;
5. Inherit as many attributes as possible

As much as possible, let some attributes inherit from the parent instead of overwriting the parent;

A simple example:

< ;style>
a:link,a:visited{color:#0000FF}
a:hover,a:active{color:#FF0000}
#zishu a:link,#zishu a:visited{ font-weight:bold}
#zishu a:hover,#zishu a:active{ font-style: italic;}

test


6. The CSS path should not be too deep;

For example:

#zishu #info #tool #sidebar h2{ font-size:12px;}

7. Some abbreviations Just abbreviate;

For example:

#zishu{pading-top:10px; padding-right:50px; padding-left:50px; padding-bottom:4px;}

Change to:

#zishu{padding:10px 50px 4px 50px}

This has no effect on rendering speed; it’s just a few fewer characters;

8. Don’t leave it empty class or no class in the HTML code;

9. Application of float

My feeling is that if used improperly, there will be performance problems 100%, and it is also very large , but I really don’t know how to make an example; here I can only suggest that if you don’t understand how float works, it’s better to use it less.

I once killed IE because of this. I wrote an example, although it has nothing to do with it: The death of IE__ turns out to be related to CSS

10. Reasonable Layout

Why do you say this? Reasonable layout can change the writing method and rendering process of CSS

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn