html - 缩放 SVG 背景以填充屏幕,没有白色边框,保持纵横比

标签 html css svg

我使用的是 SVG 背景图像,上面有 SVG 和 HTML 层。我可以在屏幕调整大小时进行 SVG 缩放,但有时图像的两侧会出现白色区域 - 例如高度短、宽度短。

有没有办法缩放图像,保留纵横比,并强制图像适应宽度或高度,具体取决于哪个大于当前的宽度/高度。例如,在屏幕高度较短且宽度较宽的情况下,人们将看不到图像的底部,但图像将填充整个宽度。

最好使用 CSS,普通 JS 也可以。

当前 HTML:

<html><body>
    <div class="container">
        <img class="svg_container" src="image.svg" id="svg_bg">
        <svg class="svg_container" id="svg_area"></svg>
        <div id='html_overlay'>
            <div id="heading_area">
                <h3>Some text</h3>
            </div></div></div></body></html>

CSS:

.svg_container {
    position:fixed;
    top:0;
    left:0;
    width:100%;
    height:100%;
    overflow:hidden
}
#svg_bg {
    overflow:hidden;
    z-index: -1;
}
#svg_area {
    z-index: 1;
}
#html_overlay{
    position:fixed;
    top:0;
    left:0;
    width:100%;
    height:100%;
    z-index: 100;
}
body {
    font-family: sans-serif;
}

最佳答案

我相信这可能就是您正在寻找的:

body {
    background: url(test.svg) no-repeat;
    background-size: 100vw auto;
}

@media (orientation: portrait) {
    body {
        background-size: auto 100vh;
    }
}

默认情况下,图像宽度将填充视口(viewport)宽度 (100vw) 的 100%,但如果页面的高度大于宽度(或横向),则背景大小将被覆盖,以便图像高度填充视口(viewport)高度的 100% (100vh)。

关于html - 缩放 SVG 背景以填充屏幕,没有白色边框,保持纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35248758/

相关文章:

iphone - 编辑 asp header

javascript - 复选框未选中,因为复选框在 html 中工作

html - 顶部和底部相似的线性渐变

html - 虽然将填充放在选择框中并使用 font-family segoe 它仅在 chrome 中剪切文本

svg - Inkscape 生成的 flow* 元素有哪些浏览器友好的替代方案?

javascript - 更改 JSON 时无法读取未定义的属性 'appendChild'

android - 进行数据连接时,在移动Chrome上检测HTML音频的Canplaythrough

css - 将 SVG 蒙版切割成图像

javascript - Jquery - 如何在 html 元素之间来回转换 (fadeIn/fadeOut) 并保持位置?

javascript - 如何使用 React Hooks 处理 React Svg 拖放