轮播图内置图片尺寸异常 一张过大被裁切一张适配模糊 CSS调整无效如何解决
轮播图图片裁切、模糊问题解决方案
问题根因
- img标签手动添加了
height="30"行内属性,会强制压缩图片高度,和CSS规则冲突导致样式调整不生效 - CSS中
.carousel img设置了min-width: 100%,当原图尺寸小于轮播容器宽度时会被强制横向拉伸,导致显示模糊 .carousel-inner固定高度370px且未设置内容适配规则,当图片实际高度超过容器高度时就会被裁切
修复方案
- 删除所有img标签上的
height="30"属性,避免行内属性覆盖CSS规则 - 调整
.carousel img的CSS规则,用object-fit属性控制图片适配逻辑,保证图片不变形、不裁切、不拉伸模糊 - 补充轮播项、图片父容器的对齐规则,保证文字和图片在容器内垂直居中显示
修正后的代码
CSS部分
<style type="text/css"> .wrapper{ font-family: Nunito; background-repeat: no-repeat; height: 100%; background-position: center; background-size: cover; overflow: hidden; position: relative; } #carouselExampleIndicators{ max-width: 100%; position: relative; margin: auto; margin-top: 0%; } .carousel-item { padding: 10px; max-width: 100%; margin: 0 auto; height: 100%; display: flex; align-items: center; } .carousel-inner { width: auto; height: 370px; max-height: 370px !important; position: relative; } .carousel img { max-width: 100%; max-height: 350px; height: auto; width: auto; object-fit: contain; display: block; margin: 0 auto; } .img1, .img2 { width: 100%; display: flex; align-items: center; justify-content: center; height: 100%; } </style>
HTML部分
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel" style="background-color: #eeeeee;"> <ol class="carousel-indicators"> <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li> <li data-target="#carouselExampleIndicators" data-slide-to="1"></li> </ol> <div class="carousel-inner"> <div class="carousel-item active"> <div class="row" style="width: 100%;margin: 0 auto;height: 100%;"> <div class="col-md-6 d-flex align-items-center"> <div class="desc1"> <h4 style="color: #081139;">LET'S GO!</h4> <p style="color: #081139;">Everyone needs to be on their toes in order to fight and stamp out bad eggs in the world</p> </div> </div> <div class="col-md-6"> <div class="img1" style="margin: 0 auto; padding: 10px;"> <img id="img1" src="Image/Carousel/singlestc.png" alt="First slide" /> </div> </div> </div> </div> <div class="carousel-item"> <div class="row" style="width: 100%;margin: 0 auto;height: 100%;"> <div class="col-md-6 d-flex align-items-center"> <div class="desc2"> <h4 style="color: #081139;">WE CARE</h4> <p style="color: #081139;">We are the world, lets make it a better place for the younger generations</p> </div> </div> <div class="col-md-6"> <div class="img2"> <img id="img2" src="Image/Carousel/SNC.png" alt="Second slide"/> </div> </div> </div> </div> </div> <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev"> <span class="carousel-control-prev-icon" aria-hidden="true"></span> <span class="sr-only">Previous</span> </a> <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next"> <span class="carousel-control-next-icon" aria-hidden="true"></span> <span class="sr-only">Next</span> </a> </div>
内容的提问来源于stack exchange,提问作者Donald Uko
相关产品推荐
相关产品推荐

