How can I have a fixed div next to a parent div?

前端 未结 3 1323
旧时难觅i
旧时难觅i 2020-12-22 05:05

I\'m trying to get a div with a button next to a parent div. Like this:

____________
|relative  |
|          |
|          |
|          |_______
|          |          


        
3条回答
  •  情深已故
    2020-12-22 05:40

    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):

    .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/

    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. Use position:fixed if you want the button element to stay fixed in the screen.

提交回复
热议问题