CSS中的背景定位
属性:background-position;
属性值:left top、left bottom、 right top 、 right bottom 、 center center 、 像素值 、 百分比。
代码如下:
<style> *{ padding: 0px; margin: 0px; } .box{ margin: 100px auto; width: 600px; height: 400px; border: 10px solid red; background-image: url("1.png"); background-repeat: no-repeat; background-size: 300px 200px; } </style> <body> <div class = "box"></div> </body> </html>
属性值(1):left top
设置background-position: left top;背景图片定位在左上角,也是默认值。
属性值(2):left bottom
设置background-position: left bottom;背景图片定位在左下角。
属性值(3):right top
设置background-position: right top;背景图片定位在右上角。
属性值(4):right bottom
设置background-position: right bottom;背景图片定位在右下角。
注意:如果第二个参数没有设置,默认为center。
属性值(5):center
设置background-position:center;背景图片定位在区域中间。两个参数,第二个参数没有设置的话,将默认为center。
属性值(6):像素值
设置background-position:“固定的两个像素值(如: 100px 100px)”,背景图片将根据所设置的像素值在指定区域进行定位。
属性值(7):百分比
设置background-position: “固定的两个百分比(如:20% 20%)”,背景图片将根据所设置的百分比在指定区域进行定位。