简体   繁体   English

background-size:封面无法完全用背景图像填充div

[英]background-size: cover is not working to completely fill div with background-image

I would like for the background image to fill the div that contains it. 我想要背景图片来填充包含它的div。 I will attach pictures to show what's happening. 我将附上图片以显示正在发生的事情。

What is the cause of this empty space? 空空间的原因是什么? It is preventing me from moving my containers closer together. 这阻止了我将容器靠近在一起。

Here is a codepen link: http://codepen.io/ManBearPigg/full/qZVJWy/ 这是一个代码笔链接: http ://codepen.io/ManBearPigg/full/qZVJWy/

#firstContainer {
  background-image: url("/focusbackground.png");
  background-size: cover;
  width: 720px;
  height: 700px;
  float: left;
  margin-left: -7%;
}

#secondContainer {
  background-image: url("/focusbackground.png");
  background-size: cover;
  width: 720px;
  height: 700px;
  float:right;
  margin-right: 5%;
}

Here is the html 这是HTML

<div id="container">
  <div id="firstContainer">
    <form id="firstForm" action="">
      <textarea id="firstArea" type="text" onkeypress="return myKeyPress(event)"></textarea>
      <input type="submit" id="firstSubmit"></input>
      <div id="lineNo"></div>
    </form>
  </div>
  <div id="secondContainer">
    <form id="secondForm" action="">
      <textarea id="secondArea" type="text" onkeypress="return mySecondKeyPress(event)"></textarea>
      <input type="submit" id="secondSubmit"></input>
    </form>
  </div>
</div>

Maybe this is more a comment than an answer but I can´t add a comment on your post, so I apologise in case I did not understand your problem. 也许这不是评论,而是答案,但是我无法在您的信息中添加评论,因此如果我不理解您的问题,我深表歉意。

What I can see is that your image itself has a white border. 我看到的是您的图像本身有白色边框。 You probably need to cut the image to remove the parts without the picture. 您可能需要剪切图像以去除没有图片的部分。 By doing that, it will cover the div 这样,它将覆盖div

@will is correct. @will是正确的。 The image you are using is an actual image file and as such will have certain properties that are difficult or not worth the bother to manipulate via HTML, CSS, JavaScript, etc. Some files like images, audio, and video need editing or converting before it is uploaded. 您使用的图像是一个实际的图像文件,因此将具有某些属性,这些属性很难或不值得通过HTML,CSS,JavaScript等进行操作。某些文件(如图像,音频和视频)需要在编辑之前进行转换或转换它已上传。 That image for instance, needs that transparent area cropped. 例如,该图像需要裁剪透明区域。 I did the following: 我做了以下工作:

  1. Downloaded the PNG image file. 下载了PNG图片文件。
  2. Used Paint.NET program to crop the transparent area of the file. 使用Paint.NET程序裁剪文件的透明区域。
  3. Uploaded file to imgur . 将文件上传到imgur
  4. Replaced the url: http://s9.postimg.org/4laackvcf/focusbackground.png with the new one from imgur: http://i.imgur.com/7s1A3zf.png :更换了网址http://s9.postimg.org/4laackvcf/focusbackground.png从imgur新一: http://i.imgur.com/7s1A3zf.png

     body { background:#628fd5; } #firstContainer { background-image: url("http://i.imgur.com/7s1A3zf.png"); background-size: cover; width: 720px; height: 700px; float: left; margin-left: -7%; } #secondContainer { background-image: url("http://i.imgur.com/7s1A3zf.png"); background-size: cover; width: 720px; height: 700px; float:right; margin-right: 5%; } 

The result is at this CodePen . 结果是在此CodePen上

Btw, I replaced the blue image with body { background:#628fd5; } 顺便说一句,我用body { background:#628fd5; }替换了蓝色图片body { background:#628fd5; } body { background:#628fd5; } . body { background:#628fd5; } It's always better to use CSS than it is to use a file. 使用CSS总是比使用文件更好。 For every image you have on a page is a HTTP request round trip which adds to latency (slows down your webpage/site). 对于页面上的每个图像,都是HTTP请求往返,这会增加延迟(降低网页/站点的速度)。

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

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