使用CSS在flexbox中水平居中文本?

时间:2016-11-12 22:49:31

标签: html css flexbox center

我试图为网站制作导航栏。我试图将我的链接文本水平居中在div中。 <table> <tr> <td class="col-1">One</td> <td class="col-2">Two</td> </tr> <tr> <td class="col-3">Three</td> <td class="col-4">Four</td> </tr> </table>没有做到这一点,text-align: center;align-items: center;都没有。有什么提示吗?

&#13;
&#13;
justify-content: center;
&#13;
#nav {
  display: flex;
  flex-direction: row rtl;
  border-top: 3px dotted #594027;
  border-bottom: 3px dotted #594027;
  width: 100%;
  align-items: center;
  justify-content: center;
}

.navbox {
  flex-wrap: wrap;
  background-color: #594027;
  width: 125px;
  margin: 5px;
  padding: 5px;
}

.navbox a {
  display: inline-block;
  color: white;
  padding: 3px;
  margin-right: auto;
  margin-left: auto;
  text-decoration: none;
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可以控制来自父级的文字,因为您需要将text-align:center添加到#nav

#nav {
  display: flex;
  flex-direction: row rtl;
  border-top: 3px dotted #594027;
  border-bottom: 3px dotted #594027;
  width: 100%;
  align-items: center;
  justify-content: center;
  /* Add text-align to Parent */
  text-align:center;
}

.navbox {
  flex-wrap: wrap;
  background-color: #594027;
  width: 125px;
  margin: 5px;
  padding: 5px;
}

.navbox a {
  display: inline-block;
  color: white;
  padding: 3px;
  margin-right: auto;
  margin-left: auto;
  text-decoration: none;
}
<div id="nav">
<div class="navbox">
<a href="index.html">Home</a>
</div>
<div class="navbox">
<a href="resume.html">Resumé</a>
</div>
<div class="navbox">
<a href="coursework.html">Coursework</a>
</div>
<div class="navbox">
<a href="accomplishments.html">Accomplishments</a>
</div>
<div class="navbox">
<a href="experience.html">Experience</a>
</div>
<div class="navbox">
<a href="blog.html">Blog</a>
</div>

</div><!--nav->