【问题标题】:Avoid extra space in anchor caused by line height - flex layout避免由行高引起的锚点中的额外空间 - flex布局
【发布时间】:2020-01-22 03:38:02
【问题描述】:

我有这组带有链接的框,它们显示为弹性项目。问题是由于每个框的高度,可点击区域(黄色区域)的覆盖范围不一致。将锚标记的行高设置为零将解决此问题,但会引入其他问题,例如重叠文本。如何在保持行高和 flex 行为的同时解决此问题?

谢谢

ul {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
}

li {
  border: 1px solid red;
  background: #fff;
  flex: 1 1 0;
}

a {
  background: yellow;
  text-decoration: none;
  padding: 40px;
  display: inline-block;
  line-height: 1.3;
  /*height: 100%;
  box-sizing: border-box;*/
  width: 100%;
}
<ul>
  <li>
    <a href="#">
        one 
      </a>
  </li>
  <li>
    <a href="#">
        two blah blahdfdfdfdfdfdfd
      </a>
  </li>
  <li>
    <a href="#">
        three blah blah
      </a>
  </li>
  <li>
    <a href="#">
        four
      </a>
  </li>
  <li>
    <a href="#">
        five
      </a>
  </li>
  <li>
    <a href="#">
        six
      </a>
  </li>
</ul>

【问题讨论】:

  • a {height: 100%; box-sizing: border-box;}
  • 谢谢!
  • 嗨 Niet,我也在尝试使框的宽度相同。将 flex: 1 1 0 添加到 flex 项,但没有更改。知道为什么吗?谢谢。我已经更新了我的原始代码 sn-p 以显示问题

标签: html css flexbox line-height


【解决方案1】:

您必须将height: 100%; box-sizing: border-box; 添加到a css

ul {
  list-style: none;
  display: flex;
}

li {
  border: 1px solid red;
  background: #fff;
}

a {
  background: yellow;
  text-decoration: none;
  padding: 40px;
  display: inline-block;
  line-height: 1.3;
  height: 100%;
  box-sizing: border-box;
}
<ul>
  <li>
    <a href="#">
        one 
      </a>
  </li>
  <li>
    <a href="#">
        two blah blah
      </a>
  </li>
  <li>
    <a href="#">
        three blah blah
      </a>
  </li>
  <li>
    <a href="#">
        four
      </a>
  </li>
  <li>
    <a href="#">
        five
      </a>
  </li>
  <li>
    <a href="#">
        six
      </a>
  </li>
</ul>

【讨论】:

  • 感谢马蒂亚斯的回答
  • 嗨,Matias,我也在尝试使框的宽度相同。将 flex: 1 1 0 添加到 flex 项,但没有更改。知道为什么吗?谢谢。我已经更新了我的原始代码 sn-p 以显示问题。
猜你喜欢
  • 1970-01-01
  • 2016-02-08
  • 1970-01-01
  • 2014-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多