CSS背景样式可以很方便地覆盖整个div或者整个页面。我们可以通过在CSS中使用background属性来实现背景样式的覆盖。
/* 示例1 */ body { background: url("image.jpg") no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; } /* 示例2 */ div { background-color: rgba(0, 0, 0, 0.5); position: absolute; top: 0; left: 0; right: 0; bottom: 0; }
示例1中,我们使用了background属性来设置背景图片,使用了cover属性来让背景图覆盖整个页面。这种方式适合于需要设置全屏背景图的网站。
示例2中,我们使用了背景颜色来实现背景覆盖。通过设置div的position为absolute,并将top、left、right、bottom属性设置为0,可以让div覆盖整个页面。这种方式适合于需要在某个区域设置背景色的网站。