简体   繁体   English

如何使用文本制作响应式全角X全高背景图像

[英]How to make responsive full width X full height background-image with text

在此处输入图片说明
I want a webpage with different background-images including text among each other, but think I get stuck in this. 我想要一个背景图像彼此不同的网页,包括彼此之间的文字,但我认为自己陷入了这种困境。 Background-images must cover entire width of website and should be responsive. 背景图片必须覆盖网站的整个宽度,并且必须具有响应能力。 Furthermure Background-images may not overlapping each other as well. 更纯净的背景图像也可能不会相互重叠。 Have checked similar questions, but that does not work well for me in this case. 已经检查了类似的问题,但是在这种情况下对我来说效果不佳。

 .first-image { background-image: url(../Img/-MI4-KK.jpg); background-size: cover; background-repeat: no-repeat; background-position: center; background-attachment: initial; background-color: #FFFFFF; background-origin: initial; background-clip: initial; } 
 <div class="first-image"> <div class="container"> <p>text</p> </div> </div> 

Have found the right answer after some research and effort. 经过一些研究和努力,找到了正确的答案。 Depend on the height of the image I figured out that background-size:cover will affect the width and height of image that have been shown. 取决于图像的高度,我发现background-size:cover将影响已显示图像的宽度和高度。 Therefore I have replaced that with 100% auto. 因此,我已将其替换为100%自动。 Furthermore after resizing the screen there was a white space under each image, which was something I would not want to see. 此外,在调整屏幕大小后,每个图像下方都有一个空白,这是我不想看到的。 In response to that I added padding-bottom of 55% (for someone who is interested how I came to this percentage: 1080 height / 1920 width x 100% = +/- 55%). 对此,我添加了55%的底部填充(对于对我感兴趣的这个百分比感兴趣的人:1080高度/ 1920宽度x 100%= +/- 55%)。

 .first-image { background-image: image 1920 x 1080 A; background-size: 100% auto; background-repeat: no-repeat; width: 100%; padding-bottom: 55%; } .second-image { background-image: image 1920 x 1080 B; background-size: 100% auto; background-repeat: no-repeat; width: 100%; padding-bottom: 55%; } .third-image { background-image: image 1920 x 1080 C; background-size: 100% auto; background-position: 100%; background-repeat: no-repeat; width: 100%; padding-bottom: 55%; } 

Here's a possible solution for you: http://codepen.io/anon/pen/GoXpXv 这是您可能的解决方案: http : //codepen.io/anon/pen/GoXpXv

I got rid of the container for you because you didn't define it in your CSS. 我为您摆脱了这个容器,因为您没有在CSS中定义它。 I created different classes for each of the image sections and have them taking up 100% of the width and 33.333% of the height. 我为每个图像部分创建了不同的类,使它们分别占据宽度的100%和高度的33.333%。 I set the body, html and div p margins and padding to 0 so there would be no white space around it. 我将body,html和div p的边距和内边距设置为0,因此周围没有空格。 This will adjust to any screen size. 这将调整为任何屏幕尺寸。 Let me know if you have any questions. 如果您有任何疑问,请告诉我。

HTML: HTML:

<div class="first-image">
  <p>first image</p>
</div>
<div class="second-image">
  <p>second image</p>
</div>
<div class="third-image">
  <p>third image</p>
</div>

CSS: CSS:

html, body {
  height: 100%;
  margin: 0px;
  padding: 0px;
}
.first-image {
  width: 100%;
  height: 33.333%;
  background-image: url(http://www.gettyimages.ca/gi-resources/images/Homepage/Category-Creative/UK/UK_Creative_462809583.jpg);
  background-size: cover;
  background-position: center;
}
.second-image {
  width: 100%;
  height: 33.333%;
  background-image: url(https://cdn.photographylife.com/wp-content/uploads/2014/06/Nikon-D810-Image-Sample-6.jpg);
  background-size: cover;
  background-position: center;
}
.third-image {
  width: 100%;
  height: 33.333%;
  background-image: url(http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image2.jpg);
  background-size: cover;
  background-position: center;
}
div p {
  margin: 0px;
  color: #FFFFFF;
}

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

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