无宽度定心div

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无宽度定心div相关的知识,希望对你有一定的参考价值。

The idea here is that you contain the content you want to center in two divs, an outer one and an inner one. You float both divs so that their widths automatically shrink to fit your content. Next, you relatively position the outer div with it's right edge in the center of the container. Lastly, you relatively position the inner div the opposite direction by half of its own width (actually the outer div's width, but they are the same). Ultimately that centers the content in whatever container it's in.

Check out the pictures on the website and it'll be clear.

You may need that empty div at the end if you depend on your "product" content to size the height for the "product_container".
  1. <div class="product_container">
  2. <div class="outer-center">
  3. <div class="product inner-center">
  4. </div>
  5. </div>
  6. <div class="clear"></div>
  7. </div>
  8.  
  9.  
  10. .outer-center {
  11. float: right;
  12. right: 50%;
  13. position: relative;
  14. }
  15. .inner-center {
  16. float: right;
  17. right: -50%;
  18. position: relative;
  19. }
  20. .clear {
  21. clear: both;
  22. }

以上是关于无宽度定心div的主要内容,如果未能解决你的问题,请参考以下文章

灵活高度垂直定心(IE8+)

超级定心丸:刚刚管理员重磅爆料手机挖矿一直会进行下去,直到挖矿停止!在也不用担心以后无pi可挖了!

如何用CSS使图片自适应显示宽度

div内容超出宽度时如何隐藏?如何显示省略标记?

解释 d3 定心力

如何使用 vanilla JavaScript 查找 div 的宽度?