gpt4 book ai didi

html - 如何将整个 div 变成可点击的链接?

转载 作者:行者123 更新时间:2023-11-28 03:51:28 25 4
gpt4 key购买 nike

这是我的代码:HTML:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Squallz Test Page</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<header>
<h1>This is a header</h1>
</header>
<div class="navigation">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Repo</a></li>
<li><a href="#">Code</a></li>
</ul>
</div>
<div class="footer">
<div class="footerinfo">
<p id="copyright">Copyright © Squallz 2017 - Rights Reserved</p>
<p id="info">Personal website by <b>Squallz</b></p>
</div>
</div>
</body>
</html>

CSS:

body {
margin: 0;
padding: 0;
font-family: sans-serif;
background: url("../images/bg.png") no-repeat;
background-attachment: fixed;
}

header {
background-color: #088CAF;
color: white;
width: 100%;
padding: 20px 0;
text-align: center;
}

.navigation {
position: sticky;
}

.navigation ul {
background-color: #075E75;
overflow: hidden;
color: white;
padding: 0;
text-align: center;
margin: 0;
border-top: 2px solid #2E2E2E;
border-bottom: 2px solid #2E2E2E;
}

.navigation ul li {
display: inline-block;
padding: 20px;
}

.navigation li:hover {
background-color: #069DAA;
}

.navigation ul li a {
text-decoration: none;
color: white;
font-size: 20px;
}

.footer {
color: white;
text-align: center;
width: 100%;
background-color: #2E2E2E;
border-top: 2px solid #099C9E;
position: absolute;
bottom: -800px;
height: 80px;
overflow: hidden;
}

我只是想让它在我用其中的链接突出显示 li 时,整个内容都可以点击。我知道我可以将它们全部制作成由 anchor 包裹的单独 div,但这很困惑

最佳答案

您需要使您的a 标签的宽度和高度都达到li 的宽度和高度。将 padding:20px; 移动到您的 a 标签 css 并添加 display:block; 允许链接可点击,而无需修改您的 HTML。

将您的 li 标签 css 更新为:

.navigation ul li {
display: inline-block;
}

将您的 a 标签 css 更新为:

.navigation ul li a {
text-decoration: none;
color: white;
font-size: 20px;
padding: 20px;
display:block;
}

完整代码:

body {
margin: 0;
padding: 0;
font-family: sans-serif;
background: url("../images/bg.png") no-repeat;
background-attachment: fixed;
}

header {
background-color: #088CAF;
color: white;
width: 100%;
padding: 20px 0;
text-align: center;
}

.navigation {
position: sticky;
}

.navigation ul {
background-color: #075E75;
overflow: hidden;
color: white;
padding: 0;
text-align: center;
margin: 0;
border-top: 2px solid #2E2E2E;
border-bottom: 2px solid #2E2E2E;
}

.navigation ul li {
display: inline-block;
}

.navigation li:hover {
background-color: #069DAA;
}

.navigation ul li a {
text-decoration: none;
color: white;
font-size: 20px;
padding: 20px;
display:block;
}

.footer {
color: white;
text-align: center;
width: 100%;
background-color: #2E2E2E;
border-top: 2px solid #099C9E;
position: absolute;
bottom: -800px;
height: 80px;
overflow: hidden;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Squallz Test Page</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<header>
<h1>This is a header</h1>
</header>
<div class="navigation">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Repo</a></li>
<li><a href="#">Code</a></li>
</ul>
</div>
<div class="footer">
<div class="footerinfo">
<p id="copyright">Copyright © Squallz 2017 - Rights Reserved</p>
<p id="info">Personal website by <b>Squallz</b></p>
</div>
</div>
</body>
</html>

关于html - 如何将整个 div 变成可点击的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43702512/

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