简体   繁体   English

缩放背景图片以适合标签上方的区域

[英]Scale background image to fit area above tag

Given a div (or other tag) is it possible to add a large "responsive" image above the tag (think site banner) that has a max width and height and then scales to stay at 100% of the browser width while the height adjusts automatically using only css? 给定一个div(或其他标签),可以在标签(认为网站横幅)上方添加具有最大宽度和高度的大型“响应式”图像,然后在高度调整时缩放以保持浏览器宽度的100%自动仅使用CSS?

I have tried 3 different method each with have a major downside. 我尝试了3种不同的方法,每种方法都有很大的缺点。 On all the methods here is the html used. 在所有方法上,这里都使用了html。 and the #nav is where I am trying to add the picture above. #nav是我尝试在上面添加图片的位置。

<ul id="nav">
    <li>link</li>
</ul>

Method 1 方法1

using :before and content:url(img); 使用:before和content:url(img);

CSS: CSS:

#nav:before {
    content:url('http://lorempixel.com/output/nature-q-c-780-480-5.jpg');
    width:100%;
    display:block;
    overflow:hidden;
}

This method will keep the height correct but will hide the right side of the image if I reduce the size of the screen. 如果我减小屏幕尺寸,此方法将保持正确的高度,但将隐藏图像的右侧。 I could not find a way to properly scale the content image. 我找不到适当缩放内容图像的方法。

JSFiddle of method 1 JSFiddle方法1

Method 2 方法2

using :before and background:url() I was able to get the image to scale properly but I am unable to change the value of the height, which gives the huge area below the image. 使用:before和background:url()可以使图像正确缩放,但无法更改height的值,这会在图像下方提供巨大的区域。

#nav:before {
    background:url('http://lorempixel.com/output/nature-q-c-780-480-5.jpg') no-repeat top;
    background-size:contain;
    width:100%;
    display:block;
    content:"";
    height:400px;
    max-height:480px;
}

JSFiddle of method 2 JSFiddle方法2

Method 3 方法3

I was somewhat able to achieve the desired effect but the padding-top is dependent on knowing the ration of the height to width for the image. 我在某种程度上能够达到预期的效果,但是顶部填充取决于知道图像的高宽比。 (I fail to understand in css terms how this is working and why this calculation is needed). (我无法用CSS的术语理解它是如何工作的以及为什么需要这种计算)。 So for a square image its X/X = 1 or 100% height. 因此,对于正方形图像,其X / X = 1或100%的高度。 And for the 480x780 image it is 61.53% 而对于480x780图片,则为61.53%

#nav {
    background:url('http://lorempixel.com/output/nature-q-c-780-480-5.jpg') no-repeat top center;
    background-size:contain;
    padding-top:61.53%;
    width:100%;
    display:block;
    height:100px;
    overflow:hidden;
}

This method does produce the desired result but I cannot always know the height and width of the image. 这种方法的确产生了预期的结果,但是我不能总是知道图像的高度和宽度。 So I am looking for a better solution. 因此,我正在寻找更好的解决方案。

I also realize that I could create a media query for each different size and an extra image, but that is way to much work. 我也意识到我可以为每个不同的尺寸和一个额外的图像创建一个媒体查询,但这是很多工作的方式。

JSFiddle of method 3 JSFiddle方法3

Any ideas on how to improve one of these methods? 关于如何改善这些方法之一的任何想法?

Hm, I hope I got you right. 嗯,我希望我没错。 This one is an image above your navigation and it's always 100% wide (same as browser size). 此图片是导航上方的图片,并且始终为100%宽(与浏览器大小相同)。 It scales up and down on resize and the height is calculated automatically so that the aspect ratio is not affected: 它会根据调整大小上下缩放,并且会自动计算高度,因此长宽比不会受到影响:

HTML 的HTML

<img class="banner" src="http://lorempixel.com/output/people-q-c-1200-200-9.jpg">
<ul>
    <li><a href="">link</a></li>
</ul>​

CSS 的CSS

img.banner {
    width: 100%;
}​

Fiddle 小提琴

http://jsfiddle.net/7HgU8/ http://jsfiddle.net/7HgU8/

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

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