HTML DOM 样式 backgroundRepeat 属性

htmlweb developmentfront end technology更新于 2025/9/29 17:48:17

样式 backgroundRepeat 属性用于设置或获取背景图像的重复方式。

语法

以下是语法 −

设置 backgroundRepeat 属性 −

object.style.backgroundRepeat = "repeat|repeat-x|repeat-y|no-repeat|initial|inherit"

值

以下是上述属性值 −

序号值和说明
1repeat
这是默认值。它在垂直和水平方向上重复背景图像。
2repeat-x
这仅在水平方向上重复背景图像。
3repeat-y
这仅在垂直方向上重复背景图像
4no-repeat
不重复背景图片。

示例

让我们来看一个 backgroundRepeat 属性的示例 −

<!DOCTYPE html>
<html>
<head>
<style>
   body {
      background-image: url("https://www.cainiaomax.com/power_bi/images/power-bi-minilogo.jpg");
      background-repeat: repeat-x;
      color:black;
      font-size:20px;
   }
</style>
<script>
   function changeBackRepeat(){
      document.body.style.backgroundRepeat="repeat-y";
      document.getElementById("Sample").innerHTML="The background image is now repeated vertically";
   }
</script>
</head>
<body>
<h2>Demo Heading</h2>
<p>This is demo text.</p>
<p>This is demo text.</p>
<p>This is demo text.</p>
<p>This is demo text.</p>
<p>Change the body background image repeat value by clicking the below button</p>
<button onclick="changeBackRepeat()">CHANGE REPEAT</button>
<p id="Sample"></p>
</body>
</html>

输出

这将产生以下输出 −

点击"更改重复"按钮 −


相关文章


有用资源