CSS居中绝对定位元素的方法,有很多种,下面是我收集的几种

1,div宽度未知1

 <body>
  <div style="position: absolute; left: 50%;">
    <div style="position: relative; left: -50%; border: dotted red 1px;">
      没有宽度<br />
      照样居中,嘿嘿嘿
    </div>
  </div>
</body>

demo:https://jsfiddle.net/skura23/0123wmsg/

 

2,div宽度未知2

 <div class="outer">

demo: http://jsfiddle.net/skura23/6xo11zwv/210/

ps:此方法适合ie8以上的浏览器

 

3,div宽度已知

 <body>
  <div>
    <div >
      居中蓄力中
    </div>
  </div>
</body>

#content {
  position: absolute; 
  left: 0; 
  right: 0; 
  margin-left: auto; 
  margin-right: auto; 
  width: 100px; /* 要设定宽度 */
}

相关文章:

  • 2021-11-30
  • 2021-11-30
  • 2021-10-14
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2021-11-30
  • 2021-11-30
猜你喜欢
  • 2022-12-23
  • 2021-11-30
  • 2021-11-30
  • 2021-11-30
  • 2022-12-23
  • 2021-09-15
相关资源
相似解决方案