要让背景图铺满全屏,我们需要使用CSS3的background-size属性。
首先,我们需要给元素设置背景图。
body {
background-image: url("example.jpg");
background-repeat: no-repeat;
} 然后,我们可以使用以下代码将背景图适应屏幕大小:
body {
background-image: url("example.jpg");
background-repeat: no-repeat;
background-size: cover;
} 其中,cover参数会自动调整背景图的大小,保证能够完全覆盖整个屏幕。如果不想自动调整而是完整展示背景图,则可以使用contain参数。
body {
background-image: url("example.jpg");
background-repeat: no-repeat;
background-size: contain;
} 除了使用cover和contain参数外,我们还可以使用具体的数值来设置背景图的大小。
body {
background-image: url("example.jpg");
background-repeat: no-repeat;
background-size: 100% 100%; /* 水平方向100%,垂直方向100% */
} 以上就是使用CSS3让背景图铺满全屏的方法。