freeCodeCamp/curriculum/challenges/chinese/01-responsive-web-design/applied-visual-design/lock-an-element-to-the-brow...

2.6 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d781e367417b2b2512acc Lock an Element to the Browser Window with Fixed Positioning 0 使用固定定位将元素锁定到浏览器窗口

Description

CSS提供的下一个布局方案是fixed位置这是一种绝对定位可以相对于浏览器窗口锁定元素。与绝对定位类似它与CSS偏移属性一起使用并且还从文档的正常流中移除元素。其他项目不再“实现”它所处的位置这可能需要在其他地方进行一些布局调整。 fixed位置和absolute位置之间的一个关键区别是,当用户滚动时,具有固定位置的元素将不会移动。

Instructions

在代码导航栏标有的一个id navbar 。将其position更改为fixed ,并将其偏离top 0像素和left 0像素。注意到没有h1位置的影响,它没有被按下以容纳导航栏并且需要单独调整。

Tests

tests:
  - text: '<code>#navbar</code>元素的<code>position</code>应设置为<code>fixed</code> 。'
    testString: 'assert($("#navbar").css("position") == "fixed", "The <code>#navbar</code> element should have a <code>position</code> set to <code>fixed</code>.");'
  - text: '您的代码应该使用<code>#navbar</code>元素上0像素的<code>top</code> CSS偏移量。'
    testString: 'assert($("#navbar").css("top") == "0px", "Your code should use the <code>top</code> CSS offset of 0 pixels on the <code>#navbar</code> element.");'
  - text: '您的代码应使用<code>#navbar</code>元素上0像素的<code>left</code> CSS偏移量。'
    testString: 'assert($("#navbar").css("left") == "0px", "Your code should use the <code>left</code> CSS offset of 0 pixels on the <code>#navbar</code> element.");'

Challenge Seed

<style>
  #navbar {



    width: 100%;
    background-color: #767676;
  }
  nav ul {
    margin: 0px;
    padding: 5px 0px 5px 30px;
  }
  nav li {
    display: inline;
    margin-right: 20px;
  }
  a {
    text-decoration: none;
  }
</style>
<body>
  <header>
    <h1>Welcome!</h1>
    <nav id="navbar">
      <ul>
        <li><a href="">Home</a></li>
        <li><a href="">Contact</a></li>
      </ul>
    </nav>
  </header>
  <p>I shift up when the #navbar is fixed to the browser window.</p>
</body>

Solution

// solution required