CSS可以通過(guò)以下方式給整個(gè)網(wǎng)頁(yè)添加背景圖:
1、使用body
元素的background
屬性:
body { background-image: url('your-image-url'); background-repeat: no-repeat; background-size: cover; }
2、使用偽元素::before
或::after
:
body::before { content: ""; background-image: url('your-image-url'); background-repeat: no-repeat; background-size: cover; position: fixed; top: 0; left: 0; right: 0; bottom: 0; }
3、使用div
元素作為背景容器:
<div class="background-container"> <div class="background-image" style="background-image: url('your-image-url')"></div> <div class="content"> <!-- 頁(yè)面內(nèi)容 --> </div> </div>
CSS:
.background-container { position: relative; height: 100vh; /* 視口高度 */ } .background-image { position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-repeat: no-repeat; background-size: cover; } .content { position: relative; z-index: 1; /* 確保內(nèi)容在背景之上 */ }
版權(quán)聲明:除非特別標(biāo)注,否則均為本站原創(chuàng)文章,轉(zhuǎn)載時(shí)請(qǐng)以鏈接形式注明文章出處。