HTML/CSS 左右浮动div内内容无法垂直居中问题求助
修复方案
推荐使用Flex布局替换原有浮动方案,实现逻辑更简单、稳定性更强,可完美覆盖需求。修改后的CSS代码如下:
.header { background-color: gray; color: black; font-size: 24pt; height: 6%; max-height: 6%; overflow: hidden; /* 新增flex布局配置 */ display: flex; justify-content: space-between; align-items: stretch; } .allSelector { width: 50%; /* 移除原有float:left,新增以下配置实现内容靠左下角显示 */ display: flex; align-items: flex-end; justify-content: flex-start; } .selectorInsides { overflow: hidden; /* 可选:调整和图片的横向间距 */ margin-left: 4px; } .allSelector > img { height: 80%; object-fit: contain; } .legend { width: 50%; /* 移除原有float:right、line-height、vertical-align,新增以下配置实现内容靠右上角显示 */ display: flex; align-items: flex-start; justify-content: flex-end; }
如果必须保留原有浮动方案,可通过相对+绝对定位实现,修改后的CSS代码如下:
.allSelector { width: 50%; height: 100%; float: left; position: relative; } .allSelector > img { position: absolute; bottom: 0; left: 0; height: 80%; object-fit: contain; } .selectorInsides { overflow: hidden; position: absolute; bottom: 0; left: 30px; /* 可根据实际图片宽度调整间距数值 */ } .legend { line-height: 24pt; height: 100%; width: 50%; float: right; text-align: right; position: relative; } .legend { position: absolute; top: 0; right: 0; }
内容的提问来源于stack exchange,提问作者A_Name
相关产品推荐
相关产品推荐

