Inheriting width from Bootstrap container when child is position fixed

岁酱吖の 提交于 2019-12-10 16:38:11

问题


I am trying to have a header div inherit its width from its parent. The header div is position: fixed. The parent is contained inside a bootstrap container.

However, as you can see in the code I've created, it is not correctly inheriting the width of its parent - it is adding some extra width from somewhere.

This is all very annoying! Any idea how to fix this?

.category-body {
  margin-left: 17% !important;
  width: 67%;
  background-color: red;
  height: 500px;
}
.category-header {
  position: fixed;
  top: 51px;
  width: inherit;
  background-color: green;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div class="category-body">We are in the category-body
    <div class="category-header">We are in the category-header</div>
  </div>
</div>

http://plnkr.co/edit/yVk15RqDqAac4H2eDJQe


回答1:


The problem stems from using a percentage value on the parent width. Browsers seem to have a problem with this. (Tested on Chrome, FF & IE11.)

If you use pixel values the problem goes away.

Revise PLUNKR

From another answer:

It seems as though the static value (250px) can be propagated through normal inheritance. Whereas when the relative value (90%) is being used, the fixed div has already been taken out-of-flow, and now inheritance-wise, it's parent is the viewport.

Learn more here:

  • position:fixed and width:inherit with percentage parent
  • How can I make a fixed positioned div inherit width of parent?
  • Set a Fixed div to 100% width of the parent container
  • Set width of a "Position: fixed" div relative to parent div
  • Set width of fixed positioned div relative to his parent having max-width
  • Fluid width fixed position


来源:https://stackoverflow.com/questions/34161665/inheriting-width-from-bootstrap-container-when-child-is-position-fixed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!