怎样使一个div中a标签的文字不管传入多少都上下左右居中?

要求,a大小填充占整个div1,a中文字上下左右居中,a中文字多少不确定?
不知如何解决?

<div class="div1">
<a herf="xxxx">hehehehehh</a>
</div>

.div1{
width:300px;
height:80px;
}
.div1 a{

}
阅读 10.3k
2 个回答
.div1{ width:300px;height:80px; background:#eee; position:relative; text-align: center;display:table; }
.div1 a{ display:table-cell; vertical-align:middle; }

注意兼容性,推荐直接用 table 标签写

translate -50%

.div1{
  width: 300px;
  height: 400px;
  background-color: #eee;
  position: relative
}
.div1 a{
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate3d(-50%, -50%, 0)
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题