简体   繁体   English

如何将100%的图像适合Jumbotron

[英]How to fit 100% of an image to a Jumbotron

I have the following HTML: 我有以下HTML:

<div class="jumbotron">
    <div class="container">
        <h1>Souplesse</h1>
        <p>Be a Good Sport</p>
    </div>
</div>

And the following CSS: 以下CSS:

.jumbotron {
  background-image:url('piscine.jpg');
  height:300px;
  background-repeat: no-repeat;
  background-size: cover;
  border-bottom:1px solid #ff6a00
}
.jumbotron .container {
  position:relative;
  top:50px;
}

My image is far too large to fit my height of 300px but I would like it to auto-resize and fit the height, so I can see all of my image. 我的图像太大了,不适合我的300px的高度,但我希望它能自动调整大小并适合高度,所以我可以看到我的所有图像。 This must be simple but I can't see what changes need to be made. 这一定很简单,但我看不出需要做出哪些改变。

Thanks 谢谢

If you would like the background image to fit the height of your jumbotron, but don't care about if it stretches to the entire width: 如果您希望背景图像适合您的jumbotron的高度,但不关心它是否延伸到整个宽度:

.jumbotron { background-size: auto 100%; }

If you want the background image to cover the entire height AND width of the jumbotron and you do not care about the image's aspect ratio: 如果您希望背景图像覆盖超大屏幕的整个高度和宽度,并且您关心图像的宽高比:

.jumbotron {background-size: 100% 100%; }

If you want the background image to be cover the entire height AND width the jumbotron but you DO care about the image's aspect ratio: 如果您希望背景图像是覆盖整个高度和宽度的超大屏幕,但在乎图像的长宽比:

.jumbotron { background-size: cover; }

That last option will have some of the image cut off if the jumbotron has a different aspect ratio than the image, but you can specify how the image is positioned with the background position property: 如果jumbotron具有与图像不同的纵横比,那么最后一个选项将会截断一些图像,但您可以使用背景位置属性指定图像的定位方式:

.jumbotron {
    /* Image in the center of container */
    background-position: center center;

    /*  OR Image in the center top of the container */
    background-position: center top;

    /*  OR Image in the center bottom of the container  */
    background-position: center bottom;
}

Try: 尝试:

.jumbotron {
  background-image:url('piscine.jpg');
  height:300px;
  background-repeat: no-repeat;
  background-size: auto 100%;
  border-bottom:1px solid #ff6a00
}

Note the: 注意:

background-size: auto 100%;

The first value is the width, the second value is the height. 第一个值是宽度,第二个值是高度。 It is compliant to use px or % for those values. 符合使用px或%来表示这些值。

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

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