【发布时间】:2013-04-07 18:21:00
【问题描述】:
当我们将position:fixed 应用于一个元素时,它会占用文档的out of the normal flow,因此它不尊重它的父元素宽度。
有没有办法让它继承它的父宽度如果这被声明为百分比? (下面的工作用例)
let widthis = $('.box').width();
$('.dimensions').text(`width is ${widthis}`);
$('button').on('click', function() {
$('.box').toggleClass('fixed');
let widthis = $('.box').width();
$('.dimensions').text(`width is ${widthis}`);
});
.container {
max-width: 500px;
height: 1000px;
}
.box {
background-color: lightgreen;
}
.fixed {
position: fixed;
}
.col-1 {
border: 1px solid red;
float: left;
width: 29%;
}
.col-2 {
border: 1px solid pink;
float: left;
width: 69%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Click this to toggle position fixed for the left column</button>
<div class="container">
<div class="col-1">
<div class="box">
fixed content<br>
<span class="dimensions"></span>
</div>
</div>
<div class="col-2">
some other content
</div>
</div>
【问题讨论】:
-
CSS 看起来像是在做它被告知要做的事情?
-
它,也可能是菜单不再是父菜单了?
-
我更新了 url,我在我的测试用例中忘记了一些 CSS 属性。改变 position:fixed 与 position:static 你会看到宽度是如何变化的。
-
是的,这可能是因为菜单不再充当父菜单,我认为您可能需要相应调整。
-
@GeorgeKatsanos,我想也许是时候选择一个答案了?
标签: css position css-position