【问题标题】:How to place a div on the right side with absolute position如何以绝对位置将div放在右侧
【发布时间】:2012-04-01 21:25:24
【问题描述】:

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

我尝试使用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>

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

请查找示例here

请帮我解决这个问题。

【问题讨论】:

标签: html css css-position absolute


【解决方案1】:
yourbox{
   position:absolute;
   right:<x>px;
   top  :<x>px;
}

将其定位在右上角。请注意,该位置取决于不是static 定位的第一个祖先元素!

编辑:

I updated your fiddle.

【讨论】:

    【解决方案2】:

    您可以尝试以下方法吗:

    float: right;
    

    【讨论】:

      【解决方案3】:

      我假设您的容器元素可能是position:relative;。这意味着对话框将根据容器而不是页面进行定位。

      你能把标记改成这个吗?

      <html>
      <body>
          <!-- 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>
          <div class="container">
              <!-- Page contents starts here. These are dynamic-->
              <div class="row">
                  <div class="span12 inner-col">
                      <h2>Documents</h2>
                  </div>
              </div>
          </div>
      </body>
      </html>
      

      使用主容器外的对话框,您可以使用相对于页面的绝对定位。

      【讨论】:

        【解决方案4】:
        yourbox {
           position: absolute;
           left: 100%;
           top: 0;
        }
        

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

        【讨论】:

        • left:100% 将 div 的 LEFT 边缘放在窗口的最右侧,因此 div 的其余部分被隐藏。 left:94% 为我工作。很好的解决方案,谢谢。
        • 我大笑起来。这假设 yourbox 是窗口宽度的 100%XDXDXDXD
        • 虽然left: 100% 并不总是您想要的,但在我的情况下,它是完美的,因为它相对于 定位了 div 的 left 边缘其容器的右侧边缘。
        【解决方案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>
        

        【讨论】:

          【解决方案6】:

          简单,使用绝对定位,而不是指定顶部和左侧,而是指定顶部和右侧!

          例如:

          #logo_image {
              width:80px;
              height:80px;
              top:10px;
              right:10px;
              z-index: 3;
              position:absolute;
          }
          

          【讨论】:

          • (-‸ლ) 这行得通。 (MDN link 怀疑者)
          【解决方案7】:

          右上角试试这个:

          position: absolute;
          top: 0;
          right: 0;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-10-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多