【问题标题】:How to prevent a fixed position header from extending beyond the width of the browser如何防止固定位置的标题超出浏览器的宽度
【发布时间】:2014-10-07 10:38:07
【问题描述】:

我的页面顶部有一个固定的标题,它应该是宽度:100%,但它超出了窗口的宽度。谁能告诉我我在这里缺少什么?我尝试将父 div 重置为 0 边距。我尝试将标题包含在父 div 中。我尝试按照here 的建议添加 box-sizing,但这也无济于事。在任何情况下,标题都会继续延伸到浏览器窗口的右侧。

html:

<body>
  <div class='header'>
    <h1 class='name'>some name</h1>
    <button class='link-button' type='menu'>Links</button>
    <div class='navigation-links'>
      <a href="#link" class='header-link'>some link</a>
    </div>
  </div>
  <div>
    <h3>content</h3>
    ...
  </div>
</body>

css:

html {
  margin: 0;
  padding: 0;
  font-family: 'Roboto Slab', serif;
}

body {
  margin: 0;
  padding: 0;
}

.header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 1;
  background-color: rgba(27,62,111,1);
  text-align: center;
  height: auto;
  margin-bottom: 0;
}

感谢您的帮助!

【问题讨论】:

  • 在 Chrome 中看起来不错。可以加个截图吗?
  • 无法使用您发布的代码进行复制。
  • 在 chrome 中也适用于我。
  • 感谢您的尝试。我想出了为什么你不能复制它。这是页面下方的填充物,我没有在问题中包含。

标签: html css


【解决方案1】:

我认为您正在寻找的是:http://www.w3schools.com/cssref/pr_dim_max-width.asp

css 属性 max-width: 100% 应该将其限制在窗口内 - 除非周围的容器允许它超出屏幕限制。

另外,您可能在标题内设置了宽度,导致它比预期的要大。

【讨论】:

    【解决方案2】:

    无法使用您的代码重现。您可能正在将最小宽度设置为其他地方的另一个 div。

    【讨论】:

      猜你喜欢
      • 2011-09-18
      • 2013-04-13
      • 1970-01-01
      • 2014-07-23
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      • 2011-08-21
      相关资源
      最近更新 更多