【问题标题】:Div background larger than window height wont scale大于窗口高度的 Div 背景不会缩放
【发布时间】:2013-08-02 20:26:34
【问题描述】:

我有一张图片将用作一系列“幻灯片”的背景,这些幻灯片都包含在一个页面中。总共有四张幻灯片。每张幻灯片都是屏幕的高度。所以我需要背景是 4x 屏幕高度。我还希望图像随屏幕宽度缩放。图片非常高,每张幻灯片上背景的哪一部分都无关紧要,因此保持纵横比应该不是问题。

我遇到的问题是,当我制作一个宽度小于图像宽度的窗口时,图像的一部分会被截断而不是缩放到屏幕宽度。到目前为止我的 CSS:

#container {
    position:relative;
    height:100%;
}
#background {
    position:absolute;
    width: 100%;
    height:400%;
    background:url(photo.png);
}
.slide {
    width: 100%;
    height:100%;
    position:relative;
}

还有 HTML:

<div id="container">
    <div id="background"></div>

    <div class="slide">
        Content 1
    </div>
    <div class="slide">
        Content 2
    </div>
    <div class="slide">
        Content 3 
    </div>
    <div class="slide">
        Content 4
    </div>
</div>

注意,必须兼容IE8及以上(即CSS3的东西)

【问题讨论】:

  • @koala_dev 我想这会很好,只是旧的浏览器需要一个 shim

标签: html css background


【解决方案1】:

也许你可以使用类似的东西:

<div id="background"> <img src="img.jpg" class="stretch" alt="" /> </div>

而不是在css中

#background {
    width: 100%; 
    height: 100%; 
    position: absolute; 
    left: 0px; 
    top: 0px; 
    z-index: 0;
}

.stretch {
    width:100%;
    height:100%;
}

我不知道你为什么不想使用 css3,也许你有一些好的目的,但我仍然建议使用它。

css3代码是:

background-size: 100%;

【讨论】:

  • +1 那将是最好的方法,将 标签延伸到容器
  • @Tomasz Grochowski 编辑了那个位,只需要 IE8+ 兼容性。
  • &lt;img&gt; 标签对我有用,必须从.strech 中删除height:100%;,一切都很好。谢谢。
【解决方案2】:

可以使用css3属性Background-size

#background {
    background-size: 100%;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 2015-10-27
    • 2011-07-01
    • 2017-04-15
    • 2020-03-01
    相关资源
    最近更新 更多