html - 我的完美背景图

标签 html css image image-scaling autosize

我目前有这样一个页面:

body {
    background-image: url("http://placehold.it/1920x1200");
    min-height: 100vh;
    min-width: 100vw;
    overflow: hidden;
}
<!DOCTYPE html>
    <html>
        <head>
            <title>Cool Background Man</title>
            <link rel="stylesheet" type="text/css" href="stylesheet.css">
        </head>
        <body>
        </body>
    </html>

但我需要 background-image 在保持缩放(不拉伸(stretch))的同时始终保持最小。这意味着始终 width: 100vwheight: 100vh,具体取决于屏幕尺寸。此外,始终图像将填满屏幕。它从不显示任何空白。图片还必须始终显示右上角,图片大小应相应调整。

总而言之,图像将始终:

  1. width: 100vwheight: 100vh
  2. 显示右上角,尺寸会随之调整。
  3. 填充屏幕任何屏幕尺寸。没有空格完全
  4. 切勿拉伸(stretch)。始终保持缩放比例。

最佳答案

Background size是你的 friend 。浏览器支持是 very good at 95% according to caniuse.com

正文{ 背景图像:url(“http://placehold.it/1920x1200”); 最小高度:100vh; 最小宽度:100vw; 溢出:隐藏;

body {
    background-image: url("http://placehold.it/1920x1200");
    min-height: 100vh;
    min-width: 100vw;
    overflow: hidden;
    background-size: cover;
    background-position: top right;
}
<!DOCTYPE html>
    <html>
        <head>
            <title>Cool Background Man</title>
            <link rel="stylesheet" type="text/css" href="stylesheet.css">
        </head>
        <body>
        </body>
    </html>

更新:一种允许您使用 CSS 过滤器的方法是将背景图像应用于伪内容并对其应用过滤器:

body {
  min-height: 100vh;
  min-width: 100vw;
  overflow: hidden;
  position: relative;
}
body:before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background-image: url("http://placehold.it/1920x1200");
  background-size: cover;
  background-position: top right;
  -webkit-filter: blur(5px); /* Or whatever filter you want */
  z-index: -1; /* Ensures it's behind the rest of the content */
}
<!DOCTYPE html>
<html>

<head>
  <title>Cool Background Man</title>
  <link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>

<body>
  Some content
</body>

</html>

关于html - 我的完美背景图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31595802/

相关文章:

css - 网站标题问题

javascript - CSS z-index 无法按预期使用 Angular 动画

页面上最后一次出现的类的 CSS 选择器

javascript - 使用 Jquery 和 servlet 检索图像不显示图像

java - 将整数数组编码为图像的策略

python - 如何在不使用磁盘存储的情况下在 python 中加载图像?

javascript - 如果值标签是某物,则添加 html

javascript - jquery ajax 中的动态内容加载

javascript - 如何更新写在 Canvas 上的文本

html - 在 Rails 部分和布局中找到 html 的定义位置的最简单方法是什么?