从零开始学习html(十三) CSS代码缩写,占用更少的带宽

一、盒模型代码简写

 1 <!DOCTYPE HTML>
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 5 <title>relative样式</title>
 6 <style type="text/css">
 7 p{
 8     padding:13px 13px 13px 13px;
 9     margin:10px 40px 10px 40px;
10 }
11 </style>
12 </head>
13 <body>
14     <p>文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本。</p>
15 </body>
16 </html>
盒模型代码简写

还记得在讲盒模型时外边距(margin)、内边距(padding)和边框(border)设置上下左右四个方向的边距是按照顺时针方向设置的:上右下左

具体应用在margin和padding的例子如下:

margin:10px 15px 12px 14px;/*上设置为10px、右设置为15px、下设置为12px、左设置为14px*/

通常有下面三种缩写方法:

1、如果top、right、bottom、left的值相同,如下面代码:

margin:10px 10px 10px 10px;

可缩写为:

margin:10px;

2、如果top和bottom值相同、left和 right的值相同,如下面代码:

margin:10px 20px 10px 20px;

可缩写为:

margin:10px 20px;

3、如果left和right的值相同,如下面代码:

margin:10px 20px 30px 20px;

可缩写为:

margin:10px 20px 30px;

注意:padding、border的缩写方法和margin是一致的。

任务

来试试,在代码编辑器中把margin和padding两个值修改成缩写形式。

参考代码如下:

p{

 padding:13px;

 margin:10px 40px;

}

盒子模型尺寸=边框+外边距+内边距+盒子中内容的尺寸

定义顺序:上 右 下 左 (顺时针)

定义三个值:1(上)2(左右)3(下)

定义两个值:1(上下) 2(左右)这个顺序是上,右,下,左,,,,顺时针方向,,,然后如果有简写,都是对应式简写,

比如只有一个值,代表都是这样,要是两个值(代表,上下和左右是一样的) 要是三个值(代表默认的最后一个left和给定的right是一样的)

二、颜色值缩写

 1 <!DOCTYPE HTML>
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 5 <title>relative样式</title>
 6 <style type="text/css">
 7 p{
 8     color:#336699;
 9 }
10 </style>
11 </head>
12 <body>
13     <p>文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本。</p>
14 </body>
15 </html>
颜色值缩写

关于颜色的css样式也是可以缩写的,当你设置的颜色是16进制的色彩值时,如果每两位的值相同,可以缩写一半。

例子1:

p{color:#000000;}

可以缩写为:

p{color: #000;}

例子2:

p{color: #336699;}

可以缩写为:

p{color: #369;}

任务

来试试,在代码编辑器中试一试颜色缩形式

参考代码:
p{
 color:#369;
}
CSS中颜色的表示方式有三种:

1:用英文单词直接描述,如:red,green,black等。

2:用16进制表示,如:#000000,#FFFFFF等

3:用缩写16进制表示,如:#000,#FFF等

三、字体缩写

 1 <!DOCTYPE HTML>
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 5 <title>字体缩写</title>
 6 <style type="text/css">
 7 body{
 8     font-style:italic;
 9     font-variant:small-caps; 
10     font-weight:bold; 
11     font-size:12px; 
12     line-height:1.6em; 
13     font-family:"宋体",sans-serif;
14 }
15 
16 </style>
17 </head>
18 <body>
19     <p>Littering a dark and dreary road lay the past relics of browser-specific tags, incompatible DOMs, broken CSS support, and abandoned browsers.</p>
20     <p>We must clear the mind of the past. Web enlightenment has been achieved thanks to the tireless efforts of folk like the W3C, WaSP, and the major browser creators.</p>
21     <p>The CSS Zen Garden invites you to relax and meditate on the important lessons of the masters. Begin to see with clarity. Learn to use the time-honored techniques in new and invigorating fashion. Become one with the web.</p>
22 </body>
23 </html>
字体缩写

网页中的字体css样式代码也有他自己的缩写方式,下面是给网页设置字体的代码:

body{
    font-style:italic;
    font-variant:small-caps; 
    font-weight:bold; 
    font-size:12px; 
    line-height:1.5em; 
    font-family:"宋体",sans-serif;
}

这么多行的代码其实可以缩写为一句:

body{
    font:italic  small-caps  bold  12px/1.5em  "宋体",sans-serif;
}

注意:

1、使用这一简写方式你至少要指定 font-size 和 font-family 属性,其他的属性(如 font-weight、font-style、font-variant、line-height)如未指定将自动使用默认值。

2、在缩写时 font-size 与 line-height 中间要加入“/”斜扛。

一般情况下因为对于中文网站,英文还是比较少的,所以下面缩写代码比较常用:

body{
    font:12px/1.5em  "宋体",sans-serif;
}

只是有字号、行间距、中文字体、英文字体设置。

可以挨个解释含义。
body{
font-style:italic; //设置字体为斜体
font-variant:small-caps; //区分大小写,浏览器会显示小型大写字母的字体
font-weight:bold; //设置粗体
font-size:12px; //字号是12像素
line-height:1.5em; //行间距(行高)1.5个行倍
font-family:"宋体",sans-serif;//设置中文字体为宋体,英文字体为sans-serif}

 

body{
font:italic small-caps bold 12px/1.5em "宋体",sans-serif;
}
前三个顺序没影响可无,后两个顺序固定且必须有

 
简写顺序:font-style | font-variant | font-weight | font-size | line-height | font-family
 
原文地址:https://www.cnblogs.com/guxinglang/p/6815187.html