CSS 的简写属性

原文地址 http://www.mark.ah.cn

定义

简写属性是可以让你同时设置其他几个 CSS 属性值的 CSS 属性。使用简写属性,Web 开发人员可以编写更简洁、更具可读性的样式表,节省时间和精力。

CSS 规范定义简写属性的目的在于将那些关于同一主题的常见属性的定义集中在一起。比如 CSS 的 background 属性就是一个简写属性,它可以定义 background-color、background-image、background-repeat 和 background-position 的值。类似地,最常见的字体相关的属性可以使用 font 的简写,盒子(box)各方向的外边距(margin) 可以使用 margin 这个简写。

1. Font 属性

font-family,font-style,font-variant,font-weight,font-size,font

font-family(字体族): “Arial”、“Times New Roman”、“宋体”、“黑体”等;

font-style(字体样式): normal(正常)、italic(斜体)或oblique(倾斜);

font-variant (字体变化): normal(正常)或small-caps(小体大写字母);

font-weight (字体浓淡):是normal(正常)或bold(加粗)。有些浏览器甚至支持采用100到900之间的数字(以百为单位);

font-size(字体大小): 可通过多种不同单位(比如像素或百分比等)来设置, 如:12xp,12pt,120%,1em

如果用 font 属性的话,就可以把几个样式进行简化,减少书的情况;font 属性的值应按以下次序书写(各个属性之间用空格隔开):

字体样式   | 字体变化     | 字体浓淡    | 字体大小  | 字体族

font-style | font-variant | font-weight | font-size | font-family

例如:

.text{

font-style:italic;

font-variant:normal;

font-weight:bold;

font-size:30px;

font-family:arial, sans-serif; 

}

上面的样式简写为:

.text{font:italic normal bold12pxarial,verdana;}

2. Background 属性

background-color:#999999; //元素的背景色

background-image : url("bg-img.png"); //设置背景图像

background-repeat : repeat-x | repeat-y | repeat | no-repeat; //设置重复方式

background-attachment : fixed | scroll; //设置背景图片的固定方式

background-position : X轴坐标,Y轴坐标[top,bottom,center,left,right,20px,10%];

//设置背景的左上角位置,坐标可以是以百分比或固定单位

background  可以用这个属性把前面几个综合起来进行简写

background 各个值的次序依次如下:

背景色| 背景图 | 背景图重复方式 | 背景图固定方式 | 背景图坐标

background-color | background-image  | background-repeat | background-attachment | background-position

如果省略某个属性不写出来,那么将自动为它取缺省值。

比如,如果去掉background-attachment和background-position的话:

background:#FFCC66url("bg-img.png") no-repeat;

例如:

