在全屏div中垂直和水平居中div

use*_*893 0 html center

我在全屏显示的另一个div中垂直和水平居中放置div时遇到问题。子div的宽度和高度是固定的。

这是代码:

html, body {
  height: 100%;
  margin: 0;
}
#header { 
  overflow: hidden;
  width:100%;
  height: 100%;

  background: orange;
  background-size:cover;
  -webkit-background-size:cover;

  display: table;
} 
#wrap {
  height: 200px;
  width: 500px;
  background:white;
  margin:0px auto;
}
Run Code Online (Sandbox Code Playgroud)
<div id="header">
  <div id="wrap">
    <h1>Header</h1>
    <div id="some-text">Some text some text some text some text</div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

Ori*_*iol 5

您可以尝试绝对居中:

#wrap {
  /* Absolute centering: */
  position: absolute;
  top: 0; right: 0; bottom: 0; left: 0;
  margin: auto;

  /* It needs a height and a width: */
  height: 200px;
  width: 500px;
}
Run Code Online (Sandbox Code Playgroud)

html, body {
  height: 100%;
  margin: 0;
}
#header { 
  overflow: hidden;
  width:100%;
  height: 100%;
  background: orange;
  background-size:cover;
  -webkit-background-size:cover;
  display: table;
} 

#wrap {
  height: 200px;
  width: 500px;
  background:white;
  position: absolute;
  top: 0; right: 0; bottom: 0; left: 0;
  margin: auto;
}
Run Code Online (Sandbox Code Playgroud)
<div id="header">
  <div id="wrap">
    <h1>Header</h1>
    <div id="some-text">Some text some text some text some text</div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)