固定div垂直和右对齐另一个div

时间:2011-06-29 22:26:04

标签: css position html

nav div必须垂直固定,并且位于容器div的右侧。

使用position:fixed;并设置值left和top,可以让div保持在同一个位置,但是左边的值取决于父div(page-container)居中。

<html>
<body>
<div id="page-container" style="width: 700px; height:1000px; margin-left: auto; margin-right: auto;background-color:moccasin;"> 

    <div class="content" style=" width: 340px; height:300px; float:left;background-color:mediumseagreen;"> 
        .content
    </div>

    <div class="nav" style="float:left;height: auto; width: 300px; display: block;background-color:lightblue;"> 
        .nav <br><br>This div must:<br> 
     1. be fixed vertically (if scrolling down, you should see me in the same place) <br>
     2. be aligned to the right side of <b>content</b>
    </div>
</div>
</body>
</html>

enter image description here

1 个答案:

答案 0 :(得分:1)

请参阅: http://jsbin.com/afenip/2

/* new css */
.nav {
    position: fixed;
    margin-left: 340px; /* same as the width on .content */
}