Home  >  Article  >  Web Front-end  >  CSS入门_html/css_WEB-ITnose

CSS入门_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:34:29985browse

 

 

选择器

 

1 2     p{3       color: maroon;4     }

 

把CSS放入HTML中

 

 1  2  3    4    HEAD FIRST CSS 5   10 

为一个或多个页面增加样式:

步骤:

  1. 取出html文件中的样式规则,把他们放在一个后缀名为.css的文件中。
  2. 从每个html文件创建一个到css文件的外部链接
  3. 做好测试

 1  2     p{ 3       color: maroon; 4     } 5  6  7  8  9   10   11   head first Lounge12     13    14   15 16 

样式的继承

只能对体中的元素增加样式,所以元素不用作讨论

上图注释:

  • 如果设置所有

    元素,就会影响到下面阴影部分的所有元素。

  • img元素也是段落的子元素,不过他没有任何文本,故不受影响
  •  

     

    长江后浪推前浪,覆盖继承

     

    然而,有些时候,不希望每个元素都继承样式。

    但是,还好,浏览器总会使用更特定的规则

    1 body{2         font-family:sans-serif;3     }4     5     /* 覆盖掉了从body继承的font-family属性 */6     em{7       font - family:serif;8     }

    单独为某些段落设置样式

     

    总是有些段落要特殊点的

    不是吗?所以,有了类的存在,就像俱乐部

    1 2 

    3 this is a specil people/class;4

    5

    6 this is 平常的 p;7

    1 /*这个样式在一个index.css文件中*/2  p{3         font-family:sans-serif;4     }5  /*specil类的p ,覆盖掉了从p继承的样式,多个元素同属一个类时,为了一次设置多个元素,可以省略类前的元素名*/6   p.specil{7          color:green;8     }

    谁是赢家?

     

    什么?一个元素加入了多个俱乐部?

    没事,选择器总是会选最特定(特定等级相同时选最后的)的样式

    1 2 

    3 this is a specil people/class;4

     1 /*选择所有段落*/ 2 p{ 3  4   color:black; 5 } 6 /*选择specil类所有成员,比上一个特定点*/ 7 .specil{ 8   color:green; 9 }10 /*这个规则只选specil类中段落,比前一个更特定*/11 p.specil{12   color:green;13 }14 p.specil{15   color:blue;16 }

    结果:

  • 如果一个元素属于多个类,选择器会选择特定等级最高的。
  • 如果特定程度都相同,选择器总会选择最后出现的规则。
  • 上面的代码最后的玩家是蓝色。
  • 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