YaoRaoLov 2009-11-21 13:56 采纳率: 50%
浏览 784
已采纳

如何在 div 中绝对居中定位元素?

I need to place a div (with position:absolute;) element in the center of my window. But I am having problems doing so, because the width is unknown.

I tried this. But it needs to be adjusted as the width is responsive.

.center {
  left: 50%;
  bottom:5px;
}

Any ideas?

转载于:https://stackoverflow.com/questions/1776915/how-to-center-absolutely-positioned-element-in-div

  • 写回答

26条回答 默认 最新

  • lrony* 2009-11-21 16:21
    关注

    <body>
      <div style="position: absolute; left: 50%;">
        <div style="position: relative; left: -50%; border: dotted red 1px;">
          I am some centered shrink-to-fit content! <br />
          tum te tum
        </div>
      </div>
    </body>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(25条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 DBIF_REPO_SQL_ERROR
  • ¥15 根据历年月数据,用Stata预测未来六个月汇率
  • ¥15 DevEco studio开发工具 真机联调找不到手机设备
  • ¥15 请教前后端分离的问题
  • ¥100 冷钱包突然失效,急寻解决方案
  • ¥15 下载honeyd时报错 configure: error: you need to instal a more recent version of libdnet
  • ¥15 距离软磁铁一定距离的磁感应强度大小怎么求
  • ¥15 霍尔传感器hmc5883l的xyz轴输出和该点的磁感应强度大小的关系是什么
  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交