admin管理员组文章数量:1389768
I am trying to design a navbar where the "SushiMan" heading is aligned to the left and the other list items (Menu, Food, Services, etc.) are aligned to the right. The layout works fine when I follow a tutorial on YouTube, but it’s not working as expected on my local machine.
Here is my HTML and CSS code:
.header__nav {
display: flex;
justify-content: space-between;
/* This will push the items to the edges */
align-items: center;
/* Vertically aligns items */
padding: 0 20px;
/* Adds space on the sides */
overflow: hidden;
}
.header__logo {
flex: 1;
display: flex;
position: relative;
padding: 20px;
}
.header__logo-overlay {
position: absolute;
inset: 0;
width: 100%;
}
<header>
<nav class="header__nav">
<div class="header__logo">
<h4>SushiMan</h4>
<div class="header__logo-overlay"> </div>
<ul class="header__menu">
<li>
<a href="#menu">Menu</a>
</li>
<li>
<a href="#menu">Food</a>
</li>
<li>
<a href="#menu">Services</a>
</li>
<li>
<a href="#menu">About Us</a>
</li>
<li>
</li>
<img src="./assets/search.svg" alt="search"/>
</ul>
<ul class="header__menu-mobile">
<li>
<img src="./assets/menu.svg"/>
</li>
</ul>
</div>
</nav>
</header>
I am trying to design a navbar where the "SushiMan" heading is aligned to the left and the other list items (Menu, Food, Services, etc.) are aligned to the right. The layout works fine when I follow a tutorial on YouTube, but it’s not working as expected on my local machine.
Here is my HTML and CSS code:
.header__nav {
display: flex;
justify-content: space-between;
/* This will push the items to the edges */
align-items: center;
/* Vertically aligns items */
padding: 0 20px;
/* Adds space on the sides */
overflow: hidden;
}
.header__logo {
flex: 1;
display: flex;
position: relative;
padding: 20px;
}
.header__logo-overlay {
position: absolute;
inset: 0;
width: 100%;
}
<header>
<nav class="header__nav">
<div class="header__logo">
<h4>SushiMan</h4>
<div class="header__logo-overlay"> </div>
<ul class="header__menu">
<li>
<a href="#menu">Menu</a>
</li>
<li>
<a href="#menu">Food</a>
</li>
<li>
<a href="#menu">Services</a>
</li>
<li>
<a href="#menu">About Us</a>
</li>
<li>
</li>
<img src="./assets/search.svg" alt="search"/>
</ul>
<ul class="header__menu-mobile">
<li>
<img src="./assets/menu.svg"/>
</li>
</ul>
</div>
</nav>
</header>
Share
Improve this question
asked Mar 15 at 10:22
riddhiriddhi
11 silver badge
2 Answers
Reset to default 1You need to close <div class="header__logo">
before the <ul class="header__menu">
. Your code should be as follows:
<header>
<nav class="header__nav">
<div class="header__logo">
<h4>SushiMan</h4>
<div class="header__logo-overlay"> </div>
</div>
<ul class="header__menu">
<li>
<a href="#menu">Menu</a>
</li>
<li>
<a href="#menu">Food</a>
</li>
<li>
<a href="#menu">Services</a>
</li>
<li>
<a href="#menu">About Us</a>
</li>
<li>
</li>
<img src="M.png" alt="search"/>
</ul>
<ul class="header__menu-mobile">
<li>
<img src="M.png"/>
</li>
</ul>
</nav>
</header>
Also, your css could be written better. But for the problem you raised, this is the solution I wrote.
Your html structure is wrong. Do not put header__menu
and header__menu-mobile
inside the logo div. Attached my codepen screenshot after the fix.
<header>
<nav class="header__nav">
<div class="header__logo">
....
</div>
<ul class="header__menu">
....
</ul>
<ul class="header__menu-mobile">
....
</ul>
</nav>
</header>
Remove flex: 1;
from header logo and apply below styles.
.header__menu {
display: flex;
margin: 0;
padding: 0;
}
.header__menu li {
margin-left: 20px;
}
本文标签:
版权声明:本文标题:html - Why is my Flexbox navbar with a left-aligned logo and right-aligned menu not working as expected? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744618268a2615864.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论