html - 为什么父div和孩子的身高不一样?
问题描述
我有一个看起来像按钮的链接,我想将背景颜色设置为不同的。
但是,我不明白为什么我的holder
div 没有和它的孩子一样高。我不考虑填充。
有没有一种干净的方法来解决这个问题?
.link {
background-color: green;
padding: .9rem 3rem;
}
.holder {
background-color: red;
text-align: center;
margin-top: 40px;
}
<div class="holder">
<a href="#" class="link">LINK</a>
</div>
解决方案
You need to add display: inline-block
to your .link
element:
.link {
background-color: green;
padding: .9rem 3rem;
display: inline-block;
}
.holder {
background-color: red;
text-align: center;
margin-top: 40px;
}
<div class="holder">
<a href="#" class="link">LINK</a>
</div>
By default, <a>
elements are display: inline
, and do not have their layout impacted by the containing block. That is to say, they do not allow for a height
or width
to be set, and do not respect vertical padding and margins.
推荐阅读
- jupyter-notebook - f-string 在 Jupyter 中不起作用
- javascript - SweetAlert 确认删除
- angularjs - 使用 api 调用结果填充 typeahead
- fancybox - fancybox 3 - 如何放大图像并继续放大下一张图像
- c# - 如何将列表框中的选定项目用作列表的名称
- javascript - 如何在 Angular 8 中使用 Observable.fromPromise?
- python - 如何将少数名称转换为带引号的实际字符串
- javascript - 将随机 ID 作为参数传递
- c - 使用 GCC 正确地将参数传递给主管调用
- javascript - 如何选择元素的第 n 个子元素并分配 click() 函数?