Home  >  Article  >  Web Front-end  >  CSS Super Tips Collection_CSS/HTML

CSS Super Tips Collection_CSS/HTML

WBOY
WBOYOriginal
2016-05-16 12:12:321291browse
一.使用css缩写

使用缩写可以帮助减少你CSS文件的大小,更加容易阅读。css缩写的主要规则请参看《常用css缩写语法总结》,这里就不展开描述。

二.明确定义单位,除非值为0

忘记定义尺寸的单位是CSS新手普遍的错误。在HTML中你可以只写width=100,但是在CSS中,你必须给一个准确的单位,比如:width:100px width:100em。只有两个例外情况可以不定义单位:行高和0值。除此以外,其他值都必须紧跟单位,注意,不要在数值和单位之间加空格。

三.区分大小写

当在XHTML中使用CSS,CSS里定义的元素名称是区分大小写的。为了避免这种错误,我建议所有的定义名称都采用小写。

class和id的值在HTML和XHTML中也是区分大小写的,如果你一定要大小写混合写,请仔细确认你在CSS的定义和XHTML里的标签是一致的。

四.取消class和id前的元素限定

当你写给一个元素定义class或者id,你可以省略前面的元素限定,因为ID在一个页面里是唯一的,鴆las s可以在页面中多次使用。你限定某个元素毫无意义。例如:

程序代码 程序代码
div#content { /* declarations */ }
fieldset.details { /* declarations */ }


可以写成

程序代码 程序代码
#content { /* declarations */ }
.details { /* declarations */ }


这样可以节省一些字节。

五.默认值

通常padding的默认值为0,background-color的默认值是transparent。但是在不同的浏览器默认值可能不同。如果怕有冲突,可以在样式表一开始就先定义所有元素的margin和padding值都为0,象这样:

程序代码 程序代码
* {
margin:0;
padding:0;
}


六.不需要重复定义可继承的值

CSS中,子元素自动继承父元素的属性值,象颜色、字体等,已经在父元素中定义过的,在子元素中可以直接继承,不需要重复定义。但是要注意,浏览器可能用一些默认值覆盖你的定义。

七.最近优先原则

如果对同一个元素的定义有多种,以最接近(最小一级)的定义为最优先,例如有这么一段代码

Update: Lorem ipsum dolor set
在CSS文件中,你已经定义了元素p,又定义了一个classupdate

程序代码 程序代码
p {
margin:1em 0;
font-size:1em;
color:#333;
}

.update {
font-weight:bold;
color:#600;
}


八.多重class定义

一个标签可以同时定义多个class。例如:我们先定义两个样式,第一个样式背景为"http://www.28600.com/article1/#"666;第二个样式有10 px的边框。

程序代码 程序代码
.one{width:200px;background:"http://www.28600.com/article1/#"666;}
.two{border:10px solid "http://www.28600.com/article1/#"F00;}


在页面代码中,我们可以这样调用:

程序代码 程序代码


这样最终的显示效果是这个div既有"http://www.28600.com/article1/#"666的背景,也有10px的边框。是的,这样做是可以的,你可以尝试一下。

九.使用子选择器(descendant selectors)

CSS初学者不知道使用子选择器是影响他们效率的原因之一。子选择器可以帮助你节约大量的class定义。我们来看下面这段代码:

程序代码 程序代码


这段代码的CSS定义是:

程序代码 程序代码
div#subnav ul { /* Some styling */ }
div#subnav ul li.subnavitem { /* Some styling */ }
div#subnav ul li.subnavitem a.subnavitem { /* Some styling */ }
div#subnav ul li.subnavitemselected { /* Some styling */ }
div#subnav ul li.subnavitemselected a.subnavitemselected { /* Some styling */ }


你可以用下面的方法替代上面的代码



样式定义是:

程序代码 程序代码
#subnav { /* Some styling */ }
#subnav li { /* Some styling */ }
#subnav a { /* Some styling */ }
#subnav .sel { /* Some styling */ }
#subnav .sel a { /* Some styling */ }


用子选择器可以使你的代码和CSS更加简洁、更加容易阅读。

十.不需要给背景图片路径加引号

为了节省字节,我建议不要给背景图片路径加引号,因为引号不是必须的。例如:

