【问题标题】:1/3 screen, full screen background with responsive scaling1/3 屏幕,具有响应缩放的全屏背景
【发布时间】:2015-02-15 15:57:17
【问题描述】:

我正在让我的主页有一个大图像(现在只是一个静态图像,可能是幻灯片或以后的东西),我将高度设置为 60%,宽度设置为 100%。在css中设置为背景图片(单独的样式表)

这是一个详细的图像,占据了整个宽度,占据了整个空间。

我希望图像在屏幕调整大小时缩放并填充 60% 最大高度 100% 最小宽度的整个高度/宽度(响应式,移动时 40% 高度)。我不想在调整大小时进行任何裁剪。 (最坏的情况,只裁剪顶部,重点是侧面保持未裁剪)

我尝试了 background-size:cover - 当纵向和其他奇怪的尺寸(尤其是移动和纵向)时它会裁剪很多

我尝试了 background-size: contains - 调整大小时没有正确填充空间。

基本上所有的“全屏背景”教程都不起作用,因为它最多只有大约 1/2 屏幕。

  • 跨浏览器支持也很重要

希望我解释得很好,非常感谢任何帮助

  • 干杯

【问题讨论】:

  • 你的 CSS/HTML 是什么?请向我们展示您到目前为止所做的尝试。
  • 当前 css: background: url(../images/temp_header.jpg) center bottom no-repeat;高度:60%;最小宽度:100%; -moz-background-size:封面;背景尺寸:封面;
  • 你能提供什么小提琴吗?
  • 从来没有用过,我更像是一个设计师而不是一个编码器。基本上我只是想保持图像的侧面可见,并且在水平缩放时保持在屏幕的侧面。 'background-size: cover' 如果在缩放时不裁剪侧面,效果会很好

标签: html css image media-queries


【解决方案1】:

您可以将媒体查询用于较小的屏幕。因为您想通过高度限制背景并保持纵横比,所以您必须通过将背景应用于伪元素来使用 hack。

小提琴示例:http://jsfiddle.net/abhitalks/nxydcu70/2/

示例片段:(查看全屏并调整大小以查看)

html, body { 
    box-sizing: border-box; margin:0; padding:0; 
    width: 100%; height: 100%; overflow: hidden; 
}
body { color: #fff; text-shadow: 0px 0px 8px #333; }
body::before {
    content: ''; transition: all 500ms; 
    position: absolute; left: 0; right: 0; height: 70%;
    z-index: -10;
    background-origin: border-box;
    background: url("http://lorempixel.com/1000/600") bottom center / cover no-repeat transparent; 
}

@media (max-width: 767px) {
    body::before {
        content: ''; transition: all 500ms; 
        position: absolute; left: 0; right: 0; height: 40%;
        z-index: -10;
        background-origin: border-box;
        background: url("http://lorempixel.com/1000/600") center center / cover no-repeat transparent; 
    }
}
<h1>Responsive background limited by height</h1>

更新:

如果您只想在标题而不是正文上使用它,则无需使用伪元素。只需将标题高度设置为 60%。

更新的小提琴:http://jsfiddle.net/abhitalks/pmtr5707/2/

更新片段

* { box-sizing: border-box; margin:0; padding:0; }
html, body { 
    width: 100%; height: 100%; overflow: hidden; 
}
header {
    width: 100%; height: 60%;
    background-origin: border-box;
    background: url("http://lorempixel.com/1000/600") bottom center / cover no-repeat transparent; 
    transition: all 500ms; 
}
header > h1 { color: #fff; text-shadow: 0px 0px 8px #333; }
header, section, footer { padding: 15px; }
footer { border-top: 1px solid #eee; }

@media (max-width: 767px) {
    header {
        width: 100%; height: 40%;
        background-origin: border-box;
        background: url("http://lorempixel.com/1000/600") center center / cover no-repeat transparent; 
        transition: all 500ms; 
    }
}
<header><h1>Header</h1></header>
<section><p>Content</p></section>
<footer><h2>Footer</h2></footer>

【讨论】:

  • 这样更接近,并且很好地解决了宽度问题。是否可以不裁剪底部和顶部(如果必须裁剪顶部)。而且一定要使用body标签/html标签吗?
  • 只需将位置设为bottom center,使其底部对齐并在顶部裁剪,如下所示:jsfiddle.net/abhitalks/nxydcu70/2 必须将背景应用于伪元素 of 你想要背景的元素。因此,如果您想要 body 的背景,请将其应用于 body::before。如果您想在div 上使用,请在div::before 上应用它
  • 然后将它应用到那个div
  • 我要不要离开剩下的身体,只改变'div::before'
  • 是的。你可以这样做。
猜你喜欢
  • 2021-07-29
  • 1970-01-01
  • 2011-05-25
  • 2021-04-04
  • 2014-05-09
  • 2014-01-18
  • 1970-01-01
  • 2018-05-10
相关资源
最近更新 更多