Heim  >  Artikel  >  Web-Frontend  >  一些常被你忽略的CSS小知识 - jerrylsxu

一些常被你忽略的CSS小知识 - jerrylsxu

WBOY
WBOYOriginal
2016-05-20 13:49:111023Durchsuche

1.CSS的color属性并非只能用于文本显示

     对于CSS的color属性,相信所有Web开发人员都使用过。如果你并不是一个特别有经

验的程序员,我相信你未必知道color属性除了能用在文本显示,还可以用作其它地方。它

可以把页面上的所有的东西都变颜色。比如:

      无法显示的图片的alt文字、 list元素的边框、无序list元素前面的小点、有序list元素前面的数字和hr元素等

 

   1: 

   2: 

   3:     <meta http-equiv="content-type" content="text/html;charset=utf-8">

   4:     <style type="text/css">

   5:         #div1

   6:         {

   7:             width: 375px;

   8:             height: 265px;

   9:             border: 1px solid blue;

  10:         }

  11:     </style>

  12: 

  13: 

  14: <div id="div1">

  15:      <img src="test.jpg" alt="图片加载失败"   style="max-width:90%">

  16:     <ol style="color:red;">

  17:         <li style="border: 1px solid">一</li>

  18:         <li>二</li>

  19:         <li>三</li>

  20:     </ol>

  21:     <hr style="color:red">

  22: </div>

  23: 

  24: 

 

有图为证:

1

 

    2.CSS里的visibility属性有个collapse属性值:collapse

 

       对于CSS里的visibility属性,相信你用过不下几百次。大多时候,你会把它的值设置

成visible(这是所有页面元素的缺省值),或者是hidden。后者相当于display: none,但仍

然占用页面空间。其实visibility可以有第三种值,就是collapse。

 

2

 

 

    3.CSS的background简写方式里新增了新的属性值

     在CSS2.1里,background属性的简写方式包含五种属性值 – background-color, background-

image,background-repeat, background-attachment, and background-position。从CSS3开始,又增加了3个新的属性值,加起来一共8个。下面是按顺序分别代表的意思:

background: [background-color] [background-image] [background-repeat] [background-attachment] 

[background-position] / [ background-size] [background-origin] [background-clip];注意里面的反斜杠,它

更font和border-radius里简写方式使用的反斜杠的用法相似。反斜杠可以在支持这种写法的浏览器里在

position后面接着写background-size。除此之外,你开可以增加另外两个描述它的属性值: background-

origin 和 background-clip.它的语法用起来像下面这个样子:

 

   1: .example {

   2: background: aquamarine url(img.png)

   3: no-repeat

   4: scroll

   5: center center / 50%

   6: content-box content-box;

   7: }


    4.CSS的clip属性只在绝对定位的元素上才会生效

        在style中加入

   1: img

   2:  {

   3:     width: 200px;

   4:     height: 200px;

   5:     clip: rect(0px 50px 200px 0px)

   6:  }


       在HTML中

1: <img src="bei.jpg" alt="图片加载失败"   style="max-width:90%">

     

     发现并没有裁剪

     3

       对img进行绝对定位

 

   1: img

   2:     {

   3:         width: 200px;

   4:         height: 200px;

   5:         position: absolute;

   6:         clip: rect(0px 50px 200px 0px)

   7:     }

 

      clip有效:

4

 

    5.元素竖向的百分比设定是相对于容器的宽度,而不是高度

         当 按百分比设定一个元素的宽度时,它是相对于父容器的宽度计算的,但是,对于一些表示竖向距离的属性,例如padding-top,padding- bottom,margin-top,margin-bottom等,当按百分比设定它们时,依据的也是父容器的宽度,而不是高度。给图片增加一个 padding-top:

 1: padding-top: 10%;

    根据效果和估算的距离即可证明是根据宽度来算的

5

 

    6.border-width属性可以使用预定义常量值

       除了可以使用标准宽度值(例如5px或1em)外,border-width属性可以接受预定义的常量值:medium, thin, 和 thick事实上,如果你不给border-width属性赋值,那它的缺省值是“medium”。

6

 

    7、你知道table里的empty-cells属性吗?

         css里的empty-cells属性是所有浏览器都支持的,甚至包括IE8,它的用法是下面这个样子:

 1: table { empty-cells: hide;}

 

估计你从语义上已经猜出它的作用了。它是为HTML table服务的。它会告诉浏览器,当一个table单元格里没有东西时,就隐藏它。

  7 

但是,empty-cells仅用于“分离边框”模式,即:border-collapse:separate;

 

    8、font-style的oblique属性值

         对与css的font-style属性,我估计大家每次见到的都是使用“normal”或 “italic”两个属性值。但事实上,你还可以让它赋值为“oblique”。

 

    9、word-wrap和overflow-wrap是等效的

         word-wrap并不是一个很常用的CSS属性,但在特定的环境中确实非常有用的。我们经常使用的一个例子是让页面中显示一个长url时换行,而不是撑破页面。在原本的div中添加一个子div,设置word-wrap属性

 

<strong>   1: <div style="width:250px;height:250px;border:1px solid red;word-wrap:break-word">

   2:          My father was a self-taught mandolin player.

   3:     He was one of the best string instrument players in our town.

   4:     He could not read music, but if he heard a tune a few times,

   5:     he could play it. When he was younger,

   6:  </div></strong>

 

效果

8

没有对长单词进行裁剪,而是将长单词作为整体另起一行显示。将word-wrap替换为overflow-wrap,效果一样。

但是,需要注意的是word-break属性,其会对长单词进行裁剪

 

<strong>   1: <div style="width:250px;height:250px;border:1px solid red;word-break:break-all">

   2:          My father was a self-taught mandolin player.

   3:     He was one of the best string instrument players in our town.

   4:     He could not read music, but if he heard a tune a few times,

   5:     he could play it. When he was younger,

   6:     </div></strong>


效果

      9

 

附:word-wrap取值:

10

word-break取值:

11

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn