【问题标题】:Placing an image in the middle of a div AND resize it when the window gets too small将图像放在 div 的中间并在窗口变得太小时时调整其大小
【发布时间】:2016-11-11 11:32:35
【问题描述】:

我有一张图片需要放在一个最大宽度为 700px 的 div 中间;

当窗口变窄并且 div 变得小于它包含的图像的宽度时,我希望图像也变小。

在下面的小提琴中,我分别添加了这两个函数:https://jsfiddle.net/rb1hyxh2/,但我找不到将它们组合起来的方法(例如,margin: 0 auto; 不适用于此处的内联块...)

<div class="header1"><div class="header2">
    <img src="http://wp.patheos.com.s3.amazonaws.com/blogs/faithwalkers/files/2013/03/bigstock-Test-word-on-white-keyboard-27134336.jpg" class="header3">
</div></div>

<div class="header1">
    <img src="http://wp.patheos.com.s3.amazonaws.com/blogs/faithwalkers/files/2013/03/bigstock-Test-word-on-white-keyboard-27134336.jpg" class="headerA">
</div>


.header1{
    padding-bottom: 16px;
    background-color: red;  
    max-width: 700px;
    margin-bottom: 30px;
}

.header2{
    background-color: blue;
    display:inline-block;
}
.header3{
    width: 100%;
}

.headerA{
    display:block;
    margin:auto;
}

【问题讨论】:

    标签: html css image center


    【解决方案1】:

    一个简单的解决方案是将 headerA 上的 ma​​x-width 设置为图像的宽度,并将其 width 设置为 100%。这样当 div 小于 500px 时它会填满 div 的大小。

    Codepen

    HTML

    <div class="header1">
      <img src="http://wp.patheos.com.s3.amazonaws.com/blogs/faithwalkers/files/2013/03/bigstock-Test-word-on-white-keyboard-27134336.jpg" class="headerA" />
    </div>
    

    CSS

    .header1 {
      padding-bottom: 16px;
      background-color: red;
      max-width: 700px;
      margin-bottom: 30px;
    }
    
    .headerA {
      display: block;
      margin: auto;
      max-width: 500px;
      width: 100%;
    }
    

    CSS

    中添加了行
      max-width: 500px;
      width: 100%;
    

    【讨论】:

    • 感谢您的快速答复。问题是我将此代码用于多个图像,所有图像的宽度都不同..
    【解决方案2】:

    如果你添加

    img {
      width: 100%; /* or any other value < 100% will stay within bounds */
    }
    

    到您的 css 文件,然后您的图像将保持在您拥有的红色 div 的范围内。这是你想要的吗?

    【讨论】:

    • 在这种情况下,图像也会变得比原来更大,不幸的是这不是我想要的。
    【解决方案3】:

    这是将所有具有.headerA 类的图像设置为其原始宽度的最大宽度的 javscript 版本,请参阅小提琴https://jsfiddle.net/rb1hyxh2/5/

    <script>
    var imgs = document.querySelectorAll('.headerA');
    for (var i = 0, element; element = imgs[i]; i++) {
            var w = imgs[i].naturalWidth;
        imgs[i].style.maxWidth = w+"px";
    }
    </script>
    

    我想这就是你要找的https://jsfiddle.net/rb1hyxh2/3/

    我在图像中添加了width: 100%;max-width: 500px;,这会将img 设置为div 的100%,但当div 大于500px 时不会;

    .headerA {
      display: block;
      margin: auto;
      width: 100%;
      max-width: 500px;
    }
    

    【讨论】:

    • 感谢您的快速答复。问题是我将此代码用于多个图像,所有图像的宽度都不同..
    • 是的,我自己已经找过了,但没找到。
    • @Jip 在我的回答中看看我的新小提琴,这应该为你做。
    • @Adam 非常感谢,这正是我想要的。但是由于某种原因,它在我的网站上不起作用。 javascript函数不会影响图像(图像获取它所在的div的max-width = 700px)。我不明白这是怎么可能的,因为 jsfiddle 工作得很好。你有什么主意吗?我试图解决它 1 小时,但似乎没有任何效果..
    • @Jip 我会检查你所有的 css 以确保没有任何东西覆盖它,还要确保你有一个指向 JQuery 的链接
    猜你喜欢
    • 2012-09-23
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    相关资源
    最近更新 更多