.bg{background-color:#FFCC66;background-image:url("bg-img.png");background-repeat:no-repeat;background-attachment:fixed;background-position:left top;}

上面可以简写为:

.bg{background:#FFCC66url("bg-img.png")  no-repeat  fixed  left  top; }

3. Border 属性

border:[border-width ||border-style ||border-color |inherit];

border-width:1px;border-style:solid;border-color:#000;

可以简写成 border:1pxsolid#000;

属性

border-top:1pxsolid#000;

border-right:2pxsolid#000;

border-bottom:3pxsolid#000;

border-left:4pxsolid#000;

可以简写成

border:1px solid#0002px solid #0003px solid #0004px solid #000;

border-style取值

none

No border.

*hidden

Same as 'none', but in the collapsing border model, also inhibits any other border (see the section on border conflicts).

dotted

The border is a series of dots.

dashed

The border is a series of short line segments.

solid

The border is a single line segment.

double

The border is two solid lines. The sum of the two lines and the space between them equals the value of 'border-width'.

groove

The border looks as though it were carved into the canvas.

ridge

The opposite of 'groove': the border looks as though it were coming out of the canvas.

*inset

In the separated borders model, the border makes the entire box look as though it were embedded in the canvas. In the collapsing border model, drawn the same as 'ridge'.

*outset

In the separated borders model, the border makes the entire box look as though it were coming out of the canvas. In the collapsing border model, drawn the same as 'groove'.

4. border-radius 圆角属性

border-top-left-radius    

border-top-right-radius    

border-bottom-right-radius    

border-bottom-left-radius

类似边框border属性
取值 [<length>  |<percentage>  ]{1,2}

border-top-left-radius:5px30%;

border-top-right-radius:5px30%;

border-bottom-right-radius:5px30%;

border-bottom-left-radius:5px30%;

border-radius:5px30%;

如果实现一个圆可以

div{width:100px;height:100px;border:2pxsolid red;border-radius:50%;background:#00ffff;}

5. Margin 和 Padding 属性

注意,值是从 top 顺时针开始的:top、right、bottom、接着是 left

上  | 右 | 下  | 左

padding-top | padding-right | padding-bottom | padding-left

margin-top  | margin-right  | margin-bottom  | margin-left

padding : 1px 2px 3px 4px;

margin : 1px 2px 3px 4px;

6. border-image 属性

引入图片

border-image-source

border-image-source:url(image url);

切割引入的图片

border-image-slice

border-image-slice: [<number>  |<percentage> ]{1,4}&& fill?

边框的宽度

border-image-width

border-image-width: [<length>  |<percentage>  |<number>  | auto ]{1,4}

图片的排列方式

border-image-repeat

border-image-repeat: [ stretch | repeat | round ]{1,2}

border-image对表格table的影响

https://drafts.csswg.org/css-backgrounds-3/#border-image-tables

https://www.w3.org/TR/2011/REC-CSS2-20110607/tables.html#border-conflict-resolution

7. transition 过渡

transition 是一个简写属性,可设置 transition-property, transition-duration, transition-timing-function,  transition-delay。 transition用来定义元素在两种状态之间的过渡。不同状态可以用:hover 、:active之类的 pseudo-classes 定义,或者使用 JavaScript 动态设置。

几种动画效果

ease:(逐渐变慢)默认值,ease函数等同于贝塞尔曲线(0.25, 0.1, 0.25, 1.0).

linear:(匀速),linear 函数等同于贝塞尔曲线(0.0, 0.0, 1.0, 1.0).

ease-in:(加速),ease-in 函数等同于贝塞尔曲线(0.42, 0, 1.0, 1.0).

ease-out:(减速),ease-out 函数等同于贝塞尔曲线(0, 0, 0.58, 1.0).

ease-in-out:(加速然后减速),ease-in-out 函数等同于贝塞尔曲线(0.42, 0, 0.58, 1.0)

cubic-bezier:(该值允许你去自定义一个时间曲线), 特定的cubic-bezier曲线。 (x1, y1, x2, y2)四个值特定于曲线上点P1和点P2。所有值需在[0, 1]区域内,否则无效。

其是cubic-bezier为通过贝赛尔曲线来计算“转换”过程中的属性值,如下曲线所示,通过改变P1(x1, y1)和P2(x2, y2)的坐标可以改变整个过程的Output Percentage。初始默认值为default.

样式

a{

-moz-transition:background0.5sease-in,color0.3sease-out;

-webkit-transition:background0.5sease-in,color0.3sease-out;

-o-transition:background0.5sease-in,color0.3sease-out;

transition:background0.5sease-in,color0.3sease-out;

}

p{

-webkit-transition:all .5sease-in-out1s;

-o-transition:all .5sease-in-out1s;

-moz-transition:all .5sease-in-out1s;

transition:all .5sease-in-out1s;

}

8. list-style 属性

CSS list-style 属性是设置list-style-type, list-style-image 和 list-style-position  的简写属性。

list-style-type取值

none

disc 实心圆点 (默认值)

circle 空心圆点

square 实心方块

decimal 十进制阿拉伯数字 从1开始

cjk-decimal 中日韩十进制数 例如: 一, 二, 三, ..., 九八, 九九, 一〇〇

decimal-leading-zero Decimal numbers Padded by initial zeros E.g. 01, 02, 03, … 98, 99

lower-roman Lowercase roman numerals E.g. i, ii, iii, iv, v…

...

list-style-position

取值

outside 标记盒在主块盒的外面。

inside 标记盒是主要块盒中的第一个行内盒,处于元素的内容流之后。

参考文献

https://developer.mozilla.org/zh-CN/docs/Web/CSS/Shorthand_properties

http://blog.csdn.net/shenzhennba/article/details/7356095

https://drafts.csswg.org/css-backgrounds-3/#the-border-shorthands

http://www.w3cplus.com/content/css3-border-image

http://www.zhangxinxu.com/wordpress/2010/01/css3-border-image/

https://developer.mozilla.org/zh-CN/docs/Web/CSS/border

https://developer.mozilla.org/zh-CN/docs/Web/CSS/transition

https://drafts.csswg.org/css-transitions/#transitions

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions

http://www.w3cplus.com/content/css3-transition

https://developer.mozilla.org/zh-CN/docs/Web/CSS/list-style

https://developer.mozilla.org/zh-CN/docs/Web/CSS/list-style-type

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,924评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,902评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,716评论 0 239
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,783评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,166评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,510评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,784评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,476评论 0 196
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,196评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,459评论 2 243
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,978评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,321评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,964评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,046评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,803评论 0 193
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,530评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,420评论 2 265

推荐阅读更多精彩内容

  • 1、垂直对齐 如果你用CSS,则你会有困惑:我该怎么垂直对齐容器中的元素?现在,利用CSS3的Transform,...
    kiddings阅读 3,097评论 0 11
  • 1.CSS3 边框 border-radius CSS属性用来设置边框圆角。当使用一个半径时确定一个圆形;当使用两...
    garble阅读 588评论 0 0
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    love2013阅读 2,278评论 0 11
  • 微醺的酒意仿佛带着我回到了多年以前。 那些年骄阳似火,你与我都带着任性与不羁的笑容。 我竟不知是从什么时候开始越加...
    熊心中的小莲花阅读 473评论 0 2
  • 姥姥有一个自己的小院子,小但缤纷。毛茸茸的冬瓜,直挺挺的小葱,还有韭菜,西红柿,豆角,小白菜...姥姥待它们就像待...
    辣么大之阅读 119评论 0 1