网页制作css平铺
CSS 平铺背景的方法
使用 background-repeat 属性可以实现背景图像的平铺效果。该属性控制背景图像在水平和垂直方向上的重复方式。
background-repeat: repeat | repeat-x | repeat-y | no-repeat;
repeat:默认值,背景图像在水平和垂直方向平铺。repeat-x:背景图像仅在水平方向平铺。repeat-y:背景图像仅在垂直方向平铺。no-repeat:背景图像不平铺,只显示一次。
示例代码
平铺整个背景:
body {
background-image: url('image.png');
background-repeat: repeat;
}
水平平铺:
div {
background-image: url('image.png');
background-repeat: repeat-x;
}
垂直平铺:
section {
background-image: url('image.png');
background-repeat: repeat-y;
}
不平铺:
article {
background-image: url('image.png');
background-repeat: no-repeat;
}
使用简写属性
background 简写属性可以一次性设置多个背景属性,包括平铺方式:
.container {
background: url('image.png') repeat;
}
平铺模式的选择
根据设计需求选择合适的平铺模式:
- 全平铺适合无缝纹理或图案背景。
- 单方向平铺适合创建条纹或渐变效果。
- 不平铺适合单独显示的背景图像。
注意事项
- 确保背景图像本身是无缝的,否则平铺后会出现明显的接缝。
- 对于大尺寸背景图像,平铺可能会导致性能问题。
- 可以使用 CSS3 的
background-size属性调整平铺图像的大小。







