Flexbox:中心主体内容

时间:2017-02-09 15:21:18

标签: html css css3 flexbox

我有这个布局here。我试图让身体内的一切都集中在一起。除了我在链接和下面的代码中尝试过的内容之外,我还尝试在包装器中添加flex-display属性,但这也没有成功。我不完全确定我做错了什么,因为我相信justify-content: center;应该做的蠢事,对吧?

还有一件事:我的<main>上方和下方都有这条丑陋的背景色。如何在不删除<main>标记的情况下解决此问题(这是我发现迄今为止唯一有效的方法)?我应该使用包装器div而不是主标签吗?

&#13;
&#13;
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;
}
&#13;
<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: yourname@email.com"><i class="fa fa-envelope" aria-hidden="true"></i></a><a href="mailto: yourname@email.com">yourname@email.com</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-->
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:4)

width: 70%元素中删除body。它没有留下任何额外的空间用于居中。

答案 1 :(得分:2)

主要部分上方的位是由.summ-box和p标签上的边距引起的。删除边距并用填充替换。

这解释了问题:https://css-tricks.com/what-you-should-know-about-collapsing-margins/

你的身体是70%,它似乎是中心,你的意思是你的身体是100%宽度?