losthorse
0
Q:

css div vertical center

.parent {
  display: flex;
  justify-content: center;
  align-items: center;
}
8
/* No Flexbox */
.parent {
  position: relative;
}
.child {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

/* With Flexbox */

.parent {
  display: flex;
  flex-direction: column;
  justify-content: center;
}


9
.parent-class {
  display: flex;
  align-items: center;
  justify-content: space-around;
}
3

  <style>
.container { 
  height: 200px;
  position: 
  relative;
  border: 3px solid green; 
}

.center {
  margin: 0;
  
  position: absolute;
  top: 50%;
  left: 50%;
  -ms-transform: 
  translate(-50%, -50%);
  transform: translate(-50%, -50%);
}
</style>

<div 
  class="container">
  <div class="center">
    
  <p>I am vertically and horizontally centered.</p>
  </div>
</div> 
4

div
{
	display: block;
	margin-left: auto;
	margin-right: auto;
	width: 40%;
} 
0

New to Communities?

Join the community