简体   繁体   English

CSS 相对定位,不断变化

[英]CSS relative positioning, keeps changing

So Basically I have a <div> which contains two buttons.所以基本上我有一个<div>包含两个按钮。 I want to position these buttons (the div) in a certain point in the page within another.我想position 这些按钮(div)在另一个页面的某个点内。

I didn't really give it much thought and I set the positioning using relative and got it positioned where I wanted.我并没有真正考虑太多,我使用相对设置定位并将其定位在我想要的位置。 However, whenever I came back to it a couple of days later, its misplaced again and I haven't changed the CSS.但是,每当我几天后回到它时,它又放错了位置,我没有更改 CSS。

So here is my CSS for the div:所以这是我的 div 的 CSS:

#button_menu {
position: relative; 
top: 263px; 
left: 710px;  
width: 250px; 
}

and basically I want it always positioned in the same location within a particular div 'bodyArea'基本上我希望它始终位于特定 div 'bodyArea' 内的相同位置

<div id="bodyArea">

<div id="button_menu">
<input type="button" class="smallbutton" id="add" value="add"/>
<input type="button" class="smallbutton" id="remove" value="remove"/>
</div>

.....

I am assuming I am doing it wrong, so If somebody could explain to me how I can achieve what I actually want?我假设我做错了,所以如果有人可以向我解释我如何才能实现我真正想要的?

many thanks,非常感谢,

Give relative positioning to the element "bodyArea", and absolute position to "button_menu".给元素“bodyArea”相对定位,绝对 position 给“button_menu”。

Go sth like this: Go 像这样:

#bodyArea {position: relative; width: 250px;}
#button_menu {position: absolute; left: 100px; top: 100px;}

You must realize that the elements thats needs to fixed shud be positioned "absolute" and their position is relative to closest parent that is positioned "relative" (default is "body").您必须意识到需要修复的元素应该定位为“绝对”,并且它们的 position 相对于定位为“相对”的最近父级(默认为“主体”)。

You need the parent div to be position: relative;您需要父 div 是position: relative; and the child div to be position: absolute;子 div 是position: absolute; :

#bodyArea {
    position: relative;
}

#button_menu {
    position: absolute; 
    top: 263px; 
    left: 710px;  
    width: 250px; 
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM