user3415893
user3415893

Reputation: 69

Center div vertically and horizontally in fullscreen div

I have a problem with centering div vertically and horizontally in another div that is fullscreen. The width and height of child div are fixed.

Here is the code:

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;
}
<div id="header">
  <div id="wrap">
    <h1>Header</h1>
    <div id="some-text">Some text some text some text some text</div>
  </div>
</div>

Upvotes: 0

Views: 5593

Answers (2)

Devin
Devin

Reputation: 7720

Simply change your CSS like this:

html, body {
    width:100%;
    height: 100%;
    margin: 0;
}
#header {
    overflow: hidden;
    width:100%;
    height: 100%;
    background: orange;
    background-size:cover;
    -webkit-background-size:cover;
}
#wrap {
    height: 200px;
    width: 500px;
    background:white;
    margin:0px auto;
    position:absolute;
    top:50%;
    left:50%;
    margin-top:-100px;
    margin-left:-250px;
}

html, body {
  width:100%;
  height: 100%;
  margin: 0;
}
#header {
  overflow: hidden;
  width:100%;
  height: 100%;
  background: orange;
  background-size:cover;
  -webkit-background-size:cover;
}
#wrap {
  height: 200px;
  width: 500px;
  background:white;
  margin:0px auto;
  position:absolute;
  top:50%;
  left:50%;
  margin-top:-100px;
  margin-left:-250px;
}
<div id="header">
  <div id="wrap">
    <h1>Header</h1>
    <div id="some-text">Some text some text some text some text</div>
  </div>
</div>

See fiddle here

Explanation:

This is one of the most common and oldest approaches to absolute centering elements with CSS when using fixed width elements. It simply consists in applying a position:absolute to the element and a top and left 50% value. While this sounds as it should work by itself, the element has properties of its own, such as width and height, so we need to apply a margin-left and margin-top equal to half of the size of the element (in this particular case, 100px and 250px)

Upvotes: 1

Oriol
Oriol

Reputation: 288500

You can try absolute centering:

#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;
}

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;
}
<div id="header">
  <div id="wrap">
    <h1>Header</h1>
    <div id="some-text">Some text some text some text some text</div>
  </div>
</div>

Upvotes: 5

Related Questions