freeCodeCamp/curriculum/challenges/chinese-traditional/01-responsive-web-design/applied-visual-design/change-an-elements-relative...

2.4 KiB
Raw Blame History

id title challengeType videoUrl forumTopicId dashedName
587d781e367417b2b2512ac9 更改元素的相對位置 0 https://scrimba.com/c/czVmMtZ 301044 change-an-elements-relative-position

--description--

在 CSS 裏一切 HTML 元素皆爲盒子,也就是通常所說的盒模型。 塊級元素自動從新的一行開始(比如標題、段落以及 div行內元素排列在上一個元素後比如圖片以及 span。 元素默認按照這種方式佈局稱爲文檔的普通流,同時 CSS 提供了 position 屬性來覆蓋它。

當元素的定位設置爲 relative 時,它允許你通過 CSS 指定該元素在當前文檔流頁面下的相對偏移量。 CSS 裏控制各個方向偏移量的屬性是 leftrighttopbottom。 它們代表從原來位置向遠離該方向偏移指定的像素、百分比或者 em。 下面的例子展示了段落向上偏移 10px

p {
  position: relative;
  bottom: 10px;
}

把元素的位置設置成相對,並不會改變該元素在佈局中所佔的位置,也不會對其它元素的位置產生影響。

**注意:**定位可以使頁面佈局更靈活、高效。 不管元素的定位是怎樣的HTML 標記在從上到下閱讀起來時應該是整潔的、有意義的。 這樣可以讓視障人士(重度依賴輔助設備比如屏幕閱讀軟件的人們)也能夠無障礙地瀏覽你的網頁。

--instructions--

h2position 設置成 relative,使用相應的 CSS 屬性調整它的位置,使其相對 top 偏移 15px同時還在文檔流中處於原來的位置。 這不會對 h1 和 p 元素的位置產生影響。

--hints--

h2 元素應有一個值爲 relativeposition 屬性。

assert($('h2').css('position') == 'relative');

你應該使用 CSS 屬性調整 h2 的位置,使其從原來的位置相對 top 偏移 15px。

assert($('h2').css('top') == '15px');

--seed--

--seed-contents--

<style>
  h2 {


  }
</style>
<body>
  <h1>On Being Well-Positioned</h1>
  <h2>Move me!</h2>
  <p>I still think the h2 is where it normally sits.</p>
</body>

--solutions--

<style>
  h2 {
    position: relative;
    top: 15px;
  }
</style>
<body>
  <h1>On Being Well-Positioned</h1>
  <h2>Move me!</h2>
  <p>I still think the h2 is where it normally sits.</p>
</body>