gpt4 book ai didi

HTML/CSS 帮助使链接悬停填充整个导航栏

转载 作者:太空宇宙 更新时间:2023-11-03 21:13:10 26 4
gpt4 key购买 nike

当我们将鼠标悬停在链接上时,我试图使导航栏完全突出显示,但它目前只能水平工作。我认为这是非常小的事情,我做错了并且已经尝试了四个小时。这是我的代码:

    html, body { 

/*require html and body 100% height and width to allow other child elements to use percentages*/

height: 100%;
width: 100%;
margin: 0;

}

a {
text-decoration: none;
display: block;
color: black;
}

li {
list-style: none;
}


div{
margin-left: 2.5%;
margin-right: 2.5%;
margin-top: 1%;
border: 1px solid black;
}

.content_section{
height: 150%;
margin-bottom: 1%;
}

#footer{
height: 25%;
margin-bottom: 1%;
}

#banner{
margin-top: 2.5%;
height: 15%;
position: relative;
}

#banner img{
width: 100%;
height: 100%;
}

#navbar{
padding: 0;
height: 5%;


text-align: center;
position: relative;
background-color: #FFCB3D;
}

#navbar li a {
display: block;
text-align: center;
text-decoration: none;
width: 20%;
height: 100%;
float: left;
}

#navbar ul a:hover{
height: 100%;
background-color: #FFF17C;
}
<!DOCTYPE html>
<html>

<head>
<title>Sample Site</title>
<link rel="stylesheet" type="text/css" href="css.css">
</head>

<body>
<div id="banner">
<img src="resources/images/banner-image.png">
</div>

<div id="navbar">
<ul id="navbar">
<li><a href="#">Page A</a></li>
<li><a href="#">Page B</a></li>
<li><a href="#">Page C</a></li>
<li><a href="#">Page D</a></li>
<li><a href="#">Page E</a></li>
</ul>
</div>

<div class="content_section">
</div>

<div id="footer">
</div>
</body>
</html>

最佳答案

我会让 ul 成为 display: flex 的父级,以从 li 中创建一行,删除 #navbar 上的 height 使其根据内容流动,删除 ul 的默认 margin,然后设置 flex-grow: 1li 上(或简称为 flex: 1 0 0)所以它们会拉伸(stretch)以均匀地填充父级,然后对 li > a 应用垂直 padding 并移除 heightfloat

    html, body { 

/*require html and body 100% height and width to allow other child elements to use percentages*/

height: 100%;
width: 100%;
margin: 0;

}

a {
text-decoration: none;
display: block;
color: black;
}

li {
list-style: none;
}


div{
margin-left: 2.5%;
margin-right: 2.5%;
margin-top: 1%;
border: 1px solid black;
}

.content_section{
height: 150%;
margin-bottom: 1%;
}

#footer{
height: 25%;
margin-bottom: 1%;
}

#banner{
margin-top: 2.5%;
height: 15%;
position: relative;
}

#banner img{
width: 100%;
height: 100%;
}

#navbar{
padding: 0;
position: relative;
background-color: #FFCB3D;
text-align: center;
}
ul#navbar {
display: flex;
margin: 0;
}
#navbar li {
flex: 1 0 0;
}

#navbar li a {
display: block;
text-decoration: none;
padding: 1em 0;
}

#navbar ul a:hover{
background-color: #FFF17C;
}
<!DOCTYPE html>
<html>

<head>
<title>Sample Site</title>
<link rel="stylesheet" type="text/css" href="css.css">
</head>

<body>
<div id="banner">
<img src="resources/images/banner-image.png">
</div>

<div id="navbar">
<ul id="navbar">
<li><a href="#">Page A</a></li>
<li><a href="#">Page B</a></li>
<li><a href="#">Page C</a></li>
<li><a href="#">Page D</a></li>
<li><a href="#">Page E</a></li>
</ul>
</div>

<div class="content_section">
</div>

<div id="footer">
</div>
</body>
</html>

关于HTML/CSS 帮助使链接悬停填充整个导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44683030/

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