开发者

css: positioning in relative to other elements

开发者 https://www.devze.com 2023-03-18 20:39 出处:网络
Assume I have two elements, div1 and div2 and I have defined the css for div1. I want to specify the position of div2 relative to div1, for example say div2 s开发者_开发技巧hould be 30px to the left o

Assume I have two elements, div1 and div2 and I have defined the css for div1. I want to specify the position of div2 relative to div1, for example say div2 s开发者_开发技巧hould be 30px to the left of div1. Is this possible?

Thanks Bruce


Yes. set the position of div 1 to relative. Set the position of div2 to absolute with the left value of 30px. Div2 will be positioned absolutely not to the body, but to the parent div which has a position of relative.


Yes (unless I'm really simplifying your question):

div1 { position: absolute; left:0; top:0; }

div2 { position: relative; left:30px; }


If div1 is exactly where you need to be on the page already, then I think Phil's answer with position: relative is the correct one. If that's not it, here are a few other alternatives.

Clarifying question, are you wanting div2 to appear 30px to the left of div1, or do you want div2 to have a 30px left margin to space it out on the right side of div1?

If it's the former, make sure div2 is before div1 in your html, and try this:

div1, div2 { float: left; }
div2 { margin-right: 30px; }

If it's the latter, make sure div1 is before div2, and try something like this:

div1, div2 { float: left; }
div2 { margin-left: 30px; }
0

精彩评论

暂无评论...
验证码 换一张
取 消