Home  >  Article  >  Web Front-end  >  CSS background-position随笔_html/css_WEB-ITnose

CSS background-position随笔_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:40:20999browse

1.定义和用法

background-position 属性设置背景图像的起始位置。

这个属性设置背景原图像(由 background-image 定义)的位置,背景图像如果要重复,将从这一点开始。

提示:您需要把 background-attachment 属性设置为 "fixed",才能保证该属性在 Firefox 和 Opera 中正常工作。

默认值: 继承性: 版本: JavaScript 语法:
0% 0%
no
CSS1
object.style.backgroundPosition="center"

2.可能的值

值 描述
  • top left
  • top center
  • top right
  • center left
  • center center
  • center right
  • bottom left
  • bottom center
  • bottom right
  • 如果您仅规定了一个关键词,那么第二个值将是"center"。

    默认值:0% 0%。

    x% y%

    第一个值是水平位置,第二个值是垂直位置。

    左上角是 0% 0%。右下角是 100% 100%。

    如果您仅规定了一个值,另一个值将是 50%。

    xpos ypos

    第一个值是水平位置,第二个值是垂直位置。

    左上角是 0 0。单位是像素 (0px 0px) 或任何其他的 CSS 单位。

    如果您仅规定了一个值,另一个值将是50%。

    您可以混合使用 % 和 position 值。

     

    3.实例操作:

    html+css代码:

    渲染效果:

    1.当background-position:-110px -70px;

    效果图:

     

     即是图片往上位移70px,往左位移110px;

    2.当background-position:10px 10px;

    效果图:

    即是往右位移10px,往下位移10px;

    3.background-position:100% 100%;

    效果图:

    图片处于容器元素的右下角,与 background-position:right bottom;效果等同。

    等同于x:{容器(container)的宽度?背景图片的宽度}*x百分比,超出的部分隐藏。即往右50px
    等同于y:{容器(container)的高度?背景图片的高度}*y百分比,超出的部分隐藏。即往下185px;

    4.background-position:50% 50%;

    效果图:

    图片水平和垂直居中。与 background-position:center center;效果等同。

    等同于x:{容器(container)的宽度?背景图片的宽度}*x百分比,超出的部分隐藏。
    等同于y:{容器(container)的高度?背景图片的高度}*y百分比,超出的部分隐藏。

    5.background-position:-50% -50%;

    效果图:

    等同于x:-{容器(container)的宽度?背景图片的宽度}*x百分比,超出的部分隐藏。即向左移动了25px;
    等同于y:-{容器(container)的高度?背景图片的高度}*y百分比,超出的部分隐藏。即向上移动了92.5px;

    6.background-position:-100% -100%;

    效果图:

     

    等同于x:-{容器(container)的宽度?背景图片的宽度}*x百分比,超出的部分隐藏。即向左移动了50px;
    等同于y:-{容器(container)的高度?背景图片的高度}*y百分比,超出的部分隐藏。即向上移动了185px;

    版权声明:本文为博主原创文章,未经博主允许不得转载。

     

     

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn