html - 背景图像在较小的屏幕尺寸上不缩放

标签 html css twitter-bootstrap responsive-design background

我对这个前端的东西很陌生。我有一张图像 1200 * 900 px,我将其用作横幅的背景。图像在大屏幕上看起来不错,但当我从响应式设计模式切换到 Apple iPhone 6S 时。背景从侧面被切断。如何在较小尺寸的情况下保留完整图像。

index.html

<div class="container-fluid banner">
       <div class="row">
            <div class="col-md-8 content">
               <h1>TThis is a banner image.</h1>
               <p>Banner image is the main image we use on our web pages showing what your page is all about.</p>
             </div><!--/.col-->
        </div>
</div>

样式.css

.banner {
    background: url(banner.jpg) no-repeat center center fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    height: 680px;
    position: relative; 
}

我尝试过的一些解决方案是使用媒体查询在较小的屏幕尺寸上设置 max-width: 100%

@media (max-width: 768px) {
    .banner{ max-width: 100%; }
}

但是这个解决方案行不通。我还有哪些其他选项可以根据屏幕尺寸缩放此背景?

最佳答案

如果你想显示完整的图片,你应该使用contain而不是cover

如果您想为更大的屏幕保留 cover,您可以将 cover 更改为仅在查询中包含 contain

@media (max-width: 768px) { 
    -webkit-background-size: contain;
     -moz-background-size: contain;
     -o-background-size: contain;
     background-size: contain;

 }

关于html - 背景图像在较小的屏幕尺寸上不缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50598348/

相关文章:

javascript - jQuery - 用于移动设备的 mousemove

javascript - 使元素在可滚动的 div 中居中

html - 拉伸(stretch)水平 div(中间 div 拉伸(stretch))

css - 如何在 Twitter Bootstrap 中将卡住的输入框和提交按钮彼此分开?

css - 如何使用 Bootstrap 4 将列表居中?

css - Bootstrap col-sm-offset 未应用于小型设备

java - XML-FO 嵌套列表 block

html - 将元素符号点添加到 Accordion HTML

javascript - 将 html id 的输出返回到 php 字符串

javascript - 多行轮播居中