html - 如何在没有 jQuery 或 JavaScript 的情况下将 'a' -link 扩展到整个 div?

标签 html css

有两个 div,每个都有一个图像和超链接。目标是强制每个 div 成为一个完整的超链接——无需 jQuery、JavaScript 或 Onclick 函数。

我现在有什么:

.near {
  display: flex;
  align-items: stretch;
  justify-content: space-between;
}

.previous,
.next {
  width: 50%;
  padding: 20px 30px;
  font-size: 1em;
  font-style: italic;
  font-weight: 400;
  cursor: pointer;
  align-items: center;
  display: flex;
  text-align: center;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 0 1px #5C3317;
}

.previous:hover,
.next:hover {
  box-shadow: 0 0 10px #5C3317;
  background: lightgreen;
}

.previous a,
.next a {
  color: #000;
}

.previous i {
  float: left;
  padding-right: .7em;
  align-self: center;
}

.next i {
  float: right;
  padding-left: .7em;
  align-self: center;
}

.text {
  width: 100%;
}
<script src="https://use.fontawesome.com/d91d412715.js"></script>
<div class="near">
  <div class="previous">
    <i class="fa fa-hand-spock-o fa-3x" aria-hidden="true"></i>
    <div class="text">
      <a href="http://google.com/">Hello</a>
    </div>
  </div>
  <div class="next">
    <div class="text">
      <a href="http://google.com/">Bye-bye</a>
    </div>
    <i class="fa fa-bicycle fa-3x" aria-hidden="true"></i>
  </div>
</div>

我怎样才能做到这一点?

最佳答案

您可以看到我所做的更改,因为它们是向左缩进的 CSS 规则。简而言之,您需要使字体很棒的图标绝对定位,以便链接元素可以占据整个框区域。

.near {
  display: flex;
  align-items: stretch;
  justify-content: space-between;
}

.previous,
.next {
  width: 50%;
  padding: 0;
  font-size: 1em;
  font-style: italic;
  font-weight: 400;
  cursor: pointer;
  align-items: center;
  display: flex;
  text-align: center;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 0 1px #5C3317;
}

.previous:hover,
.next:hover {
  box-shadow: 0 0 10px #5C3317;
  background: lightgreen;
}

.previous a,
.next a {
  color: #000;
display: block;
padding: 40px 60px;
}

.previous i {
  float: left;
  padding-right: .7em;
  align-self: center;
position: absolute;
top: 35px;
left: 35px;
}

.next i {
  float: right;
  padding-left: .7em;
  align-self: center;
position: absolute;
top: 35px;
right: 35px;
}

.text {
  width: 100%;
z-index: 2;
}
<script src="https://use.fontawesome.com/d91d412715.js"></script>
<div class="near">
  <div class="previous">
    <i class="fa fa-hand-spock-o fa-3x" aria-hidden="true"></i>
    <div class="text">
      <a href="http://google.com/">Hello</a>
    </div>
  </div>
  <div class="next">
    <div class="text">
      <a href="http://google.com/">Bye-bye</a>
    </div>
    <i class="fa fa-bicycle fa-3x" aria-hidden="true"></i>
  </div>
</div>

关于html - 如何在没有 jQuery 或 JavaScript 的情况下将 'a' -link 扩展到整个 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39576000/

相关文章:

javascript - 在 KineticJS 中使用多边形 mask /剪辑图像

html - 与按钮不同的 anchor 标签

css - 如何在 bootstrap 和 sass 中更改导航栏的属性值

css - 使用 CSS 将 "*"添加到输入占位符

html - 无法在输入框中选择文本

html - 如何在 Google Apps 脚本中将 css 连接到 html

javascript - 如何使用 Jquery 在按下输入键时向表中添加行?

javascript - 将数据从 GoogleChart 保存到 CSV

html - 包装容器中彼此相邻的 Div CSS 高度问题

css - Magento CSS 属性背景图像 :url issue with data:image/svg+xml;base64