简体   繁体   English

如何使背景图像尺寸的高度始终等于全角图像所需的图像高度?

[英]How to make background-image size height always equal what the image height needs to be for full width image?

How do I make an image always have the same crop height. 如何使图像始终具有相同的裁切高度。 As in on this site http://deliciousproductions.com.au/ the div is 420px high. 就像本网站http://deliciousproductions.com.au/中的一样,div高420px。 But the image extends further. 但是图像进一步延伸。

Here's the div 这是div

<div class="dpsplash">
</div>

css 的CSS

.dpsplash {
    background-image: url('/img/banner1.png');  
    background-size: auto 800px;
    height: 400px;
    background-position: center top;
    background-repeat: no-repeat;
}

So what this does is make the image height so that when you resize the page horizontally it doesn't shift the point where you crop the image for the div height, but instead extends the width. 因此,这样做是为了提高图像高度,以便在水平调整页面大小时,不会将裁剪图像的点移动到div高度,而是会扩展宽度。 Buuuut we run into a problem because I have to guess the image height for a full width drawing on bigger resolutions where the image won't be shorter than 400px if displayed at full width, because if a 2500x1200 image is displayed at full width on a phone, it's gonna end before the div does, looking ugly. Buuuut我们遇到了一个问题,因为我必须猜测在较大分辨率下全宽绘图的图像高度,如果以全宽显示,图像的长度不会小于400px,因为如果在全宽显示2500x1200图像时,电话,它会在div之前结束,看起来很难看。

I feel like in maths/javascript it should be that [(current page width)/(image width)] * (image height) = (the height it needs to be) or x, so the background-size style should be background-size: auto (that formula from above?); 我觉得在math / javascript中应该是[(当前页面宽度)/(图像宽度)] *(图像高度)=(需要的高度)或x,因此背景尺寸样式应为background-size: auto (that formula from above?);

I know basically no Javascript, it's so confusing but I understand logic arguments. 我基本上不知道Java语言,它是如此混乱,但是我理解逻辑参数。

Do you mean background-size: cover; 你是说background-size: cover;吗? ? What it does ( source ): 它的作用( 来源 ):

Scale the background image to be as large as possible so that the background area is completely covered by the background image. 缩放背景图像以使其尽可能大,以使背景区域完全被背景图像覆盖。 Some parts of the background image may not be in view within the background positioning area 在背景定位区域内可能看不到背景图像的某些部分

Yea, I'm not entirely sure what you're asking and your comment didn't particularly clear that up for me. 是的,我不确定您要问的是什么,您的评论对我来说还不是很清楚。

All I took out of this is that you want the image to look similar or the same on higher resolution screens? 我要做的就是让图像在高分辨率的屏幕上看起来相似还是相同? For one, if you want to test that you can zoom out on your browser to get an estimate on what it would look like on higher dpi screens. 例如,如果要测试您可以缩小浏览器,以估算出在更高dpi的屏幕上的外观。 There's also a mobile emulator as part of Chrome's devtools that will simulate higher dpi's. Chrome的devtool中还有一个移动模拟器,可以模拟更高的dpi。

As to your question, if the question is how can you maintain the aspect ratio as well as keep the image at 100% the width past 1080p, a simple solution would be to add a media query that forces width 100% past a screen width of 1920 pixels. 对于您的问题,如果问题是如何保持宽高比以及如何将图像的宽度保持在1080p之后的100%,则简单的解决方案是添加一个媒体查询,以强制宽度超过屏幕宽度100% 1920像素。 Which would look like width: 100%; 看起来像width: 100%; . If that doesn't look exactly right to you, you could try setting the width past 100% at a value like 150% so it takes up a similar sized portion of the screen as earlier. 如果那看起来不太合适,您可以尝试将超过100%的宽度设置为150%之类的值,这样它将占用屏幕上与之前相同的大小。 If that is not your question and you want the image to scale with the dpi, experiment with the values vh and vw which will do just this. 如果这不是您的问题,并且您希望图像以dpi缩放,请尝试使用值vhvw可以做到这一点。 They stand for viewport height and width. 它们代表视口的高度和宽度。 You can also set the maximum width, height, vw, and vh with max-height/width and vmin/max 您还可以使用max-height/widthvmin/max设置最大宽度,高度,vw和vh

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

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