代码之家  ›  专栏  ›  技术社区  ›  anthonypliu

如果我有一个环绕着div的图像,如何使它只显示图像的某一部分?

  •  0
  • anthonypliu  · 技术社区  · 16 年前

    <div id=#container"><img src="hello.jpg" /></div>
    

    假设我希望图像只显示200px的宽度和200px的高度。我还想让它从顶部开始显示10个像素,从左侧开始显示30个像素。

    4 回复  |  直到 16 年前
        1
  •  1
  •   Pat    16 年前

    如果你的 <div id="#container"> 是绝对定位的,可以使用“剪辑”属性:

    #container {
        /* top, right, bottom and left points of the rectangle */
        clip: rect(10px, 230px, 210px, 30px);
    }
    

    你可以多看看 in this article .

        2
  •  5
  •   MisterPhilip    16 年前

        3
  •  1
  •   TheDeadMedic    16 年前
    #container {
        width: 200px;
        height: 200px;
        overflow: hidden
    }
    #container img {
        margin: -10px 0 0 -30px
    }
    
        4
  •  0
  •   Jeaf Gilbert    16 年前
    #container {
        padding-top  : 10px;
        padding-left : 30px;
    
    }
    #container img {
        width        : 200px;
        height       : 200px;
    }
    

    图像不会被拉伸:

    #container img {
        max-width    : 200px;
        max-height   : 200px;
    }