嘿,我有以下 CSS 代码:
.parent
{
position : 'absolute';
top : '50px';
left : '50px';
width : '400px';
height : '160px';
padding : '10px';
border : '2px solid';
border-color : '#444444';
background-color : '#FF0000';
text-align : 'center';
/*display : inline; tried this also and didn't work.*/
}
.child
{
color : '#123456';
font-size : '16px';
font-family : 'Arial';
vertical-align : 'middle';
}
我只想将子项的内容放在父级 div 的中心(x 和 y),但它不起作用,它只显示父级元素顶部的文本。有什么建议吗?谢谢。
最佳答案
vertical-align
有一个骗人的名字。它实际上并不像您认为的那样垂直对齐元素。
如果您的 child 只有一行文本,您可以使用 line-height
技巧将其居中:
.parent {
line-height: 160px; /* Height of the parent */
}
另一种方法是伪造一个表:
.parent {
display: table;
}
.child {
display: table-cell;
vertical-align: middle;
}
关于html - CSS 垂直对齐属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16205907/