Files
freeCodeCamp/curriculum/challenges/chinese/01-responsive-web-design/basic-css/use-an-id-attribute-to-style-an-element.md

1.6 KiB

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

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

--hints--

设置form元素的 id 为cat-photo-form

assert($('form').attr('id') === 'cat-photo-form');

form元素应该含有background-colorcss 属性并且值为 green

assert($('#cat-photo-form').css('background-color') === 'rgb(0, 128, 0)');

确保form元素含有id属性。

assert(
  code.match(/<form.*cat-photo-form.*>/gi) &&
    code.match(/<form.*cat-photo-form.*>/gi).length > 0
);

不要在form元素上添加其他class属性或者style行内样式。

assert(!code.match(/<form.*style.*>/gi) && !code.match(/<form.*class.*>/gi));

--solutions--