
在网页开发中,超链接()和按钮(
尽管和
一个常见的错误是标签未被正确闭合,导致其作用域意外扩展,使得后续不应成为链接的文本或元素也变成了可点击的超链接。
考虑以下不正确的HTML结构:
<body>
<p>点击下方按钮前往YouTube</p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/1154">
<img src="https://img.php.cn/upload/ai_manual/000/000/000/175680126049574.png" alt="Motiff">
</a>
<div class="aritcle_card_info">
<a href="/ai/1154">Motiff</a>
<p>Motiff是由猿辅导旗下的一款界面设计工具,定位为“AI时代设计工具”</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="Motiff">
<span>148</span>
</div>
</div>
<a href="/ai/1154" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="Motiff">
</a>
</div>
<p><span>立即学习</span>“<a href="https://pan.quark.cn/s/cb6835dc7db1" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">前端免费学习笔记(深入)</a>”;</p>
<div>
<a href="https://www.youtube.com/" method="get" target="_blank">
<button class="button button1">Youtube</button>
</div> <!-- </a> 标签在此处缺失 -->
<div>
<p>点击下方按钮前往Reddit</p>
<a href="https://www.reddit.com/" method="get" target="_blank">
<button class="button button2" style="border: 0; background: transparent">
@@##@@</button>
</div> <!-- </a> 标签在此处缺失 -->
</body>在上述代码中,第一个标签在包裹
为了避免上述问题并创建语义清晰、易于维护的交互元素,推荐以下两种方法:
这种方法利用标签处理导航,同时将一个 HTML结构: CSS样式: 在上述代码中,我们移除了method="get"属性,因为它通常用于表单提交,对于简单的超链接导航是不必要的。同时,为图片添加了alt属性,增强了可访问性。 如果按钮内容只是纯文本,也可以直接将标签样式化为按钮的视觉效果。 HTML结构: CSS样式: 这种方法更加简洁,适用于纯文本按钮。<body>
<p>点击下方按钮前往YouTube</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/cb6835dc7db1" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">前端免费学习笔记(深入)</a>”;</p>
<div>
<a href="https://www.youtube.com/" target="_blank">
<div class="button button1">Youtube</div>
</a>
</div>
<div>
<p>点击下方按钮前往Reddit</p>
<a href="https://www.reddit.com/" target="_blank">
<div class="button button2">
@@##@@
</div>
</a>
</div>
</body>body {
font-family: arial, sans-serif;
}
/* 通用按钮样式 */
.button {
border: none;
color: white; /* 文本按钮颜色 */
padding: 20px;
text-align: center;
text-decoration: none; /* 移除链接下划线 */
display: inline-block; /* 使其可以设置宽度和高度,并保持行内特性 */
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
/* 确保文本内容居中,并继承父a标签的文本颜色 */
color: inherit;
}
/* YouTube 按钮特定样式 */
.button1 {
background-color: #E44040;
border-radius: 2px;
}
/* Reddit 按钮特定样式 */
.button2 {
/* 背景透明,因为内部是图片 */
background: transparent;
padding: 0; /* 图片按钮通常不需要内边距 */
border-radius: 12px;
}
/* 确保图片在按钮内部居中 */
.button2 img {
vertical-align: middle;
}
/* 移除a标签的默认下划线 */
a {
text-decoration: none;
}3.2 方法二:直接样式化标签
<a href="https://www.youtube.com/" target="_blank" class="button button1">Youtube</a>
.button {
background-color: #E44040;
border: none;
color: white;
padding: 20px;
text-align: center;
text-decoration: none; /* 移除下划线 */
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
border-radius: 2px;
}3.3 关于
以上就是HTML/CSS超链接与按钮:避免意外链接行为的结构化指南的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号