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

3.9 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
bad87fee1348bd9aedf08805 Use CSS Selectors to Style Elements 0 使用CSS选择器设置样式元素

Description

使用CSS您可以使用数百种CSS properties来更改元素在页面上的显示方式。当您输入<h2 style="color: red;">CatPhotoApp</h2> ,您使用inline CSS (代表Cascading Style Sheets对单个h2元素进行Cascading Style Sheets 。这是指定元素样式的一种方法,但有一种更好的方法来应用CSS 。在代码的顶部,创建一个style块,如下所示:
<风格>
</样式>
在该样式块中,您可以为所有h2元素创建CSS selector 。例如,如果您希望所有h2元素都是红色,则可以添加如下所示的样式规则:
<风格>
h2 {colorred;}
</样式>
请注意,在每个元素的样式规则周围同时打开和关闭花括号( {} )非常重要。您还需要确保元素的样式定义位于开始和结束样式标记之间。最后,请务必在每个元素的样式规则的末尾添加分号。

Instructions

删除h2元素的样式属性而不是创建CSS style块。添加必要的CSS以将所有h2元素变为蓝色。

Tests

tests:
  - text: 从<code>h2</code>元素中删除style属性。
    testString: 'assert(!$("h2").attr("style"), "Remove the style attribute from your <code>h2</code> element.");'
  - text: 创建<code>style</code>元素。
    testString: 'assert($("style") && $("style").length > 1, "Create a <code>style</code> element.");'
  - text: 你的<code>h2</code>元素应该是蓝色的。
    testString: 'assert($("h2").css("color") === "rgb(0, 0, 255)", "Your <code>h2</code> element should be blue.");'
  - text: 确保样式表<code>h2</code>声明对分号和右括号有效。
    testString: 'assert(code.match(/h2\s*\{\s*color\s*:.*;\s*\}/g), "Ensure that your stylesheet <code>h2</code> declaration is valid with a semicolon and closing brace.");'
  - 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, "Make sure all your <code>style</code> elements are valid and have a closing tag.");'

Challenge Seed

<h2 style="color: red;">CatPhotoApp</h2>
<main>
  <p>Click here to view more <a href="#">cat photos</a>.</p>

  <a href="#"><img src="https://bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying on its back."></a>

  <div>
    <p>Things cats love:</p>
    <ul>
      <li>cat nip</li>
      <li>laser pointers</li>
      <li>lasagna</li>
    </ul>
    <p>Top 3 things cats hate:</p>
    <ol>
      <li>flea treatment</li>
      <li>thunder</li>
      <li>other cats</li>
    </ol>
  </div>

  <form action="/submit-cat-photo">
    <label><input type="radio" name="indoor-outdoor" checked> Indoor</label>
    <label><input type="radio" name="indoor-outdoor"> Outdoor</label><br>
    <label><input type="checkbox" name="personality" checked> Loving</label>
    <label><input type="checkbox" name="personality"> Lazy</label>
    <label><input type="checkbox" name="personality"> Energetic</label><br>
    <input type="text" placeholder="cat photo URL" required>
    <button type="submit">Submit</button>
  </form>
</main>

Solution

// solution required