程序代码 程序代码
background:url(images/***.gif) #333;
可以写为:

程序代码 程序代码
background:url(images/***.gif) #333;


如果你加了引号,反而会引起一些浏览器的错误。

十一.组选择器(Group selectors)

当一些元素类型、class或者id都有共同的一些属性,你就可以使用组选择器来避免多次的重复定义。这可以节省不少字节。

例如:定义所有标题的字体、颜色和margin,你可以这样写:

程序代码 程序代码
h1,h2,h3,h4,h5,h6 {
font-family:Lucida Grande,Lucida,Arial,Helvetica,sans-serif;
color:#333;
margin:1em 0;
}


如果在使用时,有个别元素需要定义独立样式,你可以再加上新的定义,可以覆盖老的定义,例如:

程序代码 程序代码
h1 { font-size:2em; }
h2 { font-size:1.6em; }


十二.用正确的顺序指定链接的样式

当你用CSS来定义链接的多个状态样式时,要注意它们书写的顺序,正确的顺序是::link :visited :hover :active。抽取第一个字母是LVHA,你可以记忆成LoVe HAte(喜欢讨厌)。为什么这么定义,可以参考Eric Meyer的《Link Specificity》。

如果你的用户需要用键盘来控制,需要知道当前链接的焦点,你还可以定义:focus属性。:focus属性的效果也取决与你书写的位置,如果你希望聚焦元素显示:hover效果,你就把:focus写在:hover前面;如果你希望聚焦效果替代:hover效果,你就把:focus放在:hover后面。

十三.清除浮动

一个非常常见的CSS问题,定位使用浮动的时候,下面的层被浮动的层所覆盖,或者层里嵌套的子层超出了外层的范围。

通常的解决办法是在浮动层后面添加一个额外元素,例如一个div或者一个br,并且定义它的样式为clear: both。这个办法有一点牵强,幸运的是还有一个好办法可以解决,参看这篇文章《How To Clear Floats Without Structural Markup》(注:本站将尽快翻译此文)。

上面2种方法可以很好解决浮动超出的问题,但是如果当你真的需要对层或者层里的对象进行clear的时候怎么办?一种简单的方法就是用overflow属性,这个方法最初的发表在《Simple Clearing of Floats》,又在《Clearance》和《Super simple clearing floats》中被广泛讨论。

上面那一种clear方法更适合你,要看具体的情况,这里不再展开论述。另外关于float的应用,一些优秀的文章已经说得很清楚,推荐你阅读:《Floatutorial》、《Containing Floats》和《Float Layouts》

十四.横向居中(centering)

这是一个简单的技巧,但是值得再说一遍,因为我看见太多的新手问题都是问这个:CSS如何横向居中?你需要定义元素的宽,并且定义横向的margin,如果你的布局包含在一个层(容器)中,就象这样:



你可以这样定义使它横向居中:

程序代码 程序代码
#wrap {
width:760px; /* 修改为你的层的宽度 */
margin:0 auto;
}


但是IE5/Win不能正确显示这个定义,我们采用一个非常有用的技巧来解决:用text-align属性。就象这样:

程序代码 程序代码
body {
text-align:center;
}
#wrap {
width:760px; /* 修改为你的层的宽度 */
margin:0 auto;
text-align:left;
}


第一个body的text-align:center; 规则定义IE5/Win中body的所有元素居中(其他浏览器只是将文字居中) ,第二个text-align:left;是将#warp中的文字居左。

十五.导入(Import)和隐藏CSS
因为老版本浏览器不支持CSS,一个通常的做法是使用@import技巧来把CSS隐藏起来。例如:

程序代码 程序代码
@import url(main.css);

然而,这个方法对IE4不起作用,这让我很是头疼了一阵子。后来我用这样的写法:

程序代码 程序代码
@import main.css;

In this way, CSS can be hidden in IE4. Haha, it also saves 5 bytes. For a detailed explanation of @import syntax, you can see here "centricle's css filter chart"

16. Optimization for IE

Sometimes, you need to optimize IE Browser bugs define some special rules. There are too many CSS tricks (hacks) here. I only use two of them. Regardless of whether Microsoft supports CSS better in the upcoming IE7 beta version, these two All methods are the safest.

1. Comment method

(a) To hide a CSS definition in IE, you can use the child selector:

程序代码 Program code
html>body p {
/* Define content*/
}


(b) The following writing method can only be understood by IE browser (other browsers All hidden)

程序代码 Program code
* html p {
/* declarations */
}


(c) There are also times when you want IE/Win to be active and IE/Mac to be hidden, You can use the backslash trick:

程序代码 Program code
/* */
* html p {
declarations
}
/* */


2. Conditional comments (conditional comments) method

Another method, which I think is more testable than CSS Hacks, is to use Microsoft’s private attribute conditional comments (conditional comments). Using this method you can define some styles separately for IE without affecting the definition of the main style sheet. Like this:

程序代码 Program code


Seventeen. Debugging Tips: How big are the layers?

When an error occurs when debugging CSS, you have to be like a typewriter and analyze the CSS code line by line. I usually define a background color on the layer in question so it's obvious how much space the layer takes up. Some people suggest using border, which is generally possible, but the problem is that sometimes border will increase the size of elements, and border-top and boeder-bottom will destroy the vertical margin value, so it is safer to use background.

Another attribute that often causes problems is outline. Outline looks like boeder, but does not affect the size or position of the element. Only a few browsers support the outline attribute, the only ones I know of are Safari, OmniWeb, and Opera.

18. CSS code writing style

When writing CSS code, everyone has their own writing habits for indentation, line breaks, and spaces. After constant practice, I decided to adopt the following writing style:

程序代码 Program code
selector1,selector2 {property:value;}


When using union definitions, I usually write each selector on a separate line so that they are easier to find in the CSS file. Add a space between the last selector and the curly braces {. Also write each definition on its own line. The semicolon should be placed directly after the attribute value. Do not add spaces.

I am used to adding a semicolon after each attribute value. Although the rules allow you to leave out the semicolon after the last attribute value, it is easy to forget to add the semicolon when you want to add a new style. Error, so it’s better to add them all.

Finally, the closing brace} is written on a line by itself.

Spaces and line breaks aid reading
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