CSS基础选择器

1、派生选择器:是根据文档的上下文关系来确定某个标签的样式。通过合理地使用派生选择器,我们可以使HTML代码变得更加整洁。

例如,我们希望列表中的 strong 元素变为斜体字,而不是通常的粗体字,可以这样定义一个派生选择器:

li strong {font-style: italic;font-weight: normal;}

<p><strong>我是粗体字,不是斜体字,因为我不在列表当中,所以这个规则对我不起作用</strong></p><ol><li><strong>我是斜体字。这是因为 strong 元素位于 li 元素内。</strong></li<li>我是正常的字体。</li></ol>

在上面的例子中,只有 li 元素中的 strong 元素的样式为斜体字,无需为 strong 元素定义特别的 class 或 id,代码更加简洁。

2、id选择器:id 属性只能在每个 HTML 文档中出现一次。

在网页布局中,id 选择器常常用于建立派生选择器。
即使被标注为 sidebar 的元素只能在文档中出现一次,这个 id 选择器作为派生选择器也可以被使用很多次:

#sidebar p {font-style: italic;}#sidebar h2 {font-size: 1em;}

 3、CSS类选择器:.类名。

和 id 一样,class 也可被用作派生选择器:

4、属性选择器:

  1. 属性选择器:
    1. [title]
    2. {
    3. color:red;
    4. }
  2. 属性和值选择器:
    1. [title=baidu]
    2. {
    3. color:red;
    4. }
  3. 属性和值选择器--多个值:
    1. [title~=hello] { color:red; }

 

posted on 2021-07-05 11:16  代码改变世界001  阅读(57)  评论(0)    收藏  举报

导航