如何在Flex设计中将水平居中的导航链接更紧密地连接在一起?

时间:2019-06-27 22:36:39

标签: html css

我发现了与我类似的问题,但是找不到可行的解决方案。我的导航链接在标题中水平居中,徽标在正中央。我的链接均匀分布在屏幕的整个宽度上。我需要使导航链接更加紧密,并更接近徽标。我很茫然,不知道还能尝试什么。这也使我的悬停效果(下划线)延伸得比每个单词的宽度都远。在此先感谢您的帮助。

body {
    margin: 0;
   line-height: 1.2em;
   font-family: Arial, Helvetica, sans-serif;
}

html, body{
  height: 100%;
}

.cc_nav {
  display: flex; 
  flex-direction: row;
  width: 100%;
  margin-top: 0;
  padding-top: 10px;
  height: 20%;
  align-items: center;
  justify-content: center; 
  overflow: hidden;
  position: fixed;
  top: 0;
  background-color: white;
  text-align: center; 
}

a{
  text-decoration: none;
  color: rgba(0,0,0,0.8);
  font-family: Tenar Sans;
  font-size: .8em;
  flex: 1;
}

a {
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  -moz-osx-font-smoothing: grayscale;
  position: relative; 
  overflow: hidden; 
}

a:before {
  content: "";
  position: absolute; 
  width: 100%;
  height: 1px;
  bottom: 0;
  left: 0;
  background-color: rgb(192,192,192); 
  visibility: hidden;
  -webkit-transform: scaleX(0);
  transform: scaleX(0);
  -webkit-transition: all 0.3s ease-in-out 0s;
  transition: all 0.3s ease-in-out 0s; }


a:hover:before {
  visibility: visible;
  -webkit-transform: scaleX(1);
  transform: scaleX(1);
}
<header>
  <div class="cc_nav" id="centered_nav">
    <a href="index.html">HOME</a>
    <a href="services.html">SERVICES</a>
    <a href="about.html">ABOUT</a>
    <a href="index.html" class="noHover"><img src="images/logo_6.png" alt="Claire Crawford" id="logo_Claire" /></a>
    <a href="portfolio.html">PORTFOLIO</a>
    <a href="blog.html">BLOG</a>
    <a href="contact.html">GET IN TOUCH</a>
  </div>
</header>

2 个答案:

答案 0 :(得分:3)

使用flex: 1可使项目按比例增长以填充容器。您可以考虑使用flex: 0 0 auto,以使项目不会超出其默认宽度而增长或缩小。

我还添加了左右边距,以使项目不会一起出现。

body {
  margin: 0;
  line-height: 1.2em;
  font-family: Arial, Helvetica, sans-serif;
}

html,
body {
  height: 100%;
}

.cc_nav {
  display: flex;
  flex-direction: row;
  width: 100%;
  margin-top: 0;
  padding-top: 10px;
  height: 20%;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: fixed;
  top: 0;
  background-color: white;
  text-align: center;
}

a {
  text-decoration: none;
  color: rgba(0, 0, 0, 0.8);
  font-family: Tenar Sans;
  font-size: .8em;
  margin: 0 1em;
  flex: 0 0 auto;
}

a {
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  -moz-osx-font-smoothing: grayscale;
  position: relative;
  overflow: hidden;
}

a:before {
  content: "";
  position: absolute;
  width: 100%;
  height: 1px;
  bottom: 0;
  left: 0;
  background-color: rgb(192, 192, 192);
  visibility: hidden;
  -webkit-transform: scaleX(0);
  transform: scaleX(0);
  -webkit-transition: all 0.3s ease-in-out 0s;
  transition: all 0.3s ease-in-out 0s;
}

a:hover:before {
  visibility: visible;
  -webkit-transform: scaleX(1);
  transform: scaleX(1);
}
<header>
  <div class="cc_nav" id="centered_nav">
    <a href="index.html">HOME</a>
    <a href="services.html">SERVICES</a>
    <a href="about.html">ABOUT</a>
    <a href="index.html" class="noHover"><img src="images/logo_6.png" alt="Claire Crawford" id="logo_Claire" /></a>
    <a href="portfolio.html">PORTFOLIO</a>
    <a href="blog.html">BLOG</a>
    <a href="contact.html">GET IN TOUCH</a>
  </div>
</header>

有关参考,请参见flex

顺便说一句,您的代码中似乎也有错字;结束</div>标签显示为开始<div>标签。

答案 1 :(得分:-3)

我现在正在使用手机,因此无法为您重写代码,但是我建议您使用Bootstrap Flex Box。真的很有帮助。帮助了我很多次。 https://getbootstrap.com/docs/4.0/utilities/flex/ 在引导程序4中尝试此操作

<div class="d-flex flex-column">
  <div class="p-2">Flex item 1</div>
  <div class="p-2">Flex item 2</div>
  <div class="p-2">Flex item 3</div>
</div>