【问题标题】:css position:fixed overlappingcss位置:固定重叠
【发布时间】:2021-10-13 04:55:25
【问题描述】:

我的问题很简单,

我只想在页面的一侧添加一个菜单(导航栏),因此我给了它position fixed。问题在于,我尝试添加的所有 html 元素都会忽略它。 Problem posistion

【问题讨论】:

标签: html css


【解决方案1】:

如果你定位一个元素:

  • position: absolute;或
  • position: fixed

这会从 文档流 中删除该元素,这意味着,正如您所注意到的,所有其他元素将忽略该 out-of-flow 定位的元素并出现在它上面等等。

您可以在其他元素上使用paddingmargin,以确保它们不会与out-of-flow 定位元素重叠。


工作示例:

nav {
  position: fixed;
  top: 0;
  left: 0;
  width: 120px;
  padding: 12px;
  color: rgb(255, 255, 255);
  background-color: rgb(0, 0, 127);
}

main {
  margin-left: 156px;
}
<main>
  <h1>Main Page</h1>
  <p> This is the main part of the page.</p>
  <p>It has a <code>margin-left</code> of <code>156px</code>.</p>
</main>

<nav>
  <h2>Navigation</h2>
  
  <ul>
    <li>Page 1</li>
    <li>Page 2</li>
    <li>Page 3</li>
    <li>Page 4</li>
    <li>Page 5</li>
  </ul>
</nav>

【讨论】:

  • 有没有办法集成到文档流中
  • 还有其他方法——例如,使用 CSS Floats——可以让其他元素“意识到”您正在定位的元素。但是,如果您将 fixedabsolute 位置应用于元素,则明确指示应将该元素从文档流中取出。
  • CSS 浮动非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-27
相关资源
最近更新 更多