Neiko Anglin
Neiko Anglin

Reputation: 75

Centering CSS Boxes(Html and Css)

I am trying to center these boxes in the middle of the screen both horizontally and vertically. Another question is how can I make it where it re-sizes automatically when I scale my page?

/*-------------------------
    Simple reset
--------------------------*/


*{
    margin:0;
    padding:0;
}


/*-------------------------
    General Styles
--------------------------*/


/*----------------------------
    Color Themes
-----------------------------*/
.nav-colors{
     position: relative;
      background: white;
      height: 200px;
      width: 60%;
      margin: 0 auto;
      padding: 20px;
      overflow: auto;
}

.home-link{
    background-color:#00c08b;
    width: 15%;
    height: 80px;
    display: inline-block;
    margin-left: 10%;
}
.portfolio-link{
    background-color:#ea5080;
    width: 15%;
    height: 80px;
    display: inline-block;

}
.social-link{
    background-color:#53bfe2;
    width: 15%;
    height: 80px;
    display: inline-block;

}
.contact-link{
    background-color:#f8c54d;
    width: 15%;
    height: 80px;
    display: inline-block;

}
.blog-link{
    background-color:#df6dc2;
    width: 15%;
    height: 80px;
    display: inline-block;

}
<!DOCTYPE html>
<html>
    <head>
        <title>Neiko Anglin | Front-End Develper </title>

        <!-- Our CSS stylesheet file -->
        <link rel="stylesheet" href="css/styles.css" />

        <!-- Font Awesome Stylesheet -->
        <link rel="stylesheet" href="font-awesome/css/font-awesome.css" />


    </head>

    <body>
        <div class="nav-colors">
            <div class="home-link">
            </div>
            <div class="portfolio-link">
            </div>
            <div class="social-link">
            </div>
            <div class="contact-link">
            </div>
            <div class="blog-link">
            </div>

        </div>
    </body>
</html>

Upvotes: 5

Views: 83

Answers (5)

jacobcc
jacobcc

Reputation: 344

First the explanation, then some code.

Vertical centering is a classic css issue. The vh unit has come in very handy for this recently. Coupled with margin (and maybe calc) its now a solvable thing.

Centering it horizontally is simple enough, and you have that figured out. Just have a width and set margin: 0 auto and you are good to go.

With Vertical Centering the key thing to remember is you are centering your element, so half is over the middle, half is under the middle. With that we can make margin: calc(50vh-40px) auto 0 for your 80px high element and presto, it's in the middle vertically.

One step further:
Like horizontal centering, you seem to already have the dynamic width down by using %. For a dynamic vertical size we can again turn to vh. The nice thing is this saves us the css calc function. Just subtract half the height from the 50vh margin and you'll get your margin. So for height: 20vh the margin is margin: 40vh auto 0

Here is a JsFiddle

and here is some code:

CSS:

        *{
            margin: 0;
            padding: 0;
        }
        html, body{
            width: 100vw;
            height: 100vh;
        }
        .nav-colors{
            width: 80%;
            height: 20vh;
            margin: calc(40vh) auto 0;
        }
        .nav-colors div{
            width: 18%;
            margin: 0 0 0 1%;
            height: 100%;
            display: inline-block;
        }
        .home-link{background-color:#00c08b;}
        .portfolio-link{background-color:#ea5080;}
        .social-link{background-color:#53bfe2;}
        .contact-link{background-color:#f8c54d;}
        .blog-link{background-color:#df6dc2;}

HTML:

<div class="nav-colors">
    <div class="home-link"></div>
    <div class="portfolio-link"></div>
    <div class="social-link"></div>
    <div class="contact-link"></div>
    <div class="blog-link"></div>
</div>

enjoy.

Upvotes: 0

Seva Arkhangelskiy
Seva Arkhangelskiy

Reputation: 685

You just have to add some properties to your .nav-colors:

.nav-colors{
    position: relative;
    background: white;
    height: 200px;
    width: 60%;
    margin: 0 auto;
    padding: 20px;
    overflow: auto;

    line-height: 200px;
    text-align: center;
}

And add vertical-align: middle; to elements you want to center vertically.

Upvotes: 0

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196217

Change your .nav-color class to

.nav-colors{
     position: fixed;
      background: white;
      height: 80px;
      width:60%;
      margin: -60px 0 0 0;
      padding: 20px;
      overflow: auto;
      top:50%;
      left:20%;
}

/*-------------------------
    Simple reset
--------------------------*/

* {
  margin: 0;
  padding: 0;
}
/*-------------------------
    General Styles
--------------------------*/

/*----------------------------
    Color Themes
-----------------------------*/

.nav-colors {
  position: fixed;
  background: white;
  height: 80px;
  width: 60%;
  margin: -60px 0 0 0;
  padding: 20px;
  overflow: auto;
  top: 50%;
  left: 20%;
}
.home-link {
  background-color: #00c08b;
  width: 15%;
  height: 80px;
  display: inline-block;
  margin-left: 10%;
}
.portfolio-link {
  background-color: #ea5080;
  width: 15%;
  height: 80px;
  display: inline-block;
}
.social-link {
  background-color: #53bfe2;
  width: 15%;
  height: 80px;
  display: inline-block;
}
.contact-link {
  background-color: #f8c54d;
  width: 15%;
  height: 80px;
  display: inline-block;
}
.blog-link {
  background-color: #df6dc2;
  width: 15%;
  height: 80px;
  display: inline-block;
}
<div class="nav-colors">
  <div class="home-link">
  </div>
  <div class="portfolio-link">
  </div>
  <div class="social-link">
  </div>
  <div class="contact-link">
  </div>
  <div class="blog-link">
  </div>
</div>

Upvotes: 0

j08691
j08691

Reputation: 207973

You can use absolute positioning on the container to center vertically and horizontally:

/*-------------------------
    Simple reset
--------------------------*/
 * {
    margin:0;
    padding:0;
}
/*-------------------------
    General Styles
--------------------------*/

/*----------------------------
    Color Themes
-----------------------------*/
 .nav-colors {
    position: absolute;
    background: white;
    height: 84px;
    width: 60%;
    margin: auto;
    padding: 20px;
    overflow: auto;
    top:0;
    right:0;
    bottom:0;
    left:0;
}
.home-link {
    background-color:#00c08b;
    width: 15%;
    height: 80px;
    display: inline-block;
    margin-left: 10%;
}
.portfolio-link {
    background-color:#ea5080;
    width: 15%;
    height: 80px;
    display: inline-block;
}
.social-link {
    background-color:#53bfe2;
    width: 15%;
    height: 80px;
    display: inline-block;
}
.contact-link {
    background-color:#f8c54d;
    width: 15%;
    height: 80px;
    display: inline-block;
}
.blog-link {
    background-color:#df6dc2;
    width: 15%;
    height: 80px;
    display: inline-block;
}
<div class="nav-colors">
    <div class="home-link"></div>
    <div class="portfolio-link"></div>
    <div class="social-link"></div>
    <div class="contact-link"></div>
    <div class="blog-link"></div>
</div>

Upvotes: 1

Alaksandar Jesus Gene
Alaksandar Jesus Gene

Reputation: 6897

To align vertically you need a wrapper class with position absolute in CSS. Search for vertical center which will fetch you lots of results. To resize boxes along with screen resize - is responsive template. I could suggest you to use Twitter Bootstrap which takes care of your dimensions.

Upvotes: 0

Related Questions