0
Follow
0
View

2 divs in a container, how does one of them automatically center vertically

dzqfengxiang 注册会员
2023-02-27 17:13

div2 dynamic width by css style is difficult to control, unless you use js to control, it is recommended to use a fixed width to achieve horizontal centering
for example: width:80%; position: absolute; height: 100%; top:0px; left:50%; margin-left:-40%;

dongchao_123 注册会员
2023-02-27 17:13
height: 100%; width: 100%; position: relative; } DIV1 { width: 14%; height: 14%; display: flex; /* 将display属性改为flex */ align-items: center; /* 垂直居中 */ } DIV2 { display: inline-block; position: absolute; height: 100%; left: 14%; /* 与DIV1相距14% */ top: 0; bottom: 0; right: 0; } .
< !- - - - - >
edcrfvwen 注册会员
2023-02-27 17:13
height: 100%; width: 100%; display: flex; align-items: center; } DIV1{ width: 14%; height: 14%; } DIV2{ height: 100%;} .
< !- - - - - >

About the Author

Question Info

Publish Time
2023-02-27 17:13
Update Time
2023-02-27 17:13