div {
width: 200px;
height: 200px;
border: 1px dashed #ccc;
background-repeat: no-repeat;
background-position: center center;
background-image: url(img/bg.gif);
}
x% y%:表示使用百分比定位,是将图像本身(x%, y%)的那个点,与背景区域的(x%, y%)的那个点重合。最终得到背景图像起始位置坐标的计算公式为:
x = (对象的宽度 - 图像的宽度) * x%;
y = (对象的高度 - 图像的高度) * y%;
- 0 0 左上角对齐 left top
- 50% 50%居中对齐 center
- 100% 100% 右下角对齐 right bottom