英文:
'vertical-align:middle' in absolutely positioned element within absolutely positioned parent
问题
我正在使用自定义表格(全部都是div,没有table、th、tr等元素)进行工作。我试图实现的目标是垂直对齐子元素(单元格)的内容(这些子元素是绝对定位的)。我尝试过其他解决方法,但似乎不起作用,因为父元素(行)也是绝对定位的。
我还尝试过其他的技巧/解决方法(设置行高与单元格高度相同)。由于单元格中还有其他我不想干扰的元素,Flex对齐也不是最佳选择。
要复制我的布局,可以使用以下代码:
<div class='row'>
<div class='cell cell-x'>
内容
</div>
</div>
.row {
position: absolute;
left: 0;
}
.cell{
position: absolute;
overflow: hidden;
white-space: nowrap;
padding: 4px 6px 3px 6px;
border-right: 1px solid rgba(0, 0, 0, 0.2);
border-bottom: 1px solid rgba(0, 0, 0, 0.2);
background: white;
outline: none;
}
.cell-x {
left: 0px;
top: 50px;
width: 112px;
height: 50px;
vertical-align: middle;
}
- 单元格的高度是已知的。
我还尝试设置.row { display: table; }
和 .cell .general { display: table-cell;}
。
限制:不能更改任何元素的位置。
英文:
I'm working with a custom table (all divs, no table>, no th>, tr> etc...)
What I'm trying to achieve is to vertically align the content of the children (cell) element (which is absolutely positioned). I've tried other workarounds but they seem not to work since the parent (row) element is also absolutely positioned
I've also tried other hacks/workarounds (setting the line-height same as the height of the cell).
Flex alignment is also not optimal since there are other elements in the cell that I don't want to intervene in.
To reproduce my layout
<div class='row'>
<div class='cell cell-x'>
CONTENT
</div>
</div>
.row {
position: absolute;
left: 0;
}
.cell{
position: absolute;
overflow: hidden;
white-space: nowrap;
padding: 4px 6px 3px 6px;
border-right: 1px solid rgba(0, 0, 0, 0.2);
border-bottom: 1px solid rgba(0, 0, 0, 0.2);
background: white;
outline: none;
}
.cell-x {
left: 0px;
top: 50px;
width: 112px;
height: 50px;
vertical-align: middle;
}
- Height of the cell is known.
I also tried setting .row { display: table; } and .cell .general { display: table-cell;}
Restrictions: Neither of the elements' positions can be changed.
答案1
得分: 1
你可以通过结合多种技巧来实现所需的效果。以下是一个你可以尝试的方法:
在单元格内添加一个 包装元素 以充当内容的容器:
<div class='row'>
<div class='cell cell-x'>
<div class="content-wrapper">
内容
</div>
</div>
</div>
将以下样式应用于 包装元素:
.content-wrapper {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
调整 单元格 和 行元素 的样式:
.row {
position: absolute;
left: 0;
}
.cell {
position: absolute;
overflow: hidden;
white-space: nowrap;
padding: 4px 6px 3px 6px;
border-right: 1px solid rgba(0, 0, 0, 0.2);
border-bottom: 1px solid rgba(0, 0, 0, 0.2);
background: white;
outline: none;
}
.cell-x {
left: 0px;
top: 50px;
width: 112px;
height: 50px;
}
通过在内容包装器中添加 height: 100%
并设置 display: flex
, align-items: center
, 和 justify-content: center
,无论单元格的高度如何,内容都会在单元格内 垂直居中对齐。
英文:
You can achieve the desired result by using a combination of techniques. Here's an approach you can try:
Add a wrapper element inside the cell to act as a container for the content:
<div class='row'>
<div class='cell cell-x'>
<div class="content-wrapper">
CONTENT
</div>
</div>
</div>
Apply the following styles to the wrapper element:
.content-wrapper {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
Adjust the styles for the cell and row elements:
.row {
position: absolute;
left: 0;
}
.cell {
position: absolute;
overflow: hidden;
white-space: nowrap;
padding: 4px 6px 3px 6px;
border-right: 1px solid rgba(0, 0, 0, 0.2);
border-bottom: 1px solid rgba(0, 0, 0, 0.2);
background: white;
outline: none;
}
.cell-x {
left: 0px;
top: 50px;
width: 112px;
height: 50px;
}
By adding the height: 100% to the content wrapper and setting display: flex, align-items: center, and justify-content: center, the content will be vertically aligned within the cell, regardless of the height of the cell.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论