在网页中,会有不少的背景图像与一些小的图标等内容,在初学的时候,为了达到页面的效果,都是将原图切割成不少个独立的文件,这样,将会有几十个图像文件,在网页加载的时候,会与服务器进行几十次的交互,极其浪费资源,影响网页打开的速度,因些,实际的作法一般是将这些图像放在一个文件中,利用定位技术进行定位实现。在对背景处理时,主要用到了background-position这个属性。css
background-image:背景图像 html
background-color:设置背景颜色服务器
background-repeat:设置背景图像如何铺排填充学习
background-attachment:设置背景图像是随着对象内容滚动仍是固定ui
background-position:设置背景图像的位置url
background-position:<position> [ , <position> ]spa
默认背景图像的位置是从元素的左上角为原点显示的,也就是说,元素左上角的坐标是[0,0],背景图像的左上角默认与无素的左上角对齐。其中position的值能够是top、left、center、right、bottom等,若是两个position只指定一个,则第二个默认为center。这两个position就是其x轴和y轴坐标值,也能够指定为百分比,还能够指定为像素值。code
background-clip:指定背景绘制区域htm
background-origin:指定background-position属性应该是相对位置,若是背景图像background-attachment是“固定”,这个属性没有任何效果。对象
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>背景图像定位</title> <style> a{ display: block; width: 436px; height: 163px; border: 1px solid black; line-height: 163px; text-align: center; background-image: url("images/btn_bg_1.jpg"); } a:hover{ background-position:right; background-image: url("images/btn_bg_1.jpg"); } </style> </head> <body> <div class="father"> <a href=""> 这是超连接 </a> </div> </body> </html>
a:hover{ background-position:right; background-image: url("images/btn_bg_1.jpg"); }
当鼠标指向超连接时,其background-position:right;其只设置了X轴的位置为右对齐,并无设置Y轴的位置,因此Y轴的位置是默认值为center,最终显示的是整个图像中右边中间的那个。