Skip to content

<a> 标签

<a> 标签定义超链接,用于从一个页面链接到另一个页面或页面内的特定部分。

概述

<a> 标签(anchor element)创建指向其他网页、文件、位置、电子邮件地址或其他 URL 的超链接。href 属性指定了链接的目标。

语法

html
<a href="URL">链接文本</a>

属性

href 属性

指定链接的目标 URL:

html
<a href="https://www.example.com">访问示例网站</a>
<a href="page.html">跳转到页面</a>
<a href="#section1">跳转到第一部分</a>
<a href="mailto:someone@example.com">发送邮件</a>
<a href="tel:+1234567890">拨打电话</a>

target 属性

指定链接在何处打开:

html
<a href="https://www.example.com" target="_blank">在新窗口打开</a>
<a href="page.html" target="_self">在当前窗口打开(默认)</a>
<a href="page.html" target="_parent">在父框架中打开</a>
<a href="page.html" target="_top">在整个窗口中打开</a>

其他常用属性

html
<a href="https://www.example.com" 
   title="悬停时显示的提示信息"
   rel="noopener noreferrer"
   download="filename.html">
   链接文本
</a>

基本用法示例

外部链接

html
<a href="https://www.google.com">访问 Google</a>
<a href="https://github.com" target="_blank">在新窗口打开 GitHub</a>

内部链接

html
<a href="about.html">关于我们</a>
<a href="../contact/contact.html">联系我们</a>

锚点链接

html
<!-- 链接到页面内的元素 -->
<a href="#chapter1">跳转到第一章</a>
<a href="#top">回到顶部</a>

<!-- 目标元素 -->
<h2 id="chapter1">第一章</h2>
<a id="top"></a>

链接到邮箱和电话

html
<a href="mailto:someone@example.com">发送邮件</a>
<a href="mailto:someone@example.com?subject=主题&body=邮件内容">发送带主题和内容的邮件</a>
<a href="tel:+1234567890">拨打电话</a>
<a href="sms:+1234567890">发送短信</a>

样式化链接

基本链接样式

html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>链接样式</title>
    <style>
        /* 未访问的链接 */
        a:link {
            color: #0066cc;
            text-decoration: none;
        }
        
        /* 已访问的链接 */
        a:visited {
            color: #800080;
        }
        
        /* 鼠标悬停 */
        a:hover {
            color: #ff6600;
            text-decoration: underline;
        }
        
        /* 被激活的链接 */
        a:active {
            color: #ff0000;
        }
        
        /* 按钮样式的链接 */
        .button-link {
            display: inline-block;
            padding: 10px 20px;
            background-color: #0066cc;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            transition: background-color 0.3s;
        }
        
        .button-link:hover {
            background-color: #0052a3;
        }
    </style>
</head>
<body>
    <p>这是一个<a href="https://example.com">普通链接</a>。</p>
    <p><a href="https://example.com" class="button-link">按钮样式的链接</a></p>
</body>
</html>

导航菜单链接

html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>导航菜单</title>
    <style>
        .nav-menu {
            list-style: none;
            padding: 0;
            margin: 0;
            display: flex;
            background-color: #333;
        }
        
        .nav-menu li {
            margin: 0;
        }
        
        .nav-menu a {
            display: block;
            color: white;
            padding: 14px 20px;
            text-decoration: none;
            transition: background-color 0.3s;
        }
        
        .nav-menu a:hover {
            background-color: #555;
        }
        
        .nav-menu a.active {
            background-color: #0066cc;
        }
    </style>
</head>
<body>
    <ul class="nav-menu">
        <li><a href="#home" class="active">首页</a></li>
        <li><a href="#about">关于</a></li>
        <li><a href="#services">服务</a></li>
        <li><a href="#contact">联系</a></li>
    </ul>
</body>
</html>

图像链接

html
<a href="https://example.com">
    <img src="logo.png" alt="公司 Logo" width="100" height="50">
</a>

<a href="page.html">
    <img src="thumbnail.jpg" alt="缩略图">
    <span>文章标题</span>
</a>

下载链接

html
<a href="document.pdf" download>下载 PDF 文档</a>
<a href="image.jpg" download="my-image.jpg">下载并重命名图片</a>

安全考虑

使用 rel="noopener noreferrer" 属性保护用户安全:

html
<a href="https://external-site.com" target="_blank" rel="noopener noreferrer">安全的外部链接</a>

无障碍访问

html
<a href="document.pdf" aria-label="下载产品手册 PDF">
    <img src="pdf-icon.png" alt=""> 产品手册
</a>

<a href="#main-content" class="skip-link">跳转到主要内容</a>

<style>
.skip-link {
    position: absolute;
    top: -40px;
    left: 6px;
    background: #000;
    color: #fff;
    padding: 8px;
    text-decoration: none;
}

.skip-link:focus {
    top: 6px;
}
</style>

响应式链接

html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>响应式链接</title>
    <style>
        .responsive-link {
            display: block;
            padding: 15px;
            margin: 10px 0;
            background-color: #f0f0f0;
            text-align: center;
            text-decoration: none;
            color: #333;
            border-radius: 5px;
            transition: all 0.3s;
        }
        
        .responsive-link:hover {
            background-color: #0066cc;
            color: white;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        
        @media (min-width: 768px) {
            .responsive-link {
                display: inline-block;
                margin: 0 10px;
                min-width: 150px;
            }
        }
    </style>
</head>
<body>
    <a href="#section1" class="responsive-link">第一节</a>
    <a href="#section2" class="responsive-link">第二节</a>
    <a href="#section3" class="responsive-link">第三节</a>
</body>
</html>

浏览器兼容性

<a> 标签在所有浏览器中都得到完全支持:

  • Chrome
  • Firefox
  • Safari
  • Edge
  • Internet Explorer

最佳实践

  1. 始终为链接提供有意义的文本内容
  2. 对于外部链接,考虑使用 target="_blank"rel="noopener noreferrer"
  3. 使用 title 属性为链接提供额外信息
  4. 确保链接文本能够清楚地说明链接的目的地
  5. 为图像链接提供适当的 alt 文本
  6. 使用 CSS 伪类 (:link, :visited, :hover, :active) 来设置链接的不同状态
  7. 确保链接在键盘导航下可用(通过 Tab 键)

相关标签

  • <link> - 外部资源链接(用于 CSS 等)
  • <nav> - 导航区域
  • <img> - 图像标签