0

I want to center below wrapper inside the parent div element with a height equal to windows view height, using CSS.

HTML

<div class="main-header">
   <div class="wrapper">center div</div>
</div>

CSS

.main-header{
color: #fff;
background: #16a8ec;
padding: 10px;
height: 100vh;
position: relative; }

.wrapper{
position: absolute;
left: 0px;
right: 0px;
transform: translate(0px, 50%); /* i used transform but it doesn't work */
-webkit-transform: translate(0px, 50%); /* i used transform but it doesn't work */ }

Please help me guys, thanks.

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
bellabelle
  • 908
  • 7
  • 13

5 Answers5

2

With flexbox :

.main-header{
  color: #fff;
  background: #16a8ec;
  padding: 10px;
  height: 100vh;
  position: relative;
  /* Flexbox */
  display: flex;
  align-items: center; 
}

.wrapper{
  /* your wrapper style */
}

With absolute position :

.main-header{
  color: #fff;
  background: #16a8ec;
  padding: 10px;
  height: 100vh;
  position: relative; 
}

.wrapper{
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}
Victor Allegret
  • 2,344
  • 3
  • 18
  • 31
1

Do you want something like this?

The changes:

  1. html and body element should be set to height of the window(viewport), so I used the property height:100vh, then body by default has the margin set to 8px, so I used the property margin:0px.

  2. To center the div inside you just need to use, text-align: center for the div with class main-header.

html,
body {
  height: 100vh;
  margin: 0px;
}

.main-header {
  color: #fff;
  background: #16a8ec;
  padding: 10px;
  box-sizing: border-box;
  height: 100vh;
  position: relative;
  text-align: center;
}

.wrapper {
  position: absolute;
  left: 0px;
  right: 0px;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
}
<div class="main-header">
  <div class="wrapper">center div</div>
</div>
Naren Murali
  • 19,250
  • 3
  • 27
  • 54
0

Check this: Added top:46% and left:46%, you can also do it other way around.

.main-header{
color: #fff;
background: #16a8ec;
padding: 10px;
height: 100vh;
position: relative; }

.wrapper{
position: absolute;
top:46%;
left: 46%;
transform: translate(0px, 50%); /* i used transform but it doesn't work */
-webkit-transform: translate(0px, 50%); /* i used transform but it doesn't work */ }
<div class="main-header">
   <div class="wrapper">center div</div>
</div>
bhansa
  • 7,282
  • 3
  • 30
  • 55
0

You just have to add top: 0; bottom: 0 to your .wrapper like this:

.main-header{
color: #fff;
background: #16a8ec;
padding: 10px;
height: 100vh;
position: relative; }

.wrapper{
position: absolute;
left: 0px;
right: 0px;
transform: translate(0px, 50%); /* i used transform but it doesn't work */
-webkit-transform: translate(0px, 50%); /* i used transform but it doesn't work */ 
text-align: center;
top: 0;
bottom: 0;
}
<div class="main-header">
   <div class="wrapper">center div</div>
</div>

You can also check this Fiddle

kravisingh
  • 1,640
  • 12
  • 16
0

Just give display: table to parent and display: table-cell; vertival-align: middle; to child that will work for you.

Working example.

<!DOCTYPE html>
<html>

<head>
    <style>
        .main-header {
            color: #fff;
            background: #16a8ec;
            padding: 10px;
            height: 100vh;
            width: 100%;
            display: table;
        }
        
        .wrapper {
            display: table-cell;
            vertical-align: middle;
        }
    </style>
</head>

<body>
    <div class="main-header">
        <div class="wrapper">center div</div>
    </div>
</body>

</html>
Nitheesh
  • 19,238
  • 3
  • 22
  • 49