[英]Center logo in navbar list
CODEPEN: https://codepen.io/tylerisfatal/pen/vYgmaxJ编码笔: https://codepen.io/tylerisfatal/pen/vYgmaxJ
I have built a simple navbar with a list of icons and a logo inside the element but outside the element.我已经构建了一个简单的导航栏,其中包含图标列表和元素内部但元素外部的徽标。 This is sort of a two-part question:这是一个由两部分组成的问题:
How do I center this element?我如何使这个元素居中? It's parent is the nav element, yet I can't seem to center it.它的父元素是导航元素,但我似乎无法将其居中。 I tried margin: 0 auto
but it only centered it near the end of the flex container.我尝试了margin: 0 auto
但它只在 flex 容器的末端附近居中。
Is doing it this way (the logo inside the nav yet outside the ul) the most efficient way to write this?这样做是最有效的写法吗(导航内的徽标但在 ul 外)?
HTML: HTML:
<nav role="navigation" class="main-nav">
<ul class="main-nav-list">
<li class="fb">
<a href="#facebook">
<img src="https://cdn3.iconfinder.com/data/icons/capsocial-round/500/facebook-512.png" alt="">
</a>
</li>
<li>
<a href="#yelp">
<img src="https://cdn3.iconfinder.com/data/icons/social-media-2169/24/social_media_social_media_logo_yelp-512.png" alt="">
</a>
</li>
<li>
<a href="#linkedin">
<img src="https://cdn3.iconfinder.com/data/icons/free-social-icons/67/linkedin_circle_color-512.png" alt="">
</a>
</li>
</ul>
</nav>
CSS: CSS:
/* navbar Container */
.main-nav {
display: flex;
background-color: rgb(28, 143, 124);
align-items: center;
height: auto;
width: auto;
}
/* ul Container*/
.main-nav
.main-nav-list {
display: flex;
margin-right: auto; /* img placement */
justify-content: flex-end;
flex-direction: row;
list-style-type: none;
}
/* Responsive images */
.main-nav
.main-nav-list li img {
display: flex;
height: auto;
width: auto;
max-height: 150px;
max-width: 75px;
}
/* Logo */
.main-nav .logo {
display: flex;
height: auto;
width: auto;
max-height: 150px;
max-width: 75px;
margin: 0 auto;
}
Put your nav with certain width in container with body width将具有一定宽度的导航放在具有主体宽度的容器中
.container {
width: 100%;
background: rgb(28, 143, 124);
}
.main-nav {
margin: 0 auto;
width: 500px;
display: flex;
background-color: rgb(28, 143, 124);
align-items: center;
height: auto;
}
Visit https://codepen.io/losrios1337/pen/mdRmjGL !访问https://codepen.io/losrios1337/pen/mdRmjGL !
Just copy paste this into your given codepen.只需将其复制粘贴到您给定的代码笔中即可。 I guess this is what you needed我想这就是你需要的
/* navbar Container */
.main-nav {
display: flex;
background-color: rgb(28, 143, 124);
align-items: center;
height: auto;
width: auto;
}
/* ul Container*/
.main-nav
.main-nav-list {
display: flex;
/* margin-right: auto; */
/* img placement */
justify-content: flex-end;
flex-direction: row;
list-style-type: none;
margin-right: auto;
margin-left: auto;
}
/* Responsive images */
.main-nav
.main-nav-list li img {
display: flex;
height: auto;
width: auto;
max-height: 150px;
max-width: 75px;
}
/* Logo */
.main-nav .logo {
display: flex;
height: auto;
width: auto;
max-height: 150px;
max-width: 75px;
/* margin: 0 auto; */
}
You could simply put a common wrapper div around your logo:您可以简单地在您的徽标周围放置一个通用包装 div:
<div class="logo-wrapper">
<img src="https://cdn2.iconfinder.com/data/icons/icontober/64/Inkcontober_Deep_Dive_Suit-512.png" alt="" class="logo">
</div>
and position the logo with this css:和 position 带有此 css 的徽标:
.logo-wrapper {
display: flex;
justify-content: center;
width: 20%;
}
The logo wrapper gets 20% width, the ul container 40% width and both are per default flex-start-justified.徽标包装器获得 20% 的宽度,ul 容器获得 40% 的宽度,并且两者都是默认的 flex-start-justified。 So the logo wrapper is centered and also justifies its own content centered – so the logo itself is centerd.所以标志包装是居中的,也证明了它自己的内容居中——所以标志本身是居中的。
Working example (click 'Full Page' to compare):工作示例(单击“整页”进行比较):
.main-nav { display: flex; align-items: center; background-color: rgb(28, 143, 124); } /* ul Container*/.main-nav-list { box-sizing: border-box; display: flex; list-style-type: none; width: 40%; } /* Logo wrapper */.logo-wrapper { display: flex; justify-content: center; width: 20%; } /* Responsive images */.main-nav img { max-height: 150px; max-width: 75px; }.main-nav img:hover { transform: scale(1.05); }
<nav role="navigation" class="main-nav"> <ul class="main-nav-list"> <li class="fb"> <a href="#facebook"> <img src="https://cdn3.iconfinder.com/data/icons/capsocial-round/500/facebook-512.png" alt=""> </a> </li> <li> <a href="#yelp"> <img src="https://cdn3.iconfinder.com/data/icons/social-media-2169/24/social_media_social_media_logo_yelp-512.png" alt=""> </a> </li> <li> <a href="#linkedin"> <img src="https://cdn3.iconfinder.com/data/icons/free-social-icons/67/linkedin_circle_color-512.png" alt=""> </a> </li> </ul> <div class="logo-wrapper"> <img src="https://cdn2.iconfinder.com/data/icons/icontober/64/Inkcontober_Deep_Dive_Suit-512.png" alt="" class="logo"> </div> </nav>
not quite sure what you mean by this "inside the element but outside the element".不太清楚你所说的“在元素内但在元素外”是什么意思。 I guess you mean inside the nav bar but outside of the ul element.我猜你的意思是在导航栏内但在 ul 元素之外。 If so, then you can add this to your.logo class:如果是这样,那么您可以将其添加到 your.logo class:
position: fixed;
right: 50%;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.