Files
freeCodeCamp/curriculum/challenges/chinese/01-responsive-web-design/basic-css/use-an-id-attribute-to-style-an-element.chinese.md
2020-07-15 15:25:06 +05:30

3.8 KiB

id, title, challengeType, videoUrl, forumTopicId, localeTitle
id title challengeType videoUrl forumTopicId localeTitle
bad87dee1348bd9aede07836 Use an id Attribute to Style an Element 0 https://scrimba.com/c/cakyZfL 18339 使用 id 属性来设定元素的样式

Description

通过id属性,你可以做一些很酷的事情,例如,就像 class 一样,你可以使用 CSS 来设置他们的样式 可是,id不可以重用,只应用于一个元素上。同时,在 CSS 里,id的优先级要高于class,如果一个元素同时应用了classid,并设置样式有冲突,会优先应用id的样式。 选择idcat-photo-element的元素,并设置它的背景样式为green,可以在style标签里这样写:
#cat-photo-element {
  background-color: green;
}

注意在style标签里,声明 class 的时候必须在名字前插入.符号。同样,在声明 id 的时候,也必须在名字前插入#符号。

Instructions

尝试给含有cat-photo-formid属性的form表单的背景颜色设置为green

Tests

tests:
  - text: '设置<code>form</code>元素的 id 为<code>cat-photo-form</code>。'
    testString: assert($("form").attr("id") === "cat-photo-form");
  - text: '<code>form</code>元素应该含有<code>background-color</code>css 属性并且值为 <code>green</code>。'
    testString: assert($("#cat-photo-form").css("background-color") === "rgb(0, 128, 0)");
  - text: '确保<code>form</code>元素含有<code>id</code>属性。'
    testString: assert(code.match(/<form.*cat-photo-form.*>/gi) && code.match(/<form.*cat-photo-form.*>/gi).length > 0);
  - text: '不要在<code>form</code>元素上添加其他<code>class</code>属性或者<code>style</code>行内样式。'
    testString: assert(!code.match(/<form.*style.*>/gi) && !code.match(/<form.*class.*>/gi));

Challenge Seed

<link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet" type="text/css">
<style>
  .red-text {
    color: red;
  }

  h2 {
    font-family: Lobster, monospace;
  }

  p {
    font-size: 16px;
    font-family: monospace;
  }

  .thick-green-border {
    border-color: green;
    border-width: 10px;
    border-style: solid;
    border-radius: 50%;
  }

  .smaller-image {
    width: 100px;
  }

  .silver-background {
    background-color: silver;
  }
</style>

<h2 class="red-text">CatPhotoApp</h2>
<main>
  
  <a href="#"><img class="smaller-image thick-green-border" src="https://bit.ly/fcc-relaxing-cat" alt="一只仰卧着的萌猫"></a>
  
  <div class="silver-background">
    <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" id="cat-photo-form">
    <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