如何在绝对位置右侧放置div

Aru*_*hny 33 html css css-position absolute

我有一个页面,必须在不打扰实际页面的情况下显示动态消息框.此消息框必须出现在页面的右上角,与现有内容重叠.

我试过用,position: absolute但后来我无法将它放在右上角.我也无法使用,left因为我支持Bootstrap的响应式设计.

这是一个示例标记

<html>
    <body>
        <div class="container">
            <!-- Need to place this div at the top right of the page-->
            <div class="ajax-message">
                <div class="row">
                    <div class="span9">
                        <div class="alert">
                            <a class="close icon icon-remove"></a>
                            <div class="message-content">
                                Some message goes here
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- Page contents starts here. These are dynamic-->
            <div class="row">
                <div class="span12 inner-col">
                    <h2>Documents</h2>
                </div>
            </div>
        </div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

此消息框应具有50%相对于消息框的宽度,.container并且消息框的左侧不应与其重叠.即我们应该能够点击/选择左侧的内容.

在这里找到样品.

请帮我解决这个问题.

Chr*_*oph 48

yourbox{
   position:absolute;
   right:<x>px;
   top  :<x>px;
}
Run Code Online (Sandbox Code Playgroud)

把它放在右角.注意,位置取决于未static定位的第一个祖先元素!

编辑:

我更新了你的小提琴.


小智 37

yourbox {
   position: absolute;
   left: 100%;
   top: 0;
}
Run Code Online (Sandbox Code Playgroud)

左:100%; 这是重要的问题!

  • `left:100%` 将 div 的左边缘放在窗口的最右侧,因此隐藏 div 的其余部分。`left:94%` 对我有用。很好的解决方案,谢谢。 (4认同)
  • 假设 `yourbox` 是窗口宽度的 100% (2认同)

Ale*_*nko 5

您可以使用“ translateX

<div class="box">
<div class="absolute-right"></div>
</div>

<style type="text/css">
.box{
    text-align: right;
}
.absolute-right{
    display: inline-block;
    position: absolute;
}

/*The magic:*/
.absolute-right{
-moz-transform: translateX(-100%);
-ms-transform: translateX(-100%);
-webkit-transform: translateX(-100%);
-o-transform: translateX(-100%);
transform: translateX(-100%);
}
</style>
Run Code Online (Sandbox Code Playgroud)


Yos*_*sky 5

尝试这个:

left: calc(100% - [the width of your div]px);
Run Code Online (Sandbox Code Playgroud)