html - 使用 flex 时如何解决悬停问题

标签 html css flexbox

当我将鼠标悬停在 href 上时,我使用 flex 而不是颜色更改为红色,但我使用的是 flex one div,但是当我设置 margin-bottom: 50px;在按钮上,但我也不知道 href 高度增加和悬停在标签之外我只想悬停在标签文本上而不是外侧部分我也只需要边距按钮。谢谢

.wrapper {
  flex-direction: column;
  box-sizing: border-box;
  display: flex;
  max-width: 50%;
  text-align: center;
}
.flex-box {
  flex-direction: row;
  box-sizing: border-box;
  display: flex;
}
button {
  margin-right: 16px;
  flex: 1 1 100%;
  box-sizing: border-box;
  max-width: 50%;
  margin-bottom: 50px;
}
a {
  flex: 1 1 100%;
  box-sizing: border-box;
  max-width: 50%;
}
a:hover {
  color: red;
}
<div class="wrapper">
  <div class="flex-box">
    <button type="button">Login</button>
    <a href="#">Forget Password</a>
  </div>
</div>

最佳答案

调整flex-start的对齐方式,避免a标签被拉长:

.wrapper {
  flex-direction: column;
  box-sizing: border-box;
  display: flex;
  max-width: 50%;
  text-align: center;
}
.flex-box {
  flex-direction: row;
  box-sizing: border-box;
  display: flex;
  align-items:flex-start; /*Added this*/
}
button {
  margin-right: 16px;
  flex: 1 1 100%;
  box-sizing: border-box;
  max-width: 50%;
  margin-bottom: 50px;
}
a {
  flex: 1 1 100%;
  box-sizing: border-box;
  max-width: 50%;
}
a:hover {
  color: red;
}
<div class="wrapper">
  <div class="flex-box">
    <button type="button">Login</button>
    <a href="#">Forget Password</a>
  </div>
</div>

您还可以将 margin-bottom:auto 添加到 a

.wrapper {
  flex-direction: column;
  box-sizing: border-box;
  display: flex;
  max-width: 50%;
  text-align: center;
}
.flex-box {
  flex-direction: row;
  box-sizing: border-box;
  display: flex;
}
button {
  margin-right: 16px;
  flex: 1 1 100%;
  box-sizing: border-box;
  max-width: 50%;
  margin-bottom: 50px;
}
a {
  flex: 1 1 100%;
  box-sizing: border-box;
  max-width: 50%;
  margin-bottom:auto; /*Added this*/
}
a:hover {
  color: red;
}
<div class="wrapper">
  <div class="flex-box">
    <button type="button">Login</button>
    <a href="#">Forget Password</a>
  </div>
</div>

关于html - 使用 flex 时如何解决悬停问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52566921/

相关文章:

javascript - 如何在jquery中获取除输入类型="hidden"之外的td innerhtml值

html - 如何将 CSS 选择器应用于区分大小写的属性值?

html - 垂直对齐有序列表中的样式数字

html - 在 flexbox li 元素中底部对齐图像

html - Flex Box 在 IE11 和 iPhone 上的 Safari 上重叠

javascript - jQuery val() 无法在 Aloha Editor 的动态 <select> 上正常工作

html - Flex div 的高度为 228px,即使里面没有内容,我也没有在任何地方指定大小

html - 小屏幕上的文本动画故障

javascript - Justgage.js 更改标题、值、标签字体大小

html - Chrome 正在垂直拉伸(stretch)我的图像,但是在 Firefox/Edge 中一切正常