[英]How to get background image cover with 100% height?
I'm using an image with a height of 5000px, and i want make it always appear 100% in width and height to cover the background, in mobile and desktop.我正在使用高度为 5000 像素的图像,我想让它在移动和桌面上始终显示 100% 的宽度和高度以覆盖背景。
.main {
position: relative;
background: url('../images/background.png') no-repeat top center;
background-size: cover;
width: 100%;
}
This code does not work, it makes her not to appear.这段代码不起作用,它让她没有出现。 I always need to set a height, and the problem is that the mobile's height is different from the desktop.
我总是需要设置一个高度,问题是手机和桌面的高度不同。
So you could say.. 'you can set height: 100%'.. and I did .. but nothing happens, the image doesn't appear, only if i set with pxs.所以你可以说..'你可以设置高度:100%'..我做了..但没有任何反应,图像不会出现,只有当我用像素设置时。
I feel urged to update my answer since I apparently understood the question the wrong way.我觉得有必要更新我的答案,因为我显然以错误的方式理解了这个问题。 I'll leave the old version at the bottom since apparently a lot of people found it helpful even though it failed to answer the original question.
我将把旧版本留在底部,因为显然很多人发现它很有帮助,即使它没有回答最初的问题。
Since your background image is repeating itself, I'll assume you don't want the whole image, just whatever height you need.由于您的背景图像是重复的,我假设您不想要整个图像,只需要您需要的任何高度。 So, you need 2 things:
所以,你需要两件事:
.main
.main
上设置高度background-size
altogetherbackground-size
So, this should actually work for you:所以,这实际上应该对你有用:
.main {
position: relative;
background: url('../images/background.png') no-repeat top center;
width: 100%;
height: 100%;
}
If my assumption is correct, there's 1 more thing: you don't need a background over 5000px high to achieve your goal, just reduce it to 1px height (ie 1 line of your desired background) and change your css to:如果我的假设是正确的,那么还有一件事:您不需要超过 5000 像素高的背景来实现您的目标,只需将其降低到 1 像素高度(即所需背景的 1 行)并将您的 css 更改为:
.main {
position: relative;
background: url('../images/background.png') repeat-y top center;
background-size: cover;
width: 100%;
}
I hope this helps我希望这有帮助
Your .main
has no height and height:100%;
你的
.main
没有 height 和height:100%;
doesn't work because the elements containing it have no height themselves.不起作用,因为包含它的元素本身没有高度。
One possible solution would be to add this:一种可能的解决方案是添加以下内容:
html, body, .main {
height:100%;
}
This might be exactly what you need, but you may also run into other problems with this solution.这可能正是您所需要的,但您也可能会遇到此解决方案的其他问题。 It all depends on what you're actually trying to achieve.
这一切都取决于您实际想要实现的目标。
Other possible solutions:其他可能的解决方案:
Use viewport units使用视口单位
.main {
height:100vh;
}
Please be aware that some mobile devices interpret these differently from what you'd expect.请注意,某些移动设备对这些的解释与您的预期不同。
Add the background to the body itself将背景添加到身体本身
body {
background: url('../images/background.png') no-repeat top center;
background-size: cover;
}
As I wrote before: It's difficult to tell which solution is the best, it depends on your goal.正如我之前所写:很难判断哪种解决方案最好,这取决于您的目标。
Have you tried adding this style?您是否尝试过添加这种样式?
html, body{ height: 100%;}
Then adding a height:100%;
然后添加一个
height:100%;
to your .main
div到你的
.main
div
You are working with background-image... Keep in mind that the size of the rendered image has nothing to do with the image it self, but with the element created to contain it.您正在使用背景图像...请记住,渲染图像的大小与图像本身无关,而是与为包含它而创建的元素有关。
Now, if you want your image to appear at 100% height and width you can use the property background-size: contain
, instead of cover.现在,如果您希望图像以 100% 的高度和宽度显示,您可以使用属性
background-size: contain
,而不是封面。 This will tell the browser that your image should not be cropped (as long as you have a height set for the .main element).这将告诉浏览器不应裁剪您的图像(只要您为 .main 元素设置了高度)。
It seems to me, that the kind of effect you want is easier done if you just use the <img>
tag instead of css background.在我看来,如果您只使用
<img>
标签而不是 css 背景,那么您想要的那种效果会更容易实现。
I had a issue about flex box can`t fit the background height, and the code below suited for me.我有一个关于 flex box 无法适应背景高度的问题,下面的代码适合我。 The rest background-size,repeat and position depends on yours.
其余的背景大小、重复和位置取决于您的。
html{
height:auto;
}
body{
display: flex;
flex-direction: column;
min-height: 100vh;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.