响应的背景图像不显示

时间:2016-07-20 08:53:26

标签: html css responsive-design responsive responsive-images

我尝试在图片的3列中创建一些链接,但我不知道为什么这些图片不会显示。我搜索了很多关于这些事情的信息,但没有人帮助过我。有人可以帮我这个吗? :)

HTML

<div id="place">
    <div class="place_button">

            <div id="choose">
                <a href="">
                    <div class="button image1"></div>
                </a>
            </div>

            <div id="choose">
                <a href="">
                    <div class="button image2"></div>
                </a>
            </div>

            <div id="choose">
                <a href="">
                    <div class="button image3"></div>
                </a>
            </div>

    </div>
</div>

CSS

#place .place_button{
    display:table;
    margin: 0 auto 20px auto;
}

#place #choose{
    float: left;
    margin: 1%;
}

#place .button{
    display: table-cell;
    background-size: cover;
    background-position:center;
  width: 31%;
  height:auto;
    -webkit-transition:1s;-moz-transition:1s;-o-transition:1s;
}
/* There is different images! */
#place .image1{background-image: url('http://swayevents.com/sites/default/files/05_0.jpg');}
#place .image1:hover{background-image:url('https://f4.bcbits.com/img/a0401558592_16.jpg');}

#place .image2{background-image:url('http://swayevents.com/sites/default/files/05_0.jpg');}
#place .image2:hover{background-image:url('https://f4.bcbits.com/img/a0401558592_16.jpg');}

#place .image3{background-image:url('http://swayevents.com/sites/default/files/05_0.jpg');}
#place .image3:hover{background-image:url('https://f4.bcbits.com/img/a0401558592_16.jpg');}

我的JSFiddle

2 个答案:

答案 0 :(得分:1)

图片设置为 background-image 并不会给出高度和宽度。所以你需要将非百分比的高度和宽度值添加到声明背景图片的 div 中。

示例:

#place .button{ 
  width: 300px;
  height:200px;
}

<强> 修改

要使其响应,您应该更改列结构。试试这个。

<强> HTML

<div id="place">
  <div class="place_button">
    <a href="" id="choose" >
      <div class="button image1"></div>
    </a>
    <a href="" id="choose" >
      <div class="button image2"></div>
    </a>
    <a href="" id="choose" >
      <div class="button image3"></div>
    </a>
  </div>
</div>

<强> CSS

#place .place_button{
  display:table;
  margin: 0 auto 20px auto;
}

#place #choose{
  display:block;
  float: left;
  margin: 1%;
}

#place .button{
  background-size: cover;
  background-position:center;
  width: 300px;
  height:200px;
 -webkit-transition:1s;-moz-transition:1s;-o-transition:1s;
}

答案 1 :(得分:0)

我找到了关于vh单位的信息,我尝试使用它们,这对我有所帮助! 现在我的代码看起来像这样......

HTML 无需更改

CSS

#place .place_button{
    display:table;
    margin: 0 auto 20px auto;
}

#place #choose{
    float: left;
    width:31%;
    margin: 1%;
}

#place .button{
    display: table-cell;
    background-size: cover;
    background-position:center;
    height: 40vh;
    width: 40vh;
    -webkit-transition:1s;-moz-transition:1s;-o-transition:1s;
}

感谢您的帮助! :)