我目前正在构建一个响应式网站,需要固定菜单,在整个网站滚动时不会滚动。问题是它是一个流体布局,我希望“固定位置”的菜单项相对于包含父元素而不是浏览器窗口进行固定。是否有任何方法可以做到这一点?
虽然这是一个旧问题,但它首先出现在谷歌上,所以我将发布我找到的可供他人使用的工作答案。
这需要包括固定div在内的3个div。
HTML
<div class="wrapper">
<div class="parent">
<div class="child"></div>
</div>
</div>
CSS
.wrapper { position:relative; width:1280px; }
.parent { position:absolute; }
.child { position:fixed; width:960px; }
Gavin,
你遇到的问题是对定位的误解。如果你希望它相对于父元素“固定”,那么你真正想要的是将你的#fixed
设置为position:absolute
,这将更新其相对于父元素的位置。
这个问题全面描述了定位类型及其如何有效使用。
总之,你的CSS应该是
#wrap{
position:relative;
}
#fixed{
position:absolute;
top:30px;
left:40px;
}
fixed
会忽略父元素,除非父元素也是fixed
)如果我正确理解您的问题,那么JavaScript和CSS的组合将是您唯一的选择。 - Fuzzical Logicposition:sticky;
。<style>
.parent-to-position-by {
position: relative;
top: 40px; /* just to show it's relative positioned */
}
.scrolling-contents {
display: inline-block;
width: 100%;
height: 200px;
line-height: 20px;
white-space: nowrap;
background-color: #CCC;
overflow: scroll;
}
.fixed-elements {
display: inline-block;
position: absolute;
top: 0;
left: 0;
}
.fixed {
position: absolute; /* effectively fixed */
top: 20px;
left: 20px;
background-color: #F00;
width: 200px;
height: 20px;
}
</style>
HTML
<div class="parent-to-position-by">
<div class="fixed-elements">
<div class="fixed">
I am "fixed positioned"
</div>
</div>
<div class="scrolling-contents">
Lots of contents which may be scrolled.
</div>
</div>
parent-to-position-by
是相对于哪个scrolling-contents
将延伸到这个fixed-elements
只是一个绝对定位的scrolling-contents
fixed
类的绝对定位的<div>
:#wrap{ position:absolute;left:100px;top:100px; }
#fixed{
position:fixed;
width:10px;
height:10px;
background-color:#333;
margin-left:200px;
margin-top:200px;
}
And this HTML:
<div id="wrap">
<div id="fixed"></div>
</div>
试着在这个jsfiddle中玩一下。
div#wrapper {
margin: auto;
width: 80%;
}
div#main {
width: 60%;
}
div#sidebar {
position: fixed;
width: 30%;
left: 60%;
}
.wrapper{
position:absolute;
left:10%;// or some valve in px
top:10%; // or some valve in px
}
和
.wrapper .fixed-element{
position:fixed;
width:100%;
height:100%;
margin-left:auto; // to center this div inside at center give auto margin
margin-right:auto;
}
将位置从fixed更改为sticky即可。
position: sticky
<div class="Container">
<div class="First">
<p>The First div height is not fixed</p>
<p>This Layout has been tested on: IE10, IE9, IE8, FireFox, Chrome, Safari, using Pure CSS 2.1 only</p>
</div>
<div class="Second">
<div class="Wrapper">
<div class="Centered">
<p>The Second div should always span the available Container space.</p>
<p>This content is vertically Centered.</p>
</div>
</div>
</div>
</div>
CSS:
*
{
margin: 0;
padding: 0;
}
html, body, .Container
{
height: 100%;
}
.Container:before
{
content: '';
height: 100%;
float: left;
}
.First
{
/*for demonstration only*/
background-color: #bf5b5b;
}
.Second
{
position: relative;
z-index: 1;
/*for demonstration only*/
background-color: #6ea364;
}
.Second:after
{
content: '';
clear: both;
display: block;
}
/*This part his relevant only for Vertically centering*/
.Wrapper
{
position: absolute;
width: 100%;
height: 100%;
overflow: auto;
}
.Wrapper:before
{
content: '';
display: inline-block;
vertical-align: middle;
height: 100%;
}
.Centered
{
display: inline-block;
vertical-align: middle;
}
<div class="container">
<div class="relative">
<div class="absolute"></div>
<div class="content">
<p>
Content here
</p>
</div>
</div>
</div>
而对于CSS
.relative {
position: relative;
}
.absolute {
position: absolute;
top: 15px;
left: 25px;
}
在 CodePen 上查看 https://codepen.io/FelySpring/pen/jXENXY