如何实现side-picture-container与big-product-image两个div的并排对齐?
解决两个Div并排对齐的问题
你的核心问题是给大图片加了position: absolute,让它脱离了文档流,完全不占据空间,自然没法和左侧的小图容器正常并排。另外big-product-image容器本身也没设置合适的布局属性来和左侧容器同行显示。按照下面的步骤修改CSS就能解决:
- 删掉
current-product-image里的绝对定位相关属性,让它回到文档流 - 给
big-product-image设置display: inline-block和vertical-align: top,和左侧容器保持顶部对齐 - 调整大图片的高度,让它和左侧四张小图的总高度匹配(四张图每张100px,总高度加上间距设为430px左右即可)
- 把
picture-list li的布局改成block,让小图上下排列(你之前用inline-block会导致小图横向排布,不符合需求)
修改后的完整CSS代码:
.product-container { margin-top: 4vw; display: block; } .product-image-container { display: inline-block; } .picture-list li { display: block; /* 改为block实现小图上下排列 */ width: 110px; height: 114px; border: none; margin-bottom: 8px; /* 小图之间增加间距 */ } .picture-list li img { width: 97%; height: auto; } .product-image-container .side-picture-container { width: 90px; display: inline-block; vertical-align: top; margin-top: 0px; position: relative; } .picture-list li img { height: 100px; object-fit: cover; border-radius: 10%; } .big-product-image { display: inline-block; /* 让容器与左侧并排 */ vertical-align: top; /* 与左侧容器顶部对齐 */ margin-left: 15px; /* 两个容器之间增加间距 */ } .current-product-image { height: 432px; /* 匹配左侧四张小图+间距的总高度 */ width: auto; /* 保持图片原有比例,避免变形 */ border-radius: 10%; }
调整后,两个容器会基于inline-block的特性稳定并排,大图片高度与左侧小图容器高度匹配,布局不会因为绝对定位出现偏移或重叠问题。
内容的提问来源于stack exchange,提问作者Marco Aiello
相关产品推荐
相关产品推荐

