html - 当页面太短时,div 中的背景图像在顶部和底部被截断

标签 html css background scale

我是 CSS 的 super 新手,所以这可能真的很简单。我已经搜索了一堆这个问题的答案,但我还没有找到它。我想要 div,包含根据页面大小拉伸(stretch)和缩放的 Logo ,我几乎可以正常工作,但如果页面太短,背景图像的顶部和底部就会被切断。这是该站点,因此您可以看到发生了什么。

chaptertwollc.com

这是相关的 CSS。

.centerLogo {
    height: 50%;
    width: 50%;
    position: absolute;
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px;
    text-align: center;
    margin-top: auto;
    margin-right: auto;
    margin-bottom: auto;
    margin-left: auto;
    visibility: visible;
    background-repeat: no-repeat;
    background-image: url(../POP%20high%20res%20logo%20ellipse%20black%20glow%20webop.png);
    background-size: 100%;
    background-position: center center;
    background-attachment: scroll;
}

编辑:我让它工作了。就像改变一样简单

background-size: 100%;

background-size: contain;

最佳答案

跨浏览器解决方案是这样的 Svoka ​​使用了错误的 CSS 值...

-webkit-background-size: contain;
-moz-background-size: contain;
-o-background-size: contain;
background-size: contain;

Contain 告诉渲染引擎确保背景适合容器而不改变宽高比。

关于html - 当页面太短时,div 中的背景图像在顶部和底部被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24360542/

相关文章:

javascript - 仅使用 jquery 或 javascript 更改图像

javascript - 我什么时候应该将 Javascript 保留在 html 文件上?

css - Sass 和四舍五入的数字。这个可以配置吗?

ios - SpriteKit 按钮

asp.net - CSS 和 ASP.NET 控件

android - 在android的位图上将白色转换为透明

javascript - JQueryUI 可排序,无占位符位移

css - 对齐复选框和旁边的文本

html - 防止在 div 上滚动

jquery - 在 jQuery 中添加带有前缀的 flex 样式