freeCodeCamp/curriculum/challenges/chinese/01-responsive-web-design/basic-css/use-css-selectors-to-style-...

3.5 KiB

id challengeType videoUrl forumTopicId title
bad87fee1348bd9aedf08805 0 https://scrimba.com/c/cJKMBT2 18349 使用元素选择器来设置元素的样式

Description

在 CSS 中,页面样式的属性有几百个,但常用的不过几十个。 通过行内样式<h2 style="color: red;">CatPhotoApp</h2>,就可以修改h2元素的颜色为红色。 当我们只需要改变元素的某个样式时,行内样式最简单直观。当我们需要同时改变元素的很多样式时,层叠样式表往往是一个更好的选择。 在代码的顶部,创建一个style声明区域,如下方所示:
<style>
</style>

style样式声明区域内,可以创建一个元素选择器,应用于所有的h2元素。例如,如果你想所有h2元素变成红色,可以添加下方的样式规则:

<style>
  h2 {
    color: red;
  }
</style>

注意,在每个元素的样式声明区域里,左右花括号({})一定要写全。你需要确保所有样式规则位于花括号之间,并且每条样式规则都以分号结束。

Instructions

删除h2元素的行内样式,然后创建style样式声明区域,最后添加 CSS 样式规则使h2元素变为蓝色。

Tests

tests:
  - text: 删除<code>h2</code>元素的行内样式。
    testString: assert(!$("h2").attr("style"));
  - text: 创建一个<code>style</code>样式声明区域。
    testString: assert($("style") && $("style").length >= 1);
  - text: <code>h2</code>元素颜色应为蓝色。
    testString: assert($("h2").css("color") === "rgb(0, 0, 255)");
  - text: 确保<code>h2</code>选择器的内容被花括号所围绕,并且样式规则以分号结束。
    testString: assert(code.match(/h2\s*\{\s*color\s*:.*;\s*\}/g));
  - text: 所有<code>style</code>应该是有效的且有一个结束标签。
    testString: assert(code.match(/<\/style>/g) && code.match(/<\/style>/g).length === (code.match(/<style((\s)*((type|media|scoped|title|disabled)="[^"]*")?(\s)*)*>/g) || []).length);

Challenge Seed

<h2 style="color: red;">CatPhotoApp</h2>
<main>
  <p class="red-text">点击查看更多<a href="#">猫图</a>.</p>
  
  <a href="#"><img src="https://bit.ly/fcc-relaxing-cat" alt="一只仰卧着的萌猫"></a>
  
  <div>
    <p>猫咪最喜欢的三件东西:</p>
    <ul>
      <li>猫薄荷</li>
      <li>激光笔</li>
      <li>千层饼</li>
    </ul>
    <p>猫咪最讨厌的三件东西:</p>
    <ol>
      <li>跳蚤</li>
      <li>打雷</li>
      <li>同类</li>
    </ol>
  </div>
  
  <form action="https://freecatphotoapp.com/submit-cat-photo">
    <label><input type="radio" name="indoor-outdoor">室内</label>
    <label><input type="radio" name="indoor-outdoor">室外</label><br>
    <label><input type="checkbox" name="personality">忠诚</label>
    <label><input type="checkbox" name="personality">懒惰</label>
    <label><input type="checkbox" name="personality">积极</label><br>
    <input type="text" placeholder="猫咪图片地址" required>
    <button type="submit">提交</button>
  </form>
</main>

Solution

// solution required