如何在标题元素之间设置相等的距离?

时间:2018-08-31 17:41:04

标签: html css flexbox padding

我正在使用justify-content:space-between在我的标题中水平分配所有元素。在我向导航链接添加 padding-right:20px 之前,该方法可以正常工作。它将我的标头h2元素向右移动,使其不均匀。如何保持填充,但元素均等对齐?帮助表示赞赏,谢谢。

https://jsfiddle.net/2fr3L8zh/14/

HTML

None

CSS

<header>
 <div id="header-wrapper">
  <nav>
   <a>Projects</a>
   <a>About</a>
   <a>Contact</a>
  </nav>

 <h2>EMMANUEL OJIJI</h2>
 <p>SOCIAL MEDIA HERE</p>
 </div>
</header>

<section id="intro">
<div id="intro-wrapper">
<h1>I'm Lorem Ipsum — a Birmingham -based Visual Designer with a passion and 
a firm belief in considered and meaningful design.</h1>
</div>
</section>

1 个答案:

答案 0 :(得分:0)

避免对导航的最后一个元素进行填充:

nav a:not(:last-child)

html {
  margin: 0;
  padding: 0;
  height: 100%;
}

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

header {
  height: 100px;
  background-color: white;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
  display: flex;
  justify-content: center;
  ;
  align-items: center;
  padding-left: 50px;
  padding-right: 50px;
}

#header-wrapper {
  width: 90%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

nav a:not(:last-child) {
  padding-right: 20px;
}

#intro {
  height: 90vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

#intro-wrapper {
  display: flex;
  text-align: center;
  width: 50%;
}

#intro-wrapper h1 {
  font-weight: 700;
  font-family: 'Raleway', sans-serif;
  font-size: 2.2em;
  line-height: 60px;
  color: #333;
}
<header>
  <div id="header-wrapper">
    <nav>
      <a>Projects</a>
      <a>About</a>
      <a>Contact</a>
    </nav>

    <h2>EMMANUEL OJIJI</h2>
    <p>SOCIAL MEDIA HERE</p>
  </div>
</header>

<section id="intro">
  <div id="intro-wrapper">
    <h1>I'm Lorem Ipsum — a Birmingham -based Visual Designer with a passion and a firm belief in considered and meaningful design.</h1>
  </div>
</section>