如何使用CSS設(shè)置九宮格圖片
在CSS中設(shè)置九宮格圖片,可以通過使用grid
布局或者position
定位來實(shí)現(xiàn),下面分別介紹這兩種方法:
方法一:使用grid布局
HTML結(jié)構(gòu):
<div class="grid-container"> <img class="grid-item" src="image1.jpg" alt="image1"> <img class="grid-item" src="image2.jpg" alt="image2"> <img class="grid-item" src="image3.jpg" alt="image3"> </div>
CSS樣式:
.grid-container { display: grid; grid-template-columns: 1fr 1fr 1fr; grid-gap: 10px; } .grid-item { width: 100%; height: 0; object-fit: cover; }
方法二:使用position定位
HTML結(jié)構(gòu):
<div class="container"> <img class="item" src="image1.jpg" alt="image1"> <img class="item" src="image2.jpg" alt="image2"> <img class="item" src="image3.jpg" alt="image3"> </div>
CSS樣式:
.container { position: relative; width: 300px; height: 300px; } .item { position: absolute; width: 100px; height: 100px; object-fit: cover; } .item:nth-child(1) { top: 0; left: 0; } .item:nth-child(2) { top: 0; right: 0; } .item:nth-child(3) { bottom: 0; left: 0; }
版權(quán)聲明:除非特別標(biāo)注,否則均為本站原創(chuàng)文章,轉(zhuǎn)載時(shí)請(qǐng)以鏈接形式注明文章出處。