【问题标题】:background-size: contain too much whitespace when changing browser size背景大小:更改浏览器大小时包含太多空格
【发布时间】:2016-01-15 12:30:13
【问题描述】:

如果我使用background-size:contain;,当浏览器大小发生变化时如何消除空格?

对于较小的浏览器尺寸,图像和文本之间的空白太多了。网址是:http://16debut.com/test.html

CSS 是:

body { 
    margin:0px 0px; 
}

#hero {
    background-clip: content-box;
    background-image: url("imgtop.png");
    background-size: contain;
    background-repeat: no-repeat; 
    position: relative;
    height: 235vh;
}

#content {
    padding: 100px 50px;
    text-align: center;
    width: 80%;
    margin: 0px auto;
}

#content h2 {
    margin: 0px 0px 30px 0px;
}

#footer {
    padding: 30px 0px;
    text-align: center;
    background: #ddd;
}

【问题讨论】:

  • 我认为封面是响应式的,但是有没有办法在没有截止的情况下仍然拥有云?由于这个原因,我尝试了包含,但封面确实解决了空白问题。除了覆盖或包含之外,是否还有其他方法可以发挥作用?
  • 或者我可以用 contains 做些什么来阻止空格问题,例如 cover 如何阻止它发生?

标签: html css whitespace contain background-size


【解决方案1】:

jsbin demo

您想完全响应但保持底部的白云?

为同一个元素使用两张背景图片。

  • 剪下白底云,另存为单独的 .png 图像。用作第一张背景图片。
  • 可选)再次保存您的大图,只是没有白云。将该图像用作第二个背景图像值。

现在在 CSS 中:

  • 将云设置为 background-position: bottom 和 100% 大小(“宽度”)
  • 将较大的图像设置为中心(50%)位置和cover

CSS

html, body{height:100%; margin:0;}

#hero{
  position:relative;
  height:130vh; /* USE THE RIGHT RATIO since the image Logo is a bit up*/
  background: no-repeat 
    url(http://i.stack.imgur.com/eWFn6.png) bottom / 100%, /* BOTTOM CLOUDS OVERLAY */
    url(http://i.stack.imgur.com/IVgpV.png) 50% / cover;   /* BIG IMAGE */
}

HTML

<div id="hero"></div>

<div class="other">
  <h1>Other Divs</h1>
  <p>bla bla</p>
</div>

似乎 Safari 是一个非常愚蠢的浏览器(他们甚至从 2012 年起就取消了对 Windows 的支持......太棒了)。这是jsBin example 和css:

#hero{
  position:relative;
  height: 900px;
  height: 100vh; 
  background: url(http://i.stack.imgur.com/eWFn6.png) no-repeat bottom, url(http://i.stack.imgur.com/IVgpV.png) 50%;
  background-size: 100%, cover;
}

【讨论】:

  • cover 它没有响应。当您更改浏览器大小时,背景大小保持不变
  • @Chris 你 think cover 没有响应或者你知道它没有响应
  • cover 会尽量适应空间,contain 会尽量适应宽度和高度的空间。 w3schools.com/cssref/css3_pr_background-size.asp在 OP 链接上试试看,封面并不能解决问题
  • @Chris 我看到答案可能不太适合 OP 的需求——因为他可能希望所有的图像云都可见——但是,你知道 w3schools 只会对编程造成伤害吗社区?你知道他们卖假文凭,而且内容不是由编程社区维护和编辑的吗?有比 w3schools 更好的文档资源。此外,cover 完全用于响应性目的 - 所以是的,它是响应性的。很多。 stackoverflow.com/questions/25799055/…
  • 是的,cover 确实是响应式的,但不是解决这个问题的最佳解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-25
  • 2013-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-19
相关资源
最近更新 更多