Position center of div tag, which width is unknown advanced

一曲冷凌霜 提交于 2020-01-03 03:41:07

问题


I have div with no fixed with. I want this div position in the center of screen.

I know how to make this with jquery, but with pure css, I did not find any solution. How do you make this happen with pure css?


回答1:


Assuming you want to shrink-wrap the contents of the div (otherwise centring it is a pointless exercise as its width will be the width of the container):

  • Set display: inline-block; on the div to make it shrink-wrap
  • Set text-align: center; on the div's parent element to centre the div
  • Set text-align: left; on the div to override the centring of its inline content that it would inherit from its parent.


来源:https://stackoverflow.com/questions/13580678/position-center-of-div-tag-which-width-is-unknown-advanced

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!