我目前正在构建一个响应式网站,需要修复一个菜单,因此当网站的其余部分滚动时不会滚动。问题是它是一个流畅的布局,我希望“固定位置”菜单项相对于包含父元素而不是浏览器窗口是固定的。无论如何这可以做到吗?
答案 0 :(得分:15)
这个问题首先出现在谷歌上,虽然是一个旧问题,所以我发布了我找到的工作答案,这对其他人有用。
这需要3个div,包括固定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; }
答案 1 :(得分:14)
加文,
您遇到的问题是对定位的误解。如果您希望它相对于父级“固定”,那么您真的希望#fixed
为position:absolute
,这将更新其相对于父级的位置。
This question完整地描述了定位类型以及如何有效地使用它们。
总之,你的CSS应该是
#wrap{
position:relative;
}
#fixed{
position:absolute;
top:30px;
left:40px;
}
答案 2 :(得分:7)
一个简单的解决方案,不涉及诉诸JavaScript并且不会破坏CSS变换,只需要一个非滚动元素,与滚动元素大小相同,绝对定位在它上面。
基本的HTML结构将是
<强> CSS 强>
<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
将是相对于div
的相对scrolling-contents
定位。div
将超出此fixed-elements
的大小并包含其主要内容div
只是位于scrolling-contents
div
顶部相同空格的绝对定位div
。fixed
与div
类进行绝对定位,可以获得与相对于父StartWith
固定定位的效果相同的效果。 (或滚动内容,因为它们跨越整个空间)答案 3 :(得分:4)
如果您使用边距而不是位置移动固定的<div>
,则可以这样做:
#wrap{ position:absolute;left:100px;top:100px; }
#fixed{
position:fixed;
width:10px;
height:10px;
background-color:#333;
margin-left:200px;
margin-top:200px;
}
这个HTML:
<div id="wrap">
<div id="fixed"></div>
</div>
使用此jsfiddle。
答案 4 :(得分:2)
您可以做的一件简单的事情是使用%值将固定DIV相对于页面的其余部分定位。
查看固定DIV为侧栏的jsfiddle here。
div#wrapper {
margin: auto;
width: 80%;
}
div#main {
width: 60%;
}
div#sidebar {
position: fixed;
width: 30%;
left: 60%;
}
以下简要说明上述布局:
答案 5 :(得分:2)
尝试postion:在父元素上粘贴。
答案 6 :(得分:1)
这是一个更通用的解决方案,它不取决于菜单/标题高度。 它完全响应,纯CSS解决方案,适用于IE8 +,Firefox,Chrome,Safari,歌剧。 支持内容滚动而不影响菜单/标题。
进行测试Html:
<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;
}
答案 7 :(得分:1)
你可以使用绝对定位来修复包装器。 并且内部div给定一个固定的位置。
.wrapper{
position:absolute;
left:10%;// or some valve in px
top:10%; // or some valve in px
}
和 在里面的div
.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;
}
试试这可能对你有用
答案 8 :(得分:0)
样品溶液。检查,如果这是您所需要的。
<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上查看