【问题标题】:center image inside overflow-hidden-parent溢出隐藏父项内的中心图像
【发布时间】:2014-09-04 00:51:02
【问题描述】:

我有一个跨度标记内的图像,跨度具有设置的宽度和高度,并设置为溢出隐藏。所以它只显示图像的一小部分。这可行,但可见图像的一小部分是左上角。我希望它成为可见图像的中心。我想我需要绝对定位图像,但图像的大小可能会有所不同。有谁知道如何做我想做的事?

谢谢!

这是 HTML:

<div class="lightbox_images">
                <h6>Alternate Views</h6>
                <span>
                    <a href="http://www.kranichs.com/mothers_rings/mothers_rings_txt2.jpg" rel="lightbox[product_alternate_views]" title="This is my captions 1">
                        <img src="http://www.kranichs.com/mothers_rings/mothers_rings_txt2.jpg" />
                    </a>
                </span>
                <span>
                    <a href="https://www.kranichs.com/product_images/Simon-G@346_M_346_M.jpg" rel="lightbox[product_alternate_views]" title="This is my captions 2">
                        <img src="https://www.kranichs.com/product_images/Simon-G@346_M_346_M.jpg" />
                    </a>
                </span>
                <span>
                    <a href="http://www.kranichs.com/images/simong/sim_banner_01.jpg" rel="lightbox[product_alternate_views]" title="This is my captions 3">
                        <img src="http://www.kranichs.com/images/simong/sim_banner_01.jpg" />
                    </a>
                </span>
                <span>
                    <a href="http://www.kranichs.com/images/psu/psu_banner.jpg" rel="lightbox[product_alternate_views]" title="This is my captions 4">
                        <img src="http://www.kranichs.com/images/psu/psu_banner.jpg" />
                    </a>
                </span>
            </div>

这是 CSS:

.lightbox_images{
    background-color:#F9F9F9;
    border:1px solid #F0F0F0;
}
.lightbox_images h6{
    font-family:Verdana, Arial, Helvetica, sans-serif;
    color:#333333;
    font-size:14px;
    font-weight:bold;
    font-style:italic;
    text-decoration:none;
    margin:0px;
}
.lightbox_images span{
    padding:5px;
    padding-bottom:15px;
    background-color:#DFDFDF;
    margin:5px;
    display:inline-block;
    border:1px solid #CCC;
}
.lightbox_images a{
    display:inline-block;
    width:60px;
    height:60px;
    overflow:hidden;
    position:relative;
}

.lightbox_images a img{
    position:absolute;
    left:-50%;
    top:-50%;
}

.lightbox_images span:hover{
    border:1px solid #BBB;
    background-color:#CFCFCF;
}

【问题讨论】:

    标签: css


    【解决方案1】:

    正如 Billy Moat 在https://stackoverflow.com/a/14837947/2227298 中提出的,有一个解决方案不知道图像的高度和宽度。

    在这里试试:http://jsfiddle.net/LSKRy/

    <div class="outer">
        <div class="inner">
        <img src="http://3.bp.blogspot.com/-zvTnqSbUAk8/Tm49IrDAVCI/AAAAAAAACv8/05Ood5LcjkE/s1600/Ferrari-458-Italia-Nighthawk-6.jpg" alt="" />
        </div>
    </div>
    
    .outer {
        width: 300px;
        overflow: hidden;
    }
    
    .inner {
        display: inline-block;
        position: relative;
        right: -50%;
    }
    
    img {
        position: relative;
        left: -50%;
    }
    

    【讨论】:

    • 这是最好的解决方案,因为动态宽度/高度和相对定位。
    • 我喜欢这个解决方案,但是在.inner 上使用display: inline-block 会导致在.outer 有背景时看到一条空白。在.inner 和img 上使用display: table 来避免这个问题。 (注意:.inner 上的display: block 会破坏效果,但如果您愿意,可以在img 上使用display: block。)。这是一个显示与您的比较的小提琴:jsfiddle.net/gfullam/fsowennr
    • 还有其他人对这些负左移和右移感到困惑吗?
    【解决方案2】:

    鉴于这种 HTML:

    <span><img src="..." width="..." height="..." alt="..." /></span>
    

    你可以像这样使用 CSS:

    span {
      position: relative;
      display: block;
      width: 50px;  /* Change this */
      height: 50px; /* Change this */
      overflow: hidden;
      border: 1px solid #000;
    }
    span img {
      position: absolute;
      left: -10px; /* Change this */
      top: -10px;  /* Change this */
    }
    

    然后您可以根据图像的确切尺寸将图像居中。

    或者,如果您能够修改 HTML,则可以改为使用以下内容:

    <div>
      <a href="...">[name of picture]</a>
    </div>
    

    然后,用这个 CSS 匹配它:

    div {
      width: 50px;
      height: 50px;
      background: transparent url(...) center center no-repeat;
      border: 1px solid #000;
    }
    div a {
      display: block;
      height: 100%;
      text-indent: -9999em; /* Hides the link text */
    }
    

    在这种情况下,无论其尺寸如何,背景都会自动居中,并且仍然可以点击。

    【讨论】:

    • 谢谢,但是图片大小可以。我尝试使用 -50% 但这只是使其成为容器宽度的 -50%,而不是图像宽度。
    • 我添加了一种替代方法,可以处理各种尺寸的图像,但它只有在您可以控制 HTML 时才有用。
    • 谢谢,把图片设置为背景中心,成功了!
    • 我注意到现在在 ie6 中没有显示。
    【解决方案3】:

    本例中,图片位于元素的中心,无论其大小如何

    HTML:

    <div class="box">
        <img src="example.jpg">
    </div>
    

    CSS:

    div.box{
        width: 100px;
        height: 100px
        overflow: hidden;
        text-align: center;
    }
    
    div.box > img{
        left: 50%;
        margin-left: -100%;
        position: relative;
        width: auto !important;
        height: 100px !important;
    }
    

    【讨论】:

      【解决方案4】:

      如果图像的宽度和高度不同,我认为唯一的方法是使用 javascript。

      将图像设置为向左:50%;最高:50%;然后,使用 javascript(可能是图像加载事件)添加 margin-left:-imageWidth/2 px; margin-top:-imageHeight/2 px;

      所以基本上你有

      span img {
          position: absolute;
          left: 50%;
          top: 50%;
      }
      

      还有下面的js

      window.onload = function() {
      
        var images = document.getElementsByTagName('img');
      
        for(i=0; i<images.length; i++)
           images[i].onload = centerImage(images[i]);
      
      
        function centerImage(img) {
          img.style.marginLeft = -(img.width/2) + "px";
          img.style.marginTop = -(img.height/2) + "px";
        }
      
      }
      

      PS。如果您使用的是 javascript 框架/库,则代码可以简化一点,但我没有做出这样的假设。

      【讨论】:

      • +1 表示“图像的宽度和高度各不相同,我认为唯一的方法是使用 javascript。”
      【解决方案5】:

      您可以将图像设置为元素的背景,并设置 x,y 轴,如下例所示:

      #mySpan {
        background-image: url(myimage.png);
        background-repeat: no-repeat;
        background-attachment:fixed;
        background-position: -10 -10
      }
      

      【讨论】:

        猜你喜欢
        • 2012-06-05
        • 2017-12-16
        • 2015-11-10
        • 1970-01-01
        • 1970-01-01
        • 2022-11-12
        • 2013-10-12
        • 2019-11-23
        • 2021-05-20
        相关资源
        最近更新 更多