style.background属性
style.background属性是HTML DOM中的一个属性,它可以用来设置元素的背景样式,包括背景颜色、背景图片、背景位置、背景大小等信息。
使用方法
- 设置背景颜色:style.background="color",其中color可以是颜色的十六进制值,也可以是颜色的英文名称,例如:style.background="red"或style.background="#FF0000"。
- 设置背景图片:style.background="url(图片地址)",图片地址可以是远程的,也可以是本地的,例如:style.background="url(http://www.example.com/image.jpg)"或style.background="url(/images/image.jpg)"。
- 设置背景位置:style.backgroundPosition="x y",其中x和y分别表示水平和垂直方向的偏移量,单位可以是px、%、em等,例如:style.backgroundPosition="50% 50%"。
- 设置背景大小:style.backgroundSize="width height",其中width和height分别表示宽度和高度,单位可以是px、%、em等,例如:style.backgroundSize="100px 100px"。
- 设置背景重复:style.backgroundRepeat="repeat|no-repeat|repeat-x|repeat-y",其中repeat表示水平和垂直方向都重复,no-repeat表示不重复,repeat-x表示水平方向重复,repeat-y表示垂直方向重复,例如:style.backgroundRepeat="repeat-x"。
// 例子 var div = document.getElementById("div"); div.style.background = "#FF0000"; div.style.backgroundPosition = "50% 50%"; div.style.backgroundSize = "100px 100px"; div.style.backgroundRepeat = "repeat-x";
以上就是style.background属性的使用方法,可以用来设置元素的背景样式,使得网页变得更加美观。