0

I've been learning CSS and am now practicing by trying to replicate basic websites, but I've stumbled across a problem!

I'm trying to vertically align a box so that it is always in the middle, and will automatically scale if I make the browser vertically smaller. So far I've tried to replicate what I've done horizontally (normally margin: 0 auto;) but it isn't working.

My relevant HTML and CSS so far look like this:

*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* Global Values */

html{
  font-family: "Arial";
  font-size: 16px;
  line-height: 1.2;
  height: 100%;
}

body{
  background-color: #f9f9f9;
  height: 100%;
}

.container{
  display: block;
  min-width: 240px;
  max-width: 768px;
  width: 100%;
  
  /*----------------------------------------------*/
  /* This is to make sure that the container height is always the same size as the browser. */
  
  min-height: 100px; 
  height: 100%;  
  
  /*----------------------------------------------*/
  margin: 0 auto;
  text-align: center;
  border-style: solid;
}


header{
  border: solid;
  min-height: 100px;
  margin: auto 5%;
}
<body>
    
    <div class="container">
      
      <header>
        
        <h1>Jon Phillips</h1>
        <p>User Interface Designer</p>
        
        <nav class="contact">
          
          <a href="mailto:jon@jonphillips.ca" target="_blank"><p>Contact</p></a>

        </nav>
      
      </header>
      
    </div>

  </body>

I'm showing the borders so I can see what's going on, and am sure that (like horizontal centering) my margins need to be automatic. When I've done this horizontally, it's worked fine...

Does anyone have a recommendation on what to do??

Thanks in advance!

5 Answers5

0

Flexbox is your friend! You need to add the following to your container styles

  display: flex;
  align-items: center;
  justify-content: center;

We're setting the display to flex/flexbox with display: flex; and aligning everything to the center with align-items: center; and justify-content: center;

With all the vendor-prefixes it looks like:

  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;

*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* Global Values */

html{
  font-family: "Arial";
  font-size: 16px;
  line-height: 1.2;
  height: 100%;
}

body{
  background-color: #f9f9f9;
  height: 100%;
}

.container{
  display: block;
  min-width: 240px;
  max-width: 768px;
  width: 100%;
  
  /*----------------------------------------------*/
  /* This is to make sure that the container height is always the same size as the browser. */
  
  min-height: 100px; 
  height: 100%;  
  
  /*----------------------------------------------*/
  margin: 0 auto;
  text-align: center;
  border-style: solid;
  
   display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
       -ms-flex-align: center;
          align-items: center;
    -webkit-box-pack: center;
       -ms-flex-pack: center;
     justify-content: center;
}


header{
  border: solid;
  min-height: 100px;
  margin: auto 5%;
}
<body>
    
    <div class="container">
      
      <header>
        
        <h1>Jon Phillips</h1>
        <p>User Interface Designer</p>
        
        <nav class="contact">
          
          <a href="mailto:jon@jonphillips.ca" target="_blank"><p>Contact</p></a>

        </nav>
      
      </header>
      
    </div>

  </body>
Dale
  • 1,911
  • 11
  • 18
  • ...and if someone wants just to center one child of flex parent, there is `align-self: center` – Zydnar Oct 27 '17 at 21:05
0

Check this: https://codepen.io/danieldd/pen/pdooVN

<div class="container">
  <div class="item-centered"></div>
</div>
.container {
  height: 400px;
  width: 100%;
  background-color: cyan;

  display: flex; /*this is needed for centering*/
  align-items: center; /*this center vertically childred elements*/
  justify-content: center; /*this center horizontally childred elements*/
}

.item-centered {  
  height: 200px;
  width: 200px;
  background-color: red;
}
Daniel Delgado
  • 4,813
  • 5
  • 40
  • 48
-1

Sadly the auto margins trick only works horizontally. In CSS3, you can use another trick. You start with top: 50%; and then subtract half the height of your container using transform: translateY(-50%);. The perspective(1px) is just to correct the calculation to a whole pixel and prevent issues in some browsers. See the three lines I've added to your CSS:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* Global Values */

html {
  font-family: "Arial";
  font-size: 16px;
  line-height: 1.2;
  height: 100%;
}

body {
  background-color: #f9f9f9;
  height: 100%;
}

.container {
  display: block;
  min-width: 240px;
  max-width: 768px;
  width: 100%;
  /*----------------------------------------------*/
  /* This is to make sure that the container height is always the same size as the browser. */
  min-height: 100px;
  height: 100%;
  /*----------------------------------------------*/
  margin: 0 auto;
  text-align: center;
  border-style: solid;
}

header {
  border: solid;
  min-height: 100px;
  margin: auto 5%;
  position: relative;
  top: 50%;
  transform: perspective(1px) translateY(-50%);
}
<body>
  <div class="container">
    <header>
      <h1>Jon Phillips</h1>
      <p>User Interface Designer</p>
      <nav class="contact">
        <a href="mailto:jon@jonphillips.ca" target="_blank">
          <p>Contact</p>
        </a>
      </nav>
    </header>
  </div>
</body>
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
-1

On the parent of the item that you're trying to center, you can just use display: flex; and the child should center inside (with the rest of the styles that you already have set up).

This could also be achieved using absolute positioning, though it would be a few more lines:

 .parent {
      position: relative;
 }

 .child {
      /* Vertically center - will still need left / right & width adjustment otherwise */
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
 }

*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* Global Values */

html{
  font-family: "Arial";
  font-size: 16px;
  line-height: 1.2;
  height: 100%;
}

body{
  background-color: #f9f9f9;
  height: 100%;
}

.container{
  display: block;
  min-width: 240px;
  max-width: 768px;
  width: 100%;
  display: flex;
  
  /*----------------------------------------------*/
  /* This is to make sure that the container height is always the same size as the browser. */
  
  min-height: 100px; 
  height: 100%;  
  
  /*----------------------------------------------*/
  margin: 0 auto;
  text-align: center;
  border-style: solid;
}


header{
  border: solid;
  min-height: 100px;
  margin: auto 5%;
  width: 100%;
}
<body>
    
    <div class="container">
      
      <header>
        
        <h1>Jon Phillips</h1>
        <p>User Interface Designer</p>
        
        <nav class="contact">
          
          <a href="mailto:jon@jonphillips.ca" target="_blank"><p>Contact</p></a>

        </nav>
      
      </header>
      
    </div>

  </body>
Logan
  • 1,376
  • 1
  • 8
  • 10
-1

There are different approuces to solve this problem, the easyest and with a good compatibility is the use of the "table-cell display", so in your case adding this to your CSS:

.container {
  display:table-cell;
  height:100%;
  vertical-align:middle
}

The point is that a correct implementation of a table-cell should consider the parent element as a table, your parent element is the body itself, so do change your CSS into:

body{
  background-color: #f9f9f9;
  height: 100%;
  width:100%;
  display:table
}

http://jsfiddle.net/9tghnydg/

On a second though the height propriety on the .container is not strictly necessary, acting it as the unique cell of the body table:

.container {
  display:table-cell;
  vertical-align:middle
}

http://jsfiddle.net/9tghnydg/1/

holden
  • 1,721
  • 12
  • 19