這篇文章將為大家詳細講解有關css怎么實現(xiàn)圖片大于div時的居中顯示,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
當圖片大于p時,想要圖片居中顯示,如果圖片等比例縮小可能會導致圖片不能填充整個p,如果直接將圖片不設置寬高,將其外層p設置overflow:hidden;這時即使外層p設置了水平垂直居中,圖片也不是居中的效果:
解決方法:
1- 把圖片設置為背景圖片
.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;
.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怎么實現(xiàn)圖片大于div時的居中顯示就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。