div垂直中央
方案1 :设置“定位”、“父元素延迟”、“子元素也定位absolute”,“顶部”设置父元素的一半“50%”,“边距顶部”设置自己的高度的一半。
style #father { width: 1000px; 高: 300 px; 背景色:天空蓝; position: relative; } #son { height: 100px; 背景色: green; 位置: absolute; top: 50%; margin-top: -50px; }/style divid=' father ' divid=' son '我是块级元素/div
如果子元素的高度不恒定,则使用css3的变换将y轴的偏移设置为-50%
#son { width: 100px; 背景色: green; 位置: absolute; left: 50%; 变换:变换y (-50 %; 也有使用flex的方法
style #father { width: 500px; 高: 300 px; 背景色:天空蓝; 显示: Flex; align-items3360中心; } #son { width: 100px; height: 100px; 背景色: green; }/style divid=' father ' divid=' son '我在块级元素/div/div行中元素的垂直中心,在父元素上显示display:table-cell; 和垂直对齐: middle; 属于就可以了:
style #father { width: 500px; 高: 300 px; 背景色:天空蓝; display :表蜂窝; 垂直对准:米; } # son { background-color : green; }/style divid=' father ' span id=' son '啊/span/div正文转载自https://blog.csdn.net/weixin _ 37580235/articl le