這篇文章將為大家詳細講解有關css怎么實現圖片大于div時的居中顯示,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
當圖片大于p時,想要圖片居中顯示,如果圖片等比例縮小可能會導致圖片不能填充整個p,如果直接將圖片不設置寬高,將其外層p設置overflow:hidden;這時即使外層p設置了水平垂直居中,圖片也不是居中的效果:
解決方法:
1- 把圖片設置為背景圖片
<div class="face-img-contain" id="face-img-back"> </div>
.face-img-contain{
width:348px;
height:436px;
margin:0 auto;
margin-top: 14px;
position: relative;
background-image: url(../images/face-img/test-22.png);
background-repeat: no-repeat;
background-position: center;
background-size: cover;
display: flex;
justify-content: center;
align-items: center;
border: 1px solid gainsboro;
}若是后臺返回的地址,別忘了拼接方法正確
$("#face-img-back").css("background-image","url("+faceImg+")");2- 給圖片設置相對p的100%的寬高,再設置object-fit:cover;
<div class="face-img-contain-new-new">
<img src="../images/face-img/test-22.png" alt="" class="face-img-defined1" id="face-img-photo">
</div>.face-img-contain-new-new{
width:348px;
height:436px;
margin:0 auto;
margin-top: 14px;
position: relative;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
border: 1px solid gainsboro;
}
.face-img-defined1{
width:100%;
height:100%;
object-fit:cover;
}關于css怎么實現圖片大于div時的居中顯示就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。