html - “垂直对齐:顶部”不适用于表格单元格的图像+跨度内容,尽管所有儿童元素都是非块状的
问题描述
我需要text
标签位于以黄色背景突出显示的表格单元格的顶部。
虽然vertical-align: top;
定义td
的选择器和图像元素是inline-block
(不是block
),表底部的“文本”。
.table {
width: 200px;
background: #FCF3CF;
}
.table td {
vertical-align: top;
}
.image-defaultBackgroundUnderlay {
display: inline-block;
width: 60px;
height: 60px;
position: relative;
border: 1px solid #ececec;
background: white;
}
.image-imageLayer {
position: absolute;
width: 100%;
height: 100%;
top: 0;
background-position: center;
background-repeat: no-repeat;
background-size: contain;
}
<table class="table">
<tr>
<td>
<div class="image-defaultBackgroundUnderlay">
<div class="image-imageLayer" style="background-image: url(https://dummyimage.com/960x540/626ffc/e6e6e6.png);"></div>
</div>
<span>text</span>
</td>
</tr>
</table>
请不要包装解决方案。
解决方案
您需要veritcal-align:top
在图像上而不是td
.table {
width: 200px;
background: #FCF3CF;
}
.table td {
/*vertical-align: top;*/
}
.image-defaultBackgroundUnderlay {
display: inline-block;
width: 60px;
height: 60px;
position: relative;
border: 1px solid #ececec;
background: white;
vertical-align:top
}
.image-imageLayer {
position: absolute;
width: 100%;
height: 100%;
top: 0;
background-position: center;
background-repeat: no-repeat;
background-size: contain;
}
<table class="table">
<tr>
<td>
<div class="image-defaultBackgroundUnderlay">
<div class="image-imageLayer" style="background-image: url(https://dummyimage.com/960x540/626ffc/e6e6e6.png);"></div>
</div>
<span>text</span>
</td>
</tr>
</table>
在您的情况下,元素已经与顶部对齐。为了更好地理解这里是一个插图:
.table {
width: 200px;
background: #FCF3CF;
}
.table td {
height: 150px;
}
.image-defaultBackgroundUnderlay {
display: inline-block;
width: 60px;
height: 60px;
position: relative;
border: 1px solid #ececec;
background: white;
}
.image-imageLayer {
position: absolute;
width: 100%;
height: 100%;
top: 0;
background-position: center;
background-repeat: no-repeat;
background-size: contain;
}
no alignement
<table class="table">
<tr>
<td>
<div style="border:2px solid red;">
<div class="image-defaultBackgroundUnderlay">
<div class="image-imageLayer" style="background-image: url(https://dummyimage.com/960x540/626ffc/e6e6e6.png);"></div>
</div>
<span>text</span>
</div>
</td>
</tr>
</table>
aligned to top
<table class="table">
<tr>
<td style="vertical-align:top;">
<div style="border:2px solid red;">
<div class="image-defaultBackgroundUnderlay">
<div class="image-imageLayer" style="background-image: url(https://dummyimage.com/960x540/626ffc/e6e6e6.png);"></div>
</div>
<span>text</span>
</div>
</td>
</tr>
</table>
因此vertical-align
会将包含图像和文本的整行对齐到顶部,并且不会将所有元素对齐到顶部。每个元素的对齐方式将取决于vertical-align
应用于它们
推荐阅读
- visual-studio-code - 在 VS 代码中在 Julia REPL 中运行 md 文件
- reactjs - 在 React/Nextjs 重定向之前,查找链接是否会将用户重定向到 404 页面
- sympy - 如何用 sympy 中的单个变量替换大表达式
- kotlin - 错误:类型不匹配:推断类型为 FirstFragment 但预期为上下文
- javascript - 如何在 React 的输入字段中的输入文本旁边添加永久标签
- c++ - 如何使用日期库获得两次之间的差异?
- javascript - 使用 Vue.js 在 IE 中隐藏的输入字段不显示值属性
- python - 切片列表是字典的值
- python - (django)我想在创建用户时根据他们的角色创建一个指定的模型实例
- sorting - 正确排序数据框