To center horizontally, use text-align:center
.
To center vertically, one can only use vertical-align:middle
if there is another element in the same row that it is being aligned to.
See it working here.
We use an empty span with a height of 100%, and then put the content in the next element with a vertical-align:middle.
There are other techniques such as using table-cell or putting the content in an absolutely positioned element with top, bottom, left, and right all set to zero, but they all suffer from cross browser compatibility issues.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…