2

How can I center the #box div vertically centered on the page? I tried vertical-align: middle; but it doesn't work. You can check the site live here

This is the css:

iframe {
  position: fixed;  
  width: 100vw; 
  height: 100vh;  
  border: none; 
  margin: 0; 
  padding: 0; 
  overflow: hidden; 
  z-index: 999999;
}

body {
  background-color: #000;
}

#box {
  text-align: center;
  vertical-align: middle;
}

#link {
  color: #FFF;
}

#download {
  color: #FFF;
}
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
TravelWhere
  • 327
  • 1
  • 5
  • 17

6 Answers6

12

Method 1 (position, transform-translate):

* {
  padding: 0;
  margin: 0;
}
.parent {
  position: relative;
  left: 0;
  top: 0;
  width: 100vw;
  height: 100vh;
  background-color: gray;
}
.child {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 10em;
  height: 5em;
  background-color: white;
  box-shadow: 1px 1px 10px rgba(0,0,0,.4);
}
<div class="parent">
  <div class="child"></div>
</div>

Method 2 (Flexbox):

* {
  padding: 0;
  margin: 0;
}

.parent {
  position: fixed;
  left: 0;
  top: 0;
  width: 100vw;
  height: 100vh;
  background-color: gray;
  display: flex;
}

.child {
  margin: auto;
  width: 10em;
  height: 5em;
  background-color: white;
  box-shadow: 1px 1px 10px rgba(0, 0, 0, .4);
}
<div class="parent">
  <div class="child"></div>
</div>
shloosh
  • 525
  • 7
  • 10
Andrei Fedorov
  • 3,689
  • 2
  • 13
  • 25
2

You can do that with flexboxes:

html,body {
  height: 100%;
  min-height: 100%;
}
.Aligner {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  min-height: 100%;
}

.Aligner-item {
  max-width: 50%;
  background-color: red;
}
<div class="Aligner">
  <div class="Aligner-item">…</div>
</div>
Victor Leontyev
  • 8,488
  • 2
  • 16
  • 36
1

add a wrapper div; box-wrapper

#box-wrapper {
    position: relative;
}
#box {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

by the way much detailed explanation can be found on css-tricks.com/centering-css-complete-guide

0
.parent {
  text-align: center;
  white-space: nowrap;
}     
.parent:before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
  margin-right: -0.25em;
}
.child {
  display: inline-block;
  vertical-align: middle;
  width: 300px;
}
0

HTML

<div class="container"><div class="your-div"></div></div>

CSS

body, html{
    display:table;
    height:100%;
    width:100%;
}
.container{
    display:table-cell;
    vertical-align:middle;
}
.container .your-div{
    width:150px;
    height:150px;
    background:green;
    margin:0 auto;   
}
Billu
  • 2,733
  • 26
  • 47
0

The three lines below should suffice:

.container {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
James Chang
  • 608
  • 8
  • 21