Files
freeCodeCamp/curriculum/challenges/chinese/01-responsive-web-design/basic-css/override-all-other-styles-by-using-important.md
Oliver Eyton-Williams ee1e8abd87 feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

2.4 KiB

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
bad87fee1348bd9aedf07756 Important 的优先级最高 0 https://scrimba.com/c/cm24rcp 18249 override-all-other-styles-by-using-important

--description--

耶!我们刚刚又证明了行内样式会覆盖 style 标签里面所有的 CSS 声明。

不过,还有一种方式可以覆盖重新 CSS 样式。这是所有方法里面最强大的一个。在此之前,我们要考虑清楚,为什么我们要覆盖 CSS 样式。

很多时候,你使用的 CSS 库中的样式会意外覆盖你的 CSS 样式。如果想保证你的 CSS 样式不受影响,就可以使用 !important

让我们回到 pink-text class 声明之中,它的颜色样式已被之后的 class 声明、id 声明以及行内样式所覆盖。

--instructions--

pink-text class 的 color 声明里面使用 !important 关键字,以确保 h1 元素的字体颜色为粉色。类似这样:color: red !important

--hints--

h1 元素应该包含 pink-text class。

assert($('h1').hasClass('pink-text'));

h1 元素应该包含 blue-text class。

assert($('h1').hasClass('blue-text'));

h1 元素的 id 应为 orange-text

assert($('h1').attr('id') === 'orange-text');

h1 元素应该包含 color: white 的行内样式声明。

assert(code.match(/<h1.*style/gi) && code.match(/<h1.*style.*color\s*?:/gi));

pink-text class 声明中应含有 !important 关键字。

assert(
  code.match(/\.pink-text\s*?\{[\s\S]*?color:.*pink.*!important\s*;?[^\.]*\}/g)
);

h1 元素的字体颜色应为粉色。

assert($('h1').css('color') === 'rgb(255, 192, 203)');

--seed--

--seed-contents--

<style>
  body {
    background-color: black;
    font-family: monospace;
    color: green;
  }
  #orange-text {
    color: orange;
  }
  .pink-text {
    color: pink;
  }
  .blue-text {
    color: blue;
  }
</style>
<h1 id="orange-text" class="pink-text blue-text" style="color: white">Hello World!</h1>

--solutions--

<style>
  body {
    background-color: black;
    font-family: monospace;
    color: green;
  }
  #orange-text {
    color: orange;
  }
  .pink-text {
    color: pink !important;
  }
  .blue-text {
    color: blue;
  }
</style>
<h1 id="orange-text" class="pink-text blue-text" style="color: white">Hello World!</h1>