-1

I'm trying to vertical align a container div within a jumbotron.

Because the height of the container (and so the jumbotron also) is variable top: 50%; margin-top: -'height * 0.5' won't work.

@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css');
 .banner {
  padding: 48px 0px 48px 0px;
  text-align: center;
}
.banner:after {
  content: "";
  display: block;
  padding-top: 12.5%;
}

  .container {
  background: red !important; /* For testing vertical alignment */
  }
<header class="jumbotron banner">
  <div class="container">
    <h1>Title</h1>

    <p>Subtitle</p>
    <p> <a class="btn btn-lg btn-primary" href="#" role="button">Button</a>

    </p>
  </div>
</header>
Berendschot
  • 3,026
  • 1
  • 21
  • 43

3 Answers3

2

you should just center it using padding, this is the default way of doing it;

@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css');
 .banner {
  padding: 5% 0px;
  text-align: center;
}
.banner:after {
  content: "";
  display: block;
  
}

  .container {
  background: red !important; /* For testing vertical alignment */
  }
<header class="jumbotron banner">
  <div class="container">
    <h1>Title</h1>

    <p>Subtitle</p>
    <p> <a class="btn btn-lg btn-primary" href="#" role="button">Button</a>

    </p>
  </div>
</header>
maioman
  • 18,154
  • 4
  • 36
  • 42
1

To align div vertically center you can use css like

position: relative;
top: 50%;
transform: translatey(-50%);

ex

.outer{
    height:100px;
    background:gray;
}
.inner{
    background:green;
    position:relative;
    top:50%;
    transform:translatey(-50%);
}
<div class="outer">
    <div class="inner" >
                  <p>sample text</p>
        </div>
</div>
Swapnil Motewar
  • 1,080
  • 6
  • 12
-1

There are several ways you can get content aligned vertically. I am posting here one common and most recent trick.

  1. Give position: relative; to the parent element.
  2. To the child:

    .child-element { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }

See JSFIDDLE and comment below if you need further assistance.

Anwar Hussain
  • 450
  • 2
  • 12