CSS选择器

1、标签选择器

P标签选择器 p {text-align:center; color:red;}

多个标签选择器样式共享 h1,h2,h3,h4,h5,h6 {  color: green;  },目前只有元素选择器才能共享,ID和类选择器不能使用

①派生

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

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

2、ID选择器

#red {color:red;}

#green {color:green;}

<p id="red">这个段落是红色。</p>

<p id="green">这个段落是绿色。</p>

①派生

#sidebar p {    font-style: italic;    text-align: right;    margin-top: 0.5em;    }

表示idsidebar ,下一季P标签所有样式

3、类选择器

.center {text-align: center}

<h1 class="center">This heading will be center-aligned</h1>

<p class="center">This paragraph will also be center-aligned.</p>

①派生

.fancy td {    color: #f60;    background: #666;    }

②元素基于类名选择

td.fancy {    color: #f60;    background: #666;    }

<td class="fancy">

例如,您可能希望只有段落显示为红色文本:

p.important {color:red;}

选择器现在会匹配 class 属性包含 important 的所有 元素,但是其他任何类型的元素都不匹配,不论是否有此 class 属性。选择器 p.important 解释为:其 class 属性值为 important 的所有段落。 因为 h1 元素不是段落,这个规则的选择器与之不匹配,因此 h1 元素不会变成红色文本。

如果你确实希望为 h1 元素指定不同的样式,可以使用选择器 h1.important

p.important {color:red;}

h1.important {color:blue;}

③CSS多类选择器

CSS 多类选择器

在上一节中,我们处理了 class 值中包含一个词的情况。在 HTML 中,一个 class 值中可能包含一个词列表,各个词之间用空格分隔。例如,如果希望将一个特定的元素同时标记为重要(important)和警告(warning),就可以写作:

<p class="important warning">

This paragraph is a very important warning.

</p>

这两个词的顺序无关紧要,写成 warning important 也可以。

我们假设 class 为 important 的所有元素都是粗体,而 class 为 warning 的所有元素为斜体,class 中同时包含 important 和 warning 的所有元素还有一个银色的背景 。就可以写作:

.important {font-weight:bold;}

.warning {font-style:italic;}

.important.warning {background:silver;}

亲自试一试

通过把两个类选择器链接在一起,仅可以选择同时包含这些类名的元素(类名的顺序不限)。

如果一个多类选择器包含类名列表中没有的一个类名,匹配就会失败。请看下面的规则:

.important.urgent {background:silver;}

不出所料,这个选择器将只匹配 class 属性中包含词 important 和 urgent 的 元素。因此,如果一个 元素的 class 属性中只有词 important 和 warning,将不能匹配。不过,它能匹配以下元素:

<p class="important urgent warning">

This paragraph is a very important and urgent warning.

</p>

4、CSS后代选择器

①标签后代选择器

h1 em {color:red;}

注意点:

有关后代选择器有一个易被忽视的方面,即两个元素之间的层次间隔可以是无限的。

例如,如果写作 ul em,这个语法就会选择从 ul 元素继承的所有 em 元素而不论 em 的嵌套层次多深。

因此,ul em 将会选择以下标记中的所有 em 元素:

<ul>

  <li>List item 1

    <ol>

      <li>List item 1-1</li>

      <li>List item 1-2</li>

      <li>List item 1-3

        <ol>

          <li>List item 1-3-1</li>

          <li>List item <em>1-3-2</em></li>

          <li>List item 1-3-3</li>

        </ol>

      </li>

      <li>List item 1-4</li>

    </ol>

  </li>

  <li>List item 2</li>

  <li>List item 3</li>

</ul>

②、标签+样式后代选择器

假设有一个文档,其中有一个边栏,还有一个主区。边栏的背景为蓝色,主区的背景为白色,这两个区都包含链接列表。不能把所有链接都设置为蓝色,因为这样一来边栏中的蓝色链接都无法看到。

解决方法是使用后代选择器。在这种情况下,可以为包含边栏的 div 指定值为 sidebar 的 class 属性,并把主区的 class 属性值设置为 maincontent。然后编写以下样式:

div.sidebar {background:blue;}

div.maincontent {background:white;}

div.sidebar a:link {color:white;}

div.maincontent a:link {color:blue;}

5、CSS 子元素选择器(不常用)

6、CSS 相邻兄弟选择器(不常用)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值