css horizontal vertical center box

<div class = "Container"> 
  <div class = "Content"> I cassette contents </ div> </ div>    / * Method a: margin position: * Auto / .container { position: relative; } .content { position: Absolute; Top: 0; left: 0; right: 0; bottom: 0; margin: Auto; } / * NOTE: when the content no width height will fill the entire space may be a max-height restricted * / / * method two: Top position: left 50%: 50%; margin-Top: -50% * height margin-left: -50% * width * / .container { position: relative; height: 800px; } .content { width: 200px; height: 200px; background: Red; position: Absolute; Top: 50%; left: 50%; margin-Top: -100px; margin-left: -100px; } / * Method three: * Translate position / .container { position: relative; height: 800px; } {.content width: 200px; height: 200px; background: Red; position: Absolute; left: 50%; Top: 50%; -webkit-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%); } / * method four: the display: Flex * / .container { height: 800px; / * parent elements can not set the percentage, you need to have a certain height * / Run the display: Flex; / * needed for compatibility reasons * / The justify-Content: Center; align = left-items: Center; } .content { width: 200px; height: 200px; background: Red; } / * method five: parent: to U-: Table-Cell * / .container { width: 1366px; height: 800px; the display: Table-Cell; text-align = left: Center; Vertical-align = left: Middle; } .content { width: 200px; height: 200px; background: red; display: inline-block; }

  

Guess you like

Origin www.cnblogs.com/xiyuyizhihua/p/11125754.html