问题
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