Skip to content Skip to sidebar Skip to footer

Vertically Center Parent Div On Page

I know vertical alignment is tricky and there are plenty of questions about it on stackoverflow... I have been trying to figure this out for a few hours, with no success, so decide

Solution 1:

You can try flex like this :

body {
  margin: 0;
  padding: 0;
}

* {
  box-sizing: border-box;
}

.container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  text-align:center;
}

.logo {
  border: 1px solid red;
  min-height: 30px;
}

.login {
  flex: 1;
  border: 1px solid red;
  display: flex;
  flex-wrap: wrap;
  align-content: center;
}

#loginForm,
#SocialLoginForm {
  min-height: 45px;
  border: 1px solid green;
  flex: 00100%;
}
<divclass="container"><divclass="logo">
    My Logo goes here
  </div><divclass="login"><sectionid="loginForm">Login content</section><sectionid="SocialLoginForm"> social login content</section></div></div>

Solution 2:

You have a few display options:

display:table (ie8 and above)

html,
body {
  height: 100%;
  width: 100%;
}

body {
  display: table;
  margin: 0;
}

body>div {
  display: table-row;
}

body>div+div {

  display: table-cell;
  height: 100%;
  vertical-align: middle;
  text-align:center;
  }
<div>
  My Logo goes here
</div><div><sectionid="loginForm">lg form</section><sectionid="SocialLoginForm">socl form</section></div>
display:flex;

body {
  margin: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

div+div {
  margin: auto;
}
<div>
  My Logo goes here
</div><div><sectionid="loginForm">lg form</section><sectionid="SocialLoginForm">socl form</section></div>
display:grid;

body {
  height: 100vh;
  margin:0;
  display: grid;
  grid-template-rows: auto 1fr;
}

div+div {
  margin: auto;
}
<div>
  My Logo goes here
</div><div><sectionid="loginForm">lg form</section><sectionid="SocialLoginForm">socl form</section></div>

All display method require a container with an height set.

there can be also method with transform, absolute, inline-block but no need to use tricks nowdays ;)

Solution 3:

body, html{
    display:table;
    height:100%;
    width:100%;
}
.my-container{
    display:table-cell;
    vertical-align:middle;
}
.my-container.your-div{
    width:150px;
    height:150px;
    border:1px solid #e3e3e3;
    margin:0 auto;   
}
<html><body><divclass="my-container"><divclass="your-div"><div>My Logo goes here<div><div><sectionid="loginForm">loginForm</section><sectionid="SocialLoginForm">SocialLoginForm</section></div></div></div></body></html>

Solution 4:

If you dont mind using preprocessor such as Sass. I would recommend using a mixin like that:

@mixin center($axis: "both") {
  position: absolute;
  @if $axis == "y" {
  -moz-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  -o-transform: translateY(-50%);
  -webkit-transform: translateY(-50%);
  top: 50%;
  transform: translateY(-50%);
 }
 @if $axis == "x" {
   -moz-transform: translateX(-50%);
   -ms-transform: translateX(-50%);
   -o-transform: translateX(-50%);
   -webkit-transform: translateX(-50%);
   left: 50%;
   transform: translateX(-50%);
 }
 @if $axis == "both" {
   -moz-transform: translate(-50%, -50%);
   -ms-transform: translate(-50%, -50%);
   -o-transform: translate(-50%, -50%);
   -webkit-transform: translate(-50%, -50%);
   left: 50%;
   top: 50%;
   transform: translate(-50%, -50%);
   }
  }

body {
  position: relative;
  .inner-div {
    @include center(); //both horizontaly and vertically pass 'x' or 'y' to center veritcally or horizontal
  }
}

Post a Comment for "Vertically Center Parent Div On Page"