1

Quite new here, I'll try my best to explain. I'm trying to do a one page bootstrap application with several <div> and a nav bar on the very top of the page. I'd like that <div> fill the height of browser window, with text in the center of that div.

My problems

  1. the text won't center
  2. if I remove the outer wrapper <div id="welcome" class="welcome"> and put the css on "container fill-height", the whole section becomes in the middle but not full screen wide. I have no idea why we need two wrappers for that <div class="textWelcome"><h2>welcome to first div</h2></div>.

Below is my code with comments to further explain the question. Thanks in advance for your help!

<div id="welcome" class="welcome">  // when user scroll to this div, it fills the screen under nev bar
   <div class="container fill-height">
      <div class="textWelcome">
          <h2> Welcome to first div </h2> //this text should be in the middle of the "welcome" div 
      </div>
   </div>
</div>

This is my current CSS which does not work:

.welcome{
    padding-top: 150px;
    padding-bottom: 150px;
    background: #E41b13; 
 }

.textWelcome{
   vertical-align: middle;
   font-family: 'Georgia';
   color: rgb(255, 255, 255); /*white text*/
   font-weight: bold
}

Update: Now everything seems ok except that text is not vertically centered

    <div class="container fill-height">
      <div class="row">
       <div class="col-lg-1"></div>
         <div id="hello" class="col-lg-10">
              <h1 style="text-align:center;"> Hello World</h1>
         </div>
       <div class="col-lg-1"></div>
     </div>
   </div>

and CSS:

.fill-height{
   padding-top: 150px;
   padding-bottom: 150px;
   background: #E41b13; 
   min-height: 100%;
   height:100vh;
 }

#hello{
  vertical-align: middle;
  font-family: 'Georgia';
  color: rgb(255, 255, 255); /*white text*/
  font-weight: bold
}
user4046073
  • 821
  • 4
  • 18
  • 39

2 Answers2

4

You can use the transform property:

#hello{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-family: 'Georgia';
color: rgb(255, 255, 255); /*white text*/
font-weight: bold
}

Pen: http://codepen.io/giannidk/pen/bgxjLW

gianni
  • 1,199
  • 2
  • 14
  • 28
1

Firstly put your code in container class.And in order to make all section in center, Use bootstrap . You can use text-align:center; property to make text in center. Below is the code:

 <div class="container">
     <div class="row">
      <div class="col-lg-1"></div>
      <div class="col-lg-10">
        <h1 style="text-align:center;"> Hello World</h1>
      </div>
     <div class="col-lg-1"></div>
  </div>
</div>
KiranPurbey
  • 335
  • 4
  • 15