英文:
Need help personalizing my CSS navbar: how do I highlight the <li> element on mouseover?
问题
我正在尝试制作一个简单的导航栏,我已经创建了基础部分,但当我尝试个性化时遇到了困难。
我的目标是当鼠标移到每个元素(home等)上时,它会突出显示像一个框,但目前只突出显示<a>
,而不是包含它的<li>
。
我正在尝试使<li>
成为可以单击并突出显示的锚点,类似于stack的导航栏。
.nav-top {
background: rgb(151, 138, 63);
margin: 0;
padding: 1rem 0;
display: flex;
justify-content: flex-end;
align-items: center;
}
.nav-item {
list-style: none;
margin-right: 1.2rem;
padding: 5px 10px;
}
.nav-top ul {
margin: 0 auto;
padding: 0;
list-style: none
}
.nav-top li:hover {
display: block
}
.nav-top a:hover {
background: #F2F2F2;
color: #444444;
}
.nav-item a {
text-decoration: none;
color: white;
width: 100px;
}
.nav-item:first-child {
margin-right: auto;
margin-left: 2rem;
}
.nav-item a:hover {
color: aqua;
}
<nav>
<ul class="nav-top">
<li class="nav-item"><label class="logo">LoremX</label></li>
<li class="nav-item"><a href="index.html">Home</a></li>
<li class="nav-item"><a href="#">About</a></li>
<li class="nav-item" id="contact"><a href="#">Contact</a></li>
</ul>
</nav>
英文:
I am trying to make a simple navbar I managed to make the base but when I try to personalize I am getting stuck.
My objective is that when the mouse goes over each element(home, etc) it highlights like a box, but currently it only highlights the <a>
I tried and not the <li>
holding it.
I'm trying to make the <li>
an anchor that can be clicked and highlighted similar to stack's navbar.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.nav-top {
background: rgb(151, 138, 63);
margin: 0;
padding: 1rem 0;
display: flex;
justify-content: flex-end;
align-items: center;
}
.nav-item {
list-style: none;
margin-right: 1.2rem;
padding: 5px 10px;
}
.nav-top ul {
margin: 0 auto;
padding: 0;
list-style: none
}
.nav-top li:hover {
display: block
}
.nav-top a:hover {
background: #F2F2F2;
color: #444444;
}
.nav-item a {
text-decoration: none;
color: white;
width: 100px;
}
.nav-item:first-child {
margin-right: auto;
margin-left: 2rem;
}
.nav-item a:hover {
color: aqua;
}
<!-- language: lang-html -->
<navbar>
<ul class="nav-top">
<li class="nav-item"><label class="logo">LoremX</label></li>
<li class="nav-item"><a href="index.html">Home</a></li>
<li class="nav-item"><a href="#">About</a></li>
<li class="nav-item" id="contact"><a href="#">Contact</a></li>
</ul>
</navbar>
<!-- end snippet -->
答案1
得分: 2
你对 li
的唯一 :hover
声明是 display: block
的默认值,而颜色变化声明仅适用于 a
。然而,我认为你试图实现的效果更好地通过将锚点设置为块级元素并添加填充来实现。
与悬停效果无关,只是更正你的标记:
.nav-top {
display: flex;
margin: 0;
padding: 0;
list-style-type: none;
background: rgb(151, 138, 63);
}
.logo {
margin-right: auto;
margin-left: 2rem;
padding: 5px 10px;
align-self: center;
}
.nav-item {
margin-right: 1.2rem;
padding: 5px 10px;
}
.nav-item a {
display: block;
padding: .5em;
text-decoration: none;
color: white;
}
.nav-item a:hover {
color: aqua;
background: #F2F2F2;
}
<nav>
<ul class="nav-top">
<li class="logo">LoremX</li>
<li class="nav-item"><a href="#">Home</a></li>
<li class="nav-item"><a href="#">About</a></li>
<li class="nav-item" id="contact"><a href="#">Contact</a></li>
</ul>
</nav>
英文:
The only :hover
declaration you have for li
is the default value of display: block
while the color change declarations are made only for a
. However, the effect that I believe you are trying to achieve is better accomplished by making the anchors block-level with padding.
Not related to the hover effect, just correcting your markup:
- You have
.nav-top ul
selector but the example doesn't include a nestedul
- I suspect that you are misusing the
label
element. navbar
is not an HTML element and I suspect you wantnav
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.nav-top {
display: flex;
margin: 0;
padding: 0;
list-style-type: none;
background: rgb(151, 138, 63);
}
.logo {
margin-right: auto;
margin-left: 2rem;
padding: 5px 10px;
align-self: center;
}
.nav-item {
margin-right: 1.2rem;
padding: 5px 10px;
}
.nav-item a {
display: block;
padding: .5em;
text-decoration: none;
color: white;
}
.nav-item a:hover {
color: aqua;
background: #F2F2F2;
}
<!-- language: lang-html -->
<nav>
<ul class="nav-top">
<li class="logo">LoremX</li>
<li class="nav-item"><a href="#">Home</a></li>
<li class="nav-item"><a href="#">About</a></li>
<li class="nav-item" id="contact"><a href="#">Contact</a></li>
</ul>
</nav>
<!-- end snippet -->
答案2
得分: 1
在锚元素中添加填充和边框:
.nav-item a {
text-decoration: none;
color: white;
width: 100px;
padding: 10px;
border-radius: 10px;
}
英文:
just add padding and a border in an anchor element
.nav-item a {
text-decoration: none;
color: white;
width: 100px;
padding: 10px;
border-radius: 10px;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论