Method One
Idea: The display setting parent element is: table, child element: cell-table, so you can use Vertical-align:center to achieve horizontal center
Advantage: Parent element can dynamically change height (attribute of Table Element)
Cons: IE8 not supported below
<! DOCTYPE html>
Method Two:
Idea: Use an empty label span to set his vertical-align baseline to the middle, and let him be the Inline-block, with a width of 0
Cons: More than a useless empty label, Display:inline-blockie 6 7 is not supported (add on: _zoom1;*display:inline).
Of course, you can use pseudo-elements instead of span tags, but IE support is not good, but it's something.
<! DOCTYPE html>
Method Three
Idea: Sub-element absolute positioning, distance from top 50%, left 50%, then use CSS3 transform:translate (-50%;-50%)
Pros: Tall, can be used in the WebKit kernel browser
Cons: Unsupported IE9 The following does not support transform properties
| 123456789101112131415161718192021222324252627 |
<!DOCTYPEhtml><htmllang="en"><head> <metacharset="UTF-8"> <title>未知宽高元素水平垂直居中</title></head><style>.parent3{ position: relative; height:300px; width: 300px; background: #FD0C70;}.parent3 .child{ position: absolute; top: 50%; left: 50%; color: #fff; transform: translate(-50%, -50%);}</style><body><divclass="parent3"> <divclass="child">hello world-3</div> </div></body></html> |
Method Four:
Idea: Using the CSS3 Flex layout
Advantages: Simple and fast
Disadvantage: Compatibility is not good, see more: http://caniuse.com/#search =flex
<! DOCTYPE html>
CSS unknown wide height element horizontally vertically centered