position 绝对定位的居中问题。

我在表格td里面放了一个DIV。给TD设置的relative;然后这个div absolute; 我设置了。left0 right:0 maigin o auto 但是还是没有居中。。--是什么原因呢。我觉得可能是因为这个div比td的宽度还要宽。所以失效了。。
怎么解决么。图片描述

长宽高这个框应该在委3/400 这个td里面。。。

阅读 4.2k
6 个回答

方法一:
div{

position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin:auto;

}
方法2:
div{

position: absolute;
left: 50%;
top: 50%;
transform:translate(-50%,-50%);

}

用了position absolute,margin 0 auto 就没用啦

absolute不适用于margin:0 auto
你要写left: 50%, margin-left :-xxxpx
xxx为你这个div一半的宽度

为什么不用text-align:center

你现在的代码是不是先写{委/3/4002.123...}然后再写的{长宽高},是的话试试把{长宽高}代码的位置提前到{委/3/4002.123...}的前面,不清楚你代码具体的写法,这个小办法估计可能会成

有个absolute居中的技巧你可以试下:
div{
position: absolute;
top:0;
left: 0;
right: 0;
bottom: 0;
margin:auto;
}
原因:For absolutely positioned elements, the top, right, bottom,and left properties specify offsets from the edge of the element's containing block (what the element is positioned relative to).The margin of the element is then positioned inside these offsets.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题