kneijenhuijs
kneijenhuijs

Reputation: 1209

Flexbox: Center body content

I've got this lay-out here. I'm trying to get everything inside the body centered. Besides what I've tried in the code linked and below, I've also tried adding a flex-display property to the wrapper, but that also didn't pan out. I'm not entirely sure what I'm doing wrong, as I believe justify-content: center; should be doing the trick, right?

One more thing: I've got this ugly strip of background colour above and below my <main>. How can I fix this without deleting the <main> tag (that's the only think I found that has worked so far)? Should I use a wrapper div instead of the main tag?

html {
  height: 100%;
  background: #f5f5f5;
}
body {
  height: 100%;
  width: 70%;
  display: flex;
  display: -webkit-flex;
  flex-direction: column;
  -webkit-flex-direction: column;
  font-family: "sans-serif";
  align-items: center;
  -webkit-align-items: center;
}
.wrapper {
  -webkit-box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);
  -moz-box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);
  box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);
  max-width: 980px;
}
header {
  display: flex;
  display: -webkit-flex;
  flex-direction: row;
  -webkit-flex-direction: row;
  justify-content: flex-end;
  -webkit-justify-content: flex-end;
  align-items: center;
  -webkit-align-items: center;
  background: #42A8C0;
  width: 100%;
  color: white;
}
.name-container {
  flex: 1 0 auto;
  -webkit-flex: 1 0 auto;
  text-align: center;
}
.name-container h1 {
  font-weight: 100;
  letter-spacing: 2px;
  font-size: 40px;
}
.name-container h4 {
  font-weight: 500;
  letter-spacing: 1px;
}
.profile-container {
  flex: 0 0 auto;
  -webkit-flex: 0 0 auto;
  background: rgba(0, 0, 0, 0.2);
  display: flex;
  display: -webkit-flex;
  padding: 10px;
}
.social-container {
  display: flex;
  display: -webkit-flex;
  flex-direction: column;
  -webkit-flex-direction: column;
  font-size: 20px;
  margin-top: 10px;
}
.social-container a {
  color: white;
  text-decoration: none;
}
.social-container a:hover {
  text-decoration: underline;
}
.contact-list {
  list-style-type: none;
}
.contact-list .fa {
  margin-right: 10px;
  font-size: 20px;
  vertical-align: middle;
}
.contact-list li {
  margin-bottom: 5px;
}
.portrait-pic {
  border-radius: 50%;
  height: 200px;
  width: 200px;
}
main {
  background: white;
  flex: 1 0 auto;
  -webkit-flex: 1 0 auto;
  width: 100%;
  color: black;
}
section {
  padding-left: 20px;
  padding-right: 20px
}
.summ-box {
  margin: 20px auto;
  text-align: center;
}
p {
  font-weight: 200;
  letter-spacing: 1px;
}
<div class="wrapper">
  <header>
    <div class="name-container">
      <h1>My Name</h1>
      <h4>Small summary of current position</h4>
    </div>
    <!--/name-container-->
    <div class="profile-container">
      <div class="social-container">
        <ul class="contact-list">
          <li class="email"><a href="mailto: [email protected]"><i class="fa fa-envelope" aria-hidden="true"></i></a><a href="mailto: [email protected]">[email protected]</a>
          </li>
          <li class="website"><a href="http://www.google.com" target="_blank"><i class="fa fa-globe" aria-hidden="true"></i></a><a href="http://www.google.com" target="_blank">portfoliosite.com</a>
          </li>
          <li class="linkedin"><a href="http://www.google.com" target="_blank"><i class="fa fa-linkedin" aria-hidden="true"></i></a><a href="http://www.google.com" target="_blank">linkedin.com/inname</a>
          </li>
          <li class="github"><a href="http://www.google.com" target="_blank"><i class="fa fa-github" aria-hidden="true"></i></a><a href="http://www.google.com" target="_blank">github.com/username</a>
          </li>
          <li class="twitter"><a href="http://www.google.com" target="_blank"><i class="fa fa-twitter" aria-hidden="true"></i></a><a href="http://www.google.com" target="_blank">@twittername</a>
          </li>
        </ul>
      </div>
      <!--/social-container-->
      <div class="picture-container">
        <img src="https://cdn.pixabay.com/photo/2016/02/22/18/57/puppy-1216269_960_720.jpg" class="portrait-pic">
      </div>
      <!--/picture-container-->
    </div>
    <!--/profile-container-->
  </header>
  <!--/header-->
  <main>
    <section class="summ-box">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Tria genera bonorum; Nec vero alia sunt quaerenda contra Carneadeam illam sententiam. Quae quidem vel cum periculo est quaerenda vobis; Beatum, inquit. Duo Reges: constructio interrete.</p>
      <div class="line-break"></div>
      <!--/line-break-->
    </section>
    <!--/summ-box-->
  </main>
  <!--/main-->
</div>
<!--/wrapper-->

Upvotes: 2

Views: 6455

Answers (2)

Andrew Monks
Andrew Monks

Reputation: 666

The bit above the main is caused by the margins on .summ-box and the p tag. remove the margins and replace with padding.

This explains the issue: https://css-tricks.com/what-you-should-know-about-collapsing-margins/

Your body with is 70% and it appears to be centre in that, do you mean for your body to be 100% width?

Upvotes: 2

Michael Benjamin
Michael Benjamin

Reputation: 371371

Remove the width: 70% from the body element. It's not leaving any extra space for centering.

Upvotes: 5

Related Questions