简体   繁体   English

Div背景图像最大尺寸

[英]Div background image max size

I have some problems with setting the max height of a background image in a div . div设置背景图像的最大高度时遇到一些问题。 The max height should be the height of the image and not larger than that. 最大高度应为图像的高度,且不大于该高度。 It's just zooming in on the image if there's to much content in the div . 如果div有很多内容,它只是放大图像。 When the background image ends I want it to continue with a background color for the rest of my content. 当背景图像结束时,我希望它继续使用我的其余内容的背景颜色。 Here is the code I've tried 这是我试过的代码

<style>
    .profilbg {
        background: url(<?php echo $uin->backgroundimg; ?>);
        background-repeat: no-repeat;
        background-size:cover;
    }
    .profilen {
        width: 100%;
        text-align: left;
        clear: both;
    }
</style>
<div class="profilen">
    <div class="profilbg">
        <?php echo nl2br($uin->profilen); ?>
    </div>
</div>

With this code the background image is just zooming in and getting bigger if there's to much content. 使用此代码,背景图像只是放大,如果内容很多,则会变大。

You can use this CSS rule for background image: 您可以将此CSS规则用于背景图像:

.profilbg {
  background: url(<?php echo $uin->backgroundimg; ?>) no-repeat #000 // Your bg color;
  background-size: 100%;
}

This will give your background image 100% width only, not including height and after image the background color will automatically appear. 这将使您的背景图像仅为100%宽度,不包括高度,在图像之后将自动显示背景颜色。

.profilbg {
background: url(<?php echo $uin->backgroundimg; ?>) no-repeat #000 ;
background-size:100%;

}

or 要么

.profilbg {
    background: url(<?php echo $uin->backgroundimg; ?>) no-repeat #000 ;
    max-height:100%;

    }

try it...... 试试吧......

Change your CSS rule to this, and it will make the image be full width, as high as its aspect ratio makes it, align at the top and fill the remaining space with red 将您的CSS规则更改为此,它将使图像为全宽,与其纵横比一样高,在顶部对齐并用红色填充剩余空间

.profilbg {
    background: #f00 url(<?php echo $uin->backgroundimg; ?>);
    background-repeat: no-repeat;
    background-size: 100% auto;
    background-position: center top;
}

Using background shorthand 使用背景速记

.profilbg {
    background: #f00
                url(<?php echo $uin->backgroundimg; ?>)
                no-repeat
                center top / 100% auto;
}

Sample 样品

 div { background: #f00 url(http://www.placehold.it/1000x150) no-repeat center top / 100% auto; /* temp for test */ height: 300px; } 
 <div></div> 

I know this alreay is solved but it would be easy to do whit background-size: 100%; 我知道这个问题已经解决,但很容易做到background-size: 100%; As you are using cover it will cover the Whole page but if you use 100% it will cover the page whit the background whitout geting zoomed inn 当您使用封面时,它将覆盖整页,但如果您使用100%,它将覆盖页面白色背景whitout geting缩放的旅馆

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

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