CSS缩写

作者:xinsea 发布时间:April 17, 2012 分类:前端开发

0长度
如果属性的值为:0单位(px、em等),可直接缩写为0。例如:0px直接缩写为0

Color(颜色)缩写
在色彩值用16进制的时候,如果每种颜色的值是相同的,就可以写成一个:
color:#000000; color:#113366;

可以简写成:
color:#000; color:#136;

所有用到16进制色彩值的地方都可以缩写,如:background-color、border-color等。

盒子大小
主要用于两个属性:margin和padding。通常有下面四种书写方法:
property:value1; 表示所有边都是一个值value1; property:value1 value2; 表示top和bottom的值是value1,right和left的值是value2 property:value1 value2 value3; 表示top的值是value1,right和left的值是value2,bottom的值是value3 property:value1 value2 value3 value4; 四个值依次表示top,right,bottom,left

我们以margin为例:
margin-top:1px; margin-right:1px; margin-botton:1px; margin-left:1px;

四个值可以缩写为:
margin:1px 1px 1px 1px;

缩写的顺序是上->右->下->左。顺时针的方向。如果相对的边的值是相同的,则可以省掉:
margin:1px; /* 上例: 四个方向的边距相同,等同于margin:1px 1px 1px 1px; */ margin:1px 2px; /* 上下边距都为1px,左右边距均为2px,等同于margin:1px 2px 1px 2px; */ margin:1px 2px 3px; /* 右边距和左边距相同,等同于margin:1px 2px 3px 2px; */ margin:1px 2px 1px 3px; /* 注意,这里虽然上下边距都为1px,但是这里不能缩写。*/

Border缩写
border有三个子属性:
border-width:数字+单位; border-style: none || hidden || dashed || dotted || double || groove || inset || outset || ridge || solid ; border-color: 颜色;

可以缩写为一句,语法是:border:width style color。
border:5px solid #369;

也可以对每个属性采用缩写:
border-width:1px 2px 3px; /* 最多可用四个值,缩写规则和盒子大小的缩写类似,下同 */ border-style:solid dashed dotted groove; border-color:red blue white black;

border默认的宽度是3px,默认的颜色是该规则中的color属性的值,而color默认是黑色的,border的缩写中border-style是必须的。

outline
outline类似border,不同的是border会影响盒模型,而outline不会。
outline-width:数字+单位; outline-style: none || dashed || dotted || double || groove || inset || outset || ridge || solid ; outline-color: 颜色 ;

缩写为:
outline:1px solid red;

Background缩写
背景的属性如下:
background-color: color || #hex || RGB(% || 0-255) || RGBa; background-image:url(); background-repeat: repeat || repeat-x || repeat-y || no-repeat; background-position: X Y || (top||bottom||center) (left||right||center); background-attachment: scroll || fixed;

background的简写可以大大的提高css的效率,缩写语法是background:color image repeat attachment position;
background:#fff url(img.png) no-repeat 0 0;

可以省略其中一个或多个属性值,如果省略,该属性值将用浏览器默认值,默认值为:
color: transparent image: none repeat: repeat attachment: scroll position: 0% 0%

字体(fonts)
font简写也是使用最多的一个,它也是书写高效的CSS的方法之一。
属性如下:
font-style: normal || italic || oblique; font-variant:normal || small-caps; font-weight: normal || bold || bolder || || lighter || (100-900); font-size: (number+unit) || (xx-small - xx-large); line-height: normal || (number+unit); font-family:name,"more names";

可以缩写为一句,语法是:font:font-style font-variant font-weight font-size/line-height font-family;

注意,如果你缩写字体定义,至少要定义font-size和font-family两个值,可参考下图手册:
030336t77.jpg

font的各个属性也都有默认值:
font-style: normal; font-variant:normal; font-weight: normal; font-size: inherit; line-height: normal; font-family:inherit;

列表(lists)
清除所有默认的列表样式,比如数字或者圆点:list-style:none;

list-style也有三个属性:
list-style-type:none || disc || circle || square || decimal || lower-alpha || upper-alpha || lower-roman || upper-roman list-style-position: inside || outside || inherit list-style-image: (url) || none || inherit

list-style的默认属性如下:
list-style:disc outside none

如果list-tyle中定义了图片,那么图片的优先级要比list-style-type高:
list-style:circle inside url(../img.gif)
例子中,如果img.gif存在,则不会显示前面设置的circle符号。

border-radius(圆角半径)
border-radius是css3中新加入的属性,用来实现圆角边框。Gecko(firefox)和webkit(safari/chrome)需分别使用私有前缀-moz-和-webkit-。
-moz-border-radius:0 6px 6px; -webkit-border-radius:0 6px 6px; border-radius:0 6px 6px;

参考:
前端观察:http://www.qianduan.net/css-font-shorthand-attribute-handbook.html
w3cn.org:http://www.w3cn.org/article/tips/2005/103.html

重新制作了Theme

作者:xinsea 发布时间:April 13, 2012 分类:前端开发

首先上图,有图有真像。
Laconic_theme.jpg

上个皮肤还是blog刚开通的时候制作的,一直没换,这几天心血来潮的又重新鼓捣了下,投入到HtML5的怀抱,也使用了一些CSS3属性,在IE6下不真是惨不忍睹。

一些细节还需要再修正一下。

2012年CSS裸奔节

作者:xinsea 发布时间:April 9, 2012 分类:前端开发

今天咱也过个节,过这个节日的目的有二:
1、提醒自己CSS样式对一个网站的重要性
2、更是提醒自己优良的HTML结构对一个网站来说也是相当的重要

注:
平时的工作中,也是经常刻意的去掉页面样式,来审视页面的HTML结构是否合理。

z-index 取值范围

作者:xinsea 发布时间:April 5, 2012 分类:前端开发

定义和用法
z-index 属性设置元素的堆叠顺序。拥有更高堆叠顺序的元素总是会处于堆叠顺序较低的元素的前面。

注释:
1、元素可拥有正(负)的 z-index 属性值。
2、Z-index 仅能在定位元素上奏效。如 position:absolute;

取值范围

ver. max min typeof(div.style.zIndex)
IE 8.0 2147483647 -2147483648 number
Chrome 3.0.195 2147483647 -2147483645 string
Firefox 3.0.195 2147483647 -2147483645 string
Opera 3.0.195 2147483647 -2147483645 string

需要兼容以上所有浏览器时,z-index的值需使用 -2147483584 ~ 2147483584 范围内的数字

让hasLayout元素设置display:inline后与inline-block类似

作者:xinsea 发布时间:March 21, 2012 分类:前端开发

.select { display:-moz-inline-stack; display:inline-block; *display:inline; *zoom:1; vertical-align:middle; position:relative; /* 解决因为父容器{display:inline;}问题导致Firefox中li里面的链接不可点的bug */ _position:static; /* 解决因为IE6中因为上一行代码带来在{position:relative}容器中移位在bug */ }