gpt4 book ai didi

html - 如何在 Flex 设计中使水平居中的导航链接更靠近?

转载 作者:行者123 更新时间:2023-11-27 23:46:12 26 4
gpt4 key购买 nike

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

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>

最佳答案

使用 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>标签。

关于html - 如何在 Flex 设计中使水平居中的导航链接更靠近?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56799165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com