将 div 包裹在锚标记内是有效的代码吗?

htmlweb developmentfront end technology更新于 2024/7/18 18:20:17

是的,根据 HTML5 规范,我们可以将 div 放置在锚标记内,例如 -

<a href="#" target="_blank">
<div class="demo"></div>
</a>

另一个示例 -

<a href= www.tutorialspoint.com>
   <div> this now becomes a link </div>
</a>

HTML5 允许将 <a> 标签包裹在 <div> 元素周围。因此,<div> 将在 <anchor> 标签内可见。

示例

让我们看一个 div 位于 <anchor> 标签内的示例 -

<!DOCTYPE html>
<html>
<head>
   <style>
      .container {
         position: relative;
         background-color: orange;
         width: 200px;
         height: 200px;
      }
      .container a {
         position: absolute;
         top: 0;
         bottom: 0;
         left: 0;
         right: 0;
         z-index: 999;
      }
   </style>
</head>
<body>
<h1>Demo Heading</h1>
<div class="container">
  <div class="div__one">
    div one
  </div>
  <div class="div__two">
    div two
  </div>
  <a href="https://tutorialspoint.com"></a>
</div>

</body>
</html>

输出

单击橙色 div,链接将打开 -


相关文章


有用资源