【发布时间】:2016-03-13 17:08:19
【问题描述】:
我正在尝试让标题 div 从其父级继承其宽度。标题 div 是 position: fixed。父级包含在引导容器中。
但是,正如您在我创建的代码中看到的那样,它没有正确继承其父级的宽度 - 它正在从某处添加一些额外的宽度。
这一切都很烦人!知道如何解决这个问题吗?
.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>
【问题讨论】:
-
父元素的宽度是否总是
67%? -
我明白你的意思,但你的继承概念有点离题。当您将
position: fixed应用于某个元素时,您会将其从文档流中移除。然后该元素没有父元素,因此没有什么可继承的。更多详情here -
@Michael_B 这并不完全正确。即使该元素已从文档流中删除,它仍然继承
67%的宽度。它只是不起作用,因为百分比是相对于视口而不是与父容器相同的容器。宽度是继承的。例如,如果父级的宽度为300px,则为would work as expected。它并不总是适用于基于百分比的宽度。 -
@Josh Corzier 没有必要——仅适用于最小宽度:1200px。对于较小的屏幕尺寸,它有 margin-left/right: 100px (min-width: 992) 或下面的空 .category-body{}
标签: html css twitter-bootstrap css-position