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

3.3 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
bad87fee1348bd9aecf08806 Use a CSS Class to Style an Element 0 使用CSS类来设置元素的样式

Description

类是可重用的样式可以添加到HTML元素中。这是一个CSS类声明示例
<风格>
.blue-text {
颜色:蓝色;
}
</样式>
您可以看到我们在<style>标记内创建了一个名为blue-text的CSS类。您可以将类应用于HTML元素如下所示 <h2 class="blue-text">CatPhotoApp</h2>请注意在CSS style元素中类名以句点开头。在HTML元素的class属性中类名不包含句点。

Instructions

style元素中,将h2选择器更改为.red-text并将颜色的值从blue更新为red 。为您的h2元素提供值为'red-text'class属性。

Tests

tests:
  - text: 你的<code>h2</code>元素应该是红色的。
    testString: 'assert($("h2").css("color") === "rgb(255, 0, 0)", "Your <code>h2</code> element should be red.");'
  - text: 你的<code>h2</code>元素应该有<code>red-text</code>类。
    testString: 'assert($("h2").hasClass("red-text"), "Your <code>h2</code> element should have the class <code>red-text</code>.");'
  - text: 样式表应声明一个<code>red-text</code>类,并将其颜色设置为红色。
    testString: 'assert(code.match(/\.red-text\s*\{\s*color\s*:\s*red;\s*\}/g), "Your stylesheet should declare a <code>red-text</code> class and have its color set to red.");'
  - text: '不要在<code>h2</code>元素中使用<code>style=&quot;color: red&quot;</code>内联样式声明。'
    testString: 'assert($("h2").attr("style") === undefined, "Do not use inline style declarations like <code>style="color&#58; red"</code> in your <code>h2</code> element.");'

Challenge Seed

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

<h2>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