简体   繁体   English

引导程序:屏幕调整大小后,巨型机类中的背景图像无响应

[英]Bootstrap: background image unresponsive in jumbotron class upon screen resizing

I am using bootstrap for the first time in a tribute page and I am having trouble with the responsiveness of the background image in the jumbotron class. 我是在致敬页面中第一次使用引导程序,而在jumbotron类中背景图像的响应能力上却遇到了麻烦。 When I resize the screen, only half of the image shows and you have to scroll horizontally to see the whole image. 当我调整屏幕大小时,仅显示一半的图像,您必须水平滚动才能看到整个图像。 Is there a way to make it automatically adjust and fit entirely within the window. 有没有一种方法可以使其自动调整并完全适合窗口。

I've tried using the answers found in this thread (where the image tiles and repeats): Responsive Bootstrap Jumbotron Background Image but they did not work out since the problem is not the same. 我尝试使用在此线程中找到的答案(图像在其中平铺并重复): 响应式自举巨型背景图像,但由于问题不同,因此无法解决。

HTML code: HTML代码:

<div id="jumbotron1" class="jumbotron">
   <!-- title text -->
   <div id="title-container" class="container rounded">
       <h1 id="title-text" class="text-center">Hattie McDaniel</h1>
   </div>
</div>

CSS code: CSS代码:

#jumbotron1{
    background-image: 
url('//cdn3.thr.com/sites/default/files/2015/02/hattie.png'); 
   background-size: cover; 
   height: 580px;
   padding: 0px;
   margin-bottom: 0px;
   background-repeat: no-repeat;
}

I know that if I am not using the jumbotron class, I can easily add an image element with the class "img-fluid" and that's it. 我知道,如果我不使用jumbotron类,则可以轻松地使用“ img-fluid”类添加图像元素,仅此而已。 How can I do that in this case? 在这种情况下,我该怎么办?

Thanks in advance! 提前致谢!

You can try setting the background position to center, like this: 您可以尝试将背景位置设置为居中,如下所示:

#jumbotron1{
background-position:center center;
}

Hi please add below styles to your style sheets. 嗨,请在您的样式表中添加以下样式。

    #jumbotron1{
    background-image: 
    url('//cdn3.thr.com/sites/default/files/2015/02/hattie.png'); 
    background-size: cover; 
    height: 580px;
    padding: 0px;
    margin-bottom: 0px;
    background-repeat: no-repeat;
  }

  @media (max-width:980px) { 
    #jumbotron1{background-image: 
    url('//cdn3.thr.com/sites/default/files/2015/02/hattie.png'); 
    background-size: 100%;
    height: 360px;
    }
  }


  @media (max-width:768px) { 
    #jumbotron1{background-image: 
    url('//cdn3.thr.com/sites/default/files/2015/02/hattie.png'); 
    background-size: 100%;
    }
  }

  @media (max-width:550px) { 
    #jumbotron1{background-image: 
    url('//cdn3.thr.com/sites/default/files/2015/02/hattie.png'); 
    height: 180px;
    }
  }

We are using media query here 我们在这里使用媒体查询

this will resolve your issue 这将解决您的问题

#jumbotron {
background-image: 
  url('//cdn3.thr.com/sites/default/files/2015/02/hattie.png'); 
background-size: cover;
height: 100%;}

more on https://stackoverflow.com/a/31147923/7714663 有关更多信息, 请访问https://stackoverflow.com/a/31147923/7714663

One way to make the entire background image fit would be to use background-size: 100% 100%; 使整个背景图像适合的一种方法是使用background-size: 100% 100%; This makes it so that the background-image dimensions will always fit. 这样可以使背景图像尺寸始终适合。 It may look kind of weird as the screen size gets smaller 随着屏幕尺寸变小,这看起来可能很奇怪

Add the piece of code like so: 添加如下代码:

#jumbotron1{
  background-image: 
  url('//cdn3.thr.com/sites/default/files/2015/02/hattie.png'); 
  background-size: cover; 
  height: 580px;
  padding: 0px;
  margin-bottom: 0px;
  background-repeat: no-repeat;
  background-size: 100% 100%;
}

It may be better to just change the background image at certain break points with media queries though because the background-image looks terrible on smaller screen sizes 最好通过媒体查询在特定的断点处更改背景图像,因为在较小的屏幕尺寸下背景图像看起来很糟糕

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM