简体   繁体   English

div中包含背景图片的div无法正常工作

[英]Div with background image inside div doesn't work properly

I have a div with background image put inside another div, instead of fit width parent div, it fit full screen. 我有一个div,背景图片放在另一个div中,而不是适合宽度的父div,它适合全屏显示。 Please take a look my code to know clearly, sorry for bad english. 请看一下我的代码以清楚地知道,抱歉英语不好。

http://codepen.io/thehung1724/full/jEEgQq/ http://codepen.io/thehung1724/full/jEEgQq/

HTML 的HTML

<div id="video-section" class="dark-section">
  <div class="home"></div>
    <div class="fullscreen-img" style="background-image: url(http://upanh.biz/images/2014/11/23/bg1.jpg)"></div>
</div>

CSS 的CSS

*{
  margin: 0;
  padding: 0;
}

#video-section{
  margin: 0 auto;
  width: 1230px;
  height: 500px;
}

.dark-section{
  background-color: black;
}

.home{
    display: table;
    height: 500px;
    left: 0;
    position: relative;
    top: 0;
    width: 100%;
}

.fullscreen-img {
    background-position: center center;
    background-repeat: no-repeat;
    background-size: cover;
    height: auto;
    left: 0;
    min-height: 500px;
    position: fixed;
    top: 0;
    width: 100%;
    z-index: -1;
}

Thank in advance. 预先感谢。

The .home div needs to be absolutely positioned in order not to "push" the background div downwards. .home div必须绝对定位,以免向下“推”背景div。 The background div shouldn't have the fullscreen-img class, since most of those rules should be removed. 背景div不应具有fullscreen-img类,因为应删除大多数规则。 It only needs height: 100% because divs have width: 100% by default since they're block elements. 它仅需要height: 100%因为div的width: 100% ,因为它们是块元素。 Of course, move the inline styles into a class or ID rules, I left them there just to show you. 当然,将内联样式移动到类或ID规则中,我留在这里只是为了向您展示。

That's all you need basically: 这基本上就是您需要的:

  • remove the .fullscreen-img class from the background div 从背景div中删除.fullscreen-img
  • set its height to 100% instead 将其高度设置为100%
  • make the .home div absolutely positioned 使.home div绝对定位

See it here: http://codepen.io/anon/pen/azzexY 在这里查看: http : //codepen.io/anon/pen/azzexY

 *{ margin: 0; padding: 0; } body { background-color: black; } #video-section{ margin: 0 auto; width: 1230px; height: 500px; } .dark-section{ background-color: black; } .home{ display: table; height: 500px; left: 0; position: absolute; top: 0; width: 100%; } 
 <div id="video-section" class="dark-section"> <div class="home"></div> <div class="" style="height: 100%; background-image: url(http://upanh.biz/images/2014/11/23/bg1.jpg)"></div> </div> 


UPDATE 更新

Fixes/changes for your website for the problematic element ( <div style="background-image: url('images/bg2.jpg');" class="fullscreen-img img-after"></div> ): 修复/更改了您网站上有问题的元素( <div style="background-image: url('images/bg2.jpg');" class="fullscreen-img img-after"></div> ):

  • left: initial; 左:初始;
  • width: 1230px; 宽度:1230px;

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

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