gpt4 book ai didi

html - 垂直对齐方法

转载 作者:搜寻专家 更新时间:2023-10-31 08:18:58 24 4
gpt4 key购买 nike

vertically-align 的最佳方法是什么?与它的元素尺寸相关的东西。截至目前,我只知道 vertical-align:middle;<tr> 中运行良好.

是否还有我忽略的任何其他方法可以在程序上实现这一目标?

最佳答案

这里有 3 个非常好的技巧,可以让子元素在容器中垂直居中 - 即使您不知道子元素的高度。前2个来自this CSS-tricks article

标记(适用于所有方法):

<div class="block">    
<div class="centered">
Some text
</div>
</div>

解决方案 #1:CSS 表格方法 ( FIDDLE )

CSS:

/* This parent can be any width and height */
.block {
display: table;
width: 100%;
background: orange;
height: 300px;
}


.centered {
display: table-cell;
text-align: center;
vertical-align: middle;
background: pink;
}

解决方案#2:伪('Ghost')元素方法( FIDDLE )

CSS:

/* This parent can be any width and height */
.block {
text-align: center;
background: orange;
height: 300px;

}

/* The ghost, nudged to maintain perfect centering */
.block:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em; /* Adjusts for spacing */
}

/* The element to be centered, can
also be of any width and height */
.centered {
display: inline-block;
vertical-align: middle;
width: 300px;
background: pink;
}

解决方案#3:Flexbox ( FIDDLE )

(相关)CSS:

.block {
background: orange;
height: 300px;
display: flex;
align-items: center;
}

关于html - 垂直对齐方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20763508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com