HTML DOM 样式 backgroundSize 属性
htmlweb developmentfront end technology更新于 2024/5/20 13:00:17
HTML DOM 样式 background-size 属性用于设置或获取背景图像的大小。
语法
以下是语法 −
设置 backgroundSize 属性 −
object.style.backgroundSize = "auto|length|cover|contain|intial|inherit"
属性
以上属性的解释如下 −
| Sr.No | 属性 &说明 |
|---|---|
| 1 | auto 背景图片以全尺寸显示,这是默认值。 |
| 2 | length 用于设置背景图片的宽度和高度。第一个值表示宽度,第二个值表示高度。如果仅指定一个值,则缺失值默认为"自动"。 |
| 3 | percentage 用于设置背景图片的宽度和高度(以百分比表示)。百分比是根据父级图片的高度和宽度计算的。第一个值设置宽度,第二个值设置高度。如果仅指定一个值,则缺失值默认为"自动"。 |
| 4 | cover 将背景图片缩放至最大,以覆盖整个背景区域。 |
| 5 | contain 缩放图片,使其高度和宽度能够适应内容区域。 |
| 6 | initial 用于将此属性设置为初始值。 |
| 7 | inherit 用于继承父级属性值 |
示例
让我们看一下 HTML DOM 样式 backgroundSize 属性的示例 −
<!DOCTYPE html>
<html>
<head>
<style>
#DIV1{
box-shadow: 0 2px 0 4px mintcream;
padding: 45px;
background: url("https://www.cainiaomax.com/plsql/images/plsql-mini-logo.jpg");
background-size: 150px 150px;
}
</style>
<script>
function changeBackSize(){
document.getElementById("DIV1").style.backgroundSize="100px 100px";
document.getElementById("Sample").innerHTML="The background image size is now reduced";
}
</script>
</head>
<body>
<h2>PL/SQL Tutorial</h2>
<div id="DIV1"></div>
<p>Change the background image size for the above div by clicking the below button</p>
<button onclick="changeBackSize()">CHANGE SIZE</button>
<p id="Sample"></p>
</body>
</html>
输出
这将产生以下输出 −

点击"更改尺寸"按钮 −

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

