简体   繁体   English

我如何在父div旁边有一个固定的div?

[英]How can I have a fixed div next to a parent div?

I'm trying to get a div with a button next to a parent div. 我正在尝试在父div旁边添加一个带按钮的div。 Like this: 像这样:

____________
|relative  |
|          |
|          |
|          |_______
|          | fixed |
|          |_______|
|          |
|          |
|__________|

The fixed div has to be fixed during scrolling but at all time next to the parent and not under or above its parent. 固定div必须在滚动期间固定,但始终固定在父级旁边,而不是在父级之下或之上。

How can I accomplish this? 我该怎么做?

Use position:fixed for the second div 使用position:fixed于第二格

HTML HTML

<div class="main">
    zx
</div>
<div class="fix">
    <button>Click</button>
</div>

CSS CSS

html, body{
    height:100%;
    margin:0
}
.main{
    height:100%;
    width:50%;
    background:grey;
}
.fix{
     position:fixed;
     top:50%;
     border:red solid 2px;
     background:yellow;
     left:50%
}

DEMO DEMO

Your main problem is that the element with position: fixed is always relative to the body. 您的主要问题是position: fixed的元素始终相对于身体。 It behaves differently from elements with position: absolute , which is relative to parent element with position: relative declared. 它的行为与具有position: absolute元素的行为不同,后者相对于具有position: relative声明的父元素。

So the main problem is, that if you set left to the fixed element, it sticks to left edge of body element, even if it's parent is positioned relative. 因此,主要问题是,如果将left设置为fixed元素,则即使它的父元素相对放置,它也会粘贴到body元素的左边缘。 But you could use a trick, and skip left declaration for fixed element. 但是您可以使用技巧,并跳过固定元素的left声明。

 .main { /*just for visualisation*/ width: 300px; height: 1500px; background: #ccc; } .main, .fake-wrapper { float: left; } .fixed { position: fixed; top: 50%; } 
 <div class="main"> <!-- your content --> </div> <div class="fake-wrapper"> <div class="fixed"> <a href="/">click!</a> </div> </div> 

Here's a JSFiddle example 这是一个JSFiddle示例

you're looking for position: absolute , not fixed -- if your parent has the class parent and the button has the class button , what you need is similar to (assuming button has a fixed width of 100px for this): 您正在寻找position: absolute ,不固定-如果您的父级具有class parent级,而按钮具有class button ,则您需要的类似于(假设按钮的固定宽度为100px):

.parent { position: relative; }
.button { position: absolute; top: 45%; right: -100px; }

here's an example fiddle (added some width/heights to demonstrate, these should come from your content instead) http://jsfiddle.net/WpnP4/ 这是一个示例小提琴(添加了一些宽度/高度来演示,这些宽度/高度应该来自您的内容) http://jsfiddle.net/WpnP4/

Edit: just realized the question is not 100% clear -- I assumed you wanted the button to be next to a specific element and scroll with the screen. 编辑:刚刚意识到问题不是100%清楚-我以为您希望按钮位于特定元素旁边并在屏幕上滚动。 Use position:fixed if you want the button element to stay fixed in the screen. 如果希望按钮元素在屏幕中保持固定,请使用position:fixed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 div 中固定背景? - How can I have fixed background in a div? 如何在子div的父集中居中并将其固定在顶部? - How do I center child div in parent and have it fixed to the top? 如何使固定的div继承父级的宽度? - How can I make a fixed positioned div inherit width of parent? 我怎样才能有垂直固定的div元素? - How can I have vertically fixed div element? 如何使包含图像的 div 具有固定大小? - How can I make div that contains images to have fixed size? 如何删除元素父级的下一个div? - How can I remove the next div of the parent of an element? 如何创建子div来填充固定高度的父div中剩余的垂直空间? - How can I create a child div that fills in the remaining vertical space in a parent div of a fixed height? 如何将图像浮动到居中的div旁边? - How can I have an image float next to a centered div? 如何将子div放置在父div的固定位置上,当我更改父位置时,子应该自动移动? - How can i place a child div on a fixed position of a parent div that when i change place of parent then child should move automatically? 如何使用具有“position:fixed”元素的代码段中的代码? 我无法让它留在我的内心 <div> 身高(父母) - how can I use a code from a snippet which have a “position:fixed” element ? I can't make it stay inside my <div>'s height (parent)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM