【问题标题】:Keep an Image Always Centered Regardless of Browser Size无论浏览器大小如何,始终保持图像居中
【发布时间】:2014-07-10 00:15:04
【问题描述】:

我想知道是否可以将img 保持在div 中,无论浏览器大小如何,都始终居中?居中是指图像的左/右侧被裁剪以确保图像保持居中而不修改高度。另外,当浏览器宽度小于图像宽度时,是否可以防止出现水平滚动条?

如果我的图像位于 CSS 中的 background-url 标记中,我确信这真的很容易做到,但由于此图像位于 SlidesJS 轮播中,因此图像必须来自 img 标记。

目前,只要浏览器宽度大于图像,我就使用margin:0 auto; 来保持图像居中。一旦浏览器宽度缩小,图像不会随着浏览器尺寸的缩小而调整。我还没有弄清楚当浏览器宽度太小时如何删除水平滚动条。

这就是我想要实现的目标:http://imgur.com/Nxh5n

这是页面布局的示例:http://imgur.com/r9tYx

我的代码示例:http://jsfiddle.net/9tRZG/

HTML:

<div id="wrapper">
    <div id="slides">
        <div class="slides_container">
            <div class="slide"> <!-- Carousel slide #1 -->
                <img src="http://www.placehold.it/200x50/">
            </div>
            <div class="slide"> <!-- Carousel slide #1 -->
                <img src="http://www.placehold.it/200x50/">
            </div>
            <div class="slide"> <!-- Carousel slide #1 -->
                <img src="http://www.placehold.it/200x50/">
            </div>
        </div>
    </div>
</div>​

CSS:

#wrapper {
    width: 200px;
    margin: 0 auto;
}​

【问题讨论】:

标签: html css


【解决方案1】:

试试这个:http://jsfiddle.net/9tRZG/1/

#wrapper {
    max-width: 200px; /* max-width doesn't behave correctly in legacy IE */
    margin: 0 auto;
}
#wrapper img{
    width:100%;       /* the image will now scale down as its parent gets smaller */
}
​

要裁剪边缘,您可以这样做:http://jsfiddle.net/9tRZG/2/

#wrapper {
    max-width: 600px; /* so this will scale down when the screen resizes */
    margin: 0 auto;
    overflow:hidden;  /* so that the children are cropped */
    border:solid 1px #000; /* you can remove this, I'm only using it to demonstrate the bounding box */
}

#wrapper .slides_container{
    width:600px;            /* static width here */
    position:relative;      /* so we can position it relative to its parent */
    left:50%;               /* centering the box */
    margin-left:-300px;     /* centering the box */
}
#wrapper img{
    display:block;          /* this allows us to use the centering margin trick */
    margin: 2px auto;       /* the top/bottom margin isn't necessary here, but the left/right is */
}

【讨论】:

  • 看起来不错,但我不想修改图像的高度。我只想根据需要裁剪边缘以保持图像居中。
  • 为了好玩,我制定了您想要的布局:jsfiddle.net/9tRZG/3
  • 我觉得应该提一下,上面带有循环图像的 jsfiddle 如果图像宽度不同,则不会使所有图像居中。
【解决方案2】:

Jeff Hines 链接了 putting image always in center page,ShankarSangoli 解释了如何实现这一目标。

img.centered {
     position:fixed;
     left: 50%;
     top:  50%;
     /*
         if, for instance, the image is 64x64 pixels,
         then "move" it half its width/height to the
         top/left by using negative margins
     */
     margin-left: -32px;
     margin-top:  -32px;
 }

【讨论】:

    【解决方案3】:

    我不确定对齐中心是否适合我的滚动条。 您可以使用以下内容:

    overflow-x: hidden;
    

    【讨论】:

    • 似乎没有用。你在哪里申请overflow-x:hidden;?
    • 你可以将它应用到body或者包含滚动条的div上。
    • 这可能是浏览器问题尝试以下操作: body { overflow: -moz-scrollbars-vertical;溢出-x:隐藏;溢出-y:滚动; } ___
    • 将overflow-x 应用于身体似乎已经成功了,但我只需要将它应用于图像。当我这样做时,它仍然不起作用。不管@Jeff Hines 已经链接了一个解决方案。谢谢你的帮助:)
    猜你喜欢
    • 2013-09-19
    • 2013-05-04
    • 2012-03-23
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    相关资源
    最近更新 更多