为什么我不能将图像样式高度设置为100%高度与div容器相同?

时间:2017-12-05 12:59:53

标签: html css

为什么我不能将图像样式高度设置为100%高度与div容器相同?

当我尝试测试代码时,它会像这样显示。

https://i.imgur.com/Q1MGP0Z.png

我想要显示这样的图像。

https://i.imgur.com/GeLUUCo.png

类名img_look样式height: 100%;我该怎么办?



.li_look{
    padding: 0px;
    margin: 0;
    position: relative;
    width: 25%;
    border-right: 3px solid #fff;
    margin-right: -3px;
    z-index: 11;
    float: left;
    list-style: none;
    color: #333;
    font-size: 19px;
    background: #fff;
}
.div_1{
    float: none;
    margin: 0;
    width: 100%;
    height: 197.207px;
    display: block;
    position: relative;
}
.a_tag{
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    height: 100%;
    margin-left: auto;
    margin-right: auto;
    overflow: hidden;
    display: block;
    cursor: pointer;
}
.div_2{
    transform: translateX(-50%);
    left: 50%;
    top: 0;
    background-color: transparent;
    display: block;
    position: absolute;
}
.img_look{
    height: 100%;
    display: block;
    margin-left: auto;
    margin-right: auto;
    max-width: unset;

}

<ul>
  <li class="li_look"> 
    <div class="div_1">
      <a href="#" class="a_tag">
        <div class="div_2">
          <img src="https://i2.wp.com/www.thisblogrules.com/wp-content/uploads/2010/02/man-and-bear-bath.jpg?resize=550%2C356" class="img_look">
        </div>
      </a>
    </div>
  </li>
</ul>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

问题在于img标签的父容器。因为没有为它定义高度,所以不能相对于父容器使用100%。在.div_2的代码中定义如下所示的高度。它会正常工作。

&#13;
&#13;
.li_look{
    padding: 0px;
    margin: 0;
    position: relative;
    width: 25%;
    border-right: 3px solid #fff;
    margin-right: -3px;
    z-index: 11;
    float: left;
    list-style: none;
    color: #333;
    font-size: 19px;
    background: #fff;
}
.div_1{
    float: none;
    margin: 0;
    width: 100%;
    height: 197.207px;
    display: block;
    position: relative;
}
.a_tag{
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    height: 100%;
    width:100%;
    margin-left: auto;
    margin-right: auto;
    overflow: hidden;
    display: block;
    cursor: pointer;
}
.div_2{
    transform: translateX(-50%);
    left: 50%;
    top: 0;
    background-color: transparent;
    display: block;
    position: absolute;
    height:100%;
}
.img_look{
    height: 100%;
    display: block;
    margin-left: auto;
    margin-right: auto;
    max-width: unset;

}
&#13;
<ul>
  <li class="li_look"> 
    <div class="div_1">
      <a href="#" class="a_tag">
        <div class="div_2">
          <img src="https://i2.wp.com/www.thisblogrules.com/wp-content/uploads/2010/02/man-and-bear-bath.jpg?resize=550%2C356" class="img_look">
        </div>
      </a>
    </div>
  </li>
</ul>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您的图片与div的大小相同,但您的div与保留它的a标记的大小不同。由于您的aoverflow:hidden;,您无法看到您的div实际上更大。

您只能将height:100%;width:100%;添加到您的div,但保持图片尺寸不变。

另外:你觉得这感觉真的不对吗?

height: 197.207px;

答案 2 :(得分:0)

我认为你需要这个https://jsfiddle.net/wqhch5et/查看全屏模式。 你需要改变位置:绝对;定位:固定;在div_2中

.li_look{
    padding: 0px;
    margin: 0;
    position: relative;
    width: 25%;
    border-right: 3px solid #fff;
    margin-right: -3px;
    z-index: 11;
    float: left;
    list-style: none;
    color: #333;
    font-size: 19px;
    background: #fff;
}
.div_1{
    float: none;
    margin: 0;
    width: 100%;
    height: 197.207px;
    display: block;
    position: relative;
}
.a_tag{
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    height: 100%;
    margin-left: auto;
    margin-right: auto;
    overflow: hidden;
    display: block;
    cursor: pointer;
}
.div_2{
    transform: translateX(-50%);
    left: 50%;
    top: 0;
    background-color: transparent;
    display: block;
    position: fixed;
}
.img_look{
    height: 100%;
    display: block;
    margin-left: auto;
    margin-right: auto;
    max-width: unset;

}
<ul>
  <li class="li_look"> 
    <div class="div_1">
      <a href="#" class="a_tag">
        <div class="div_2">
          <img src="https://i2.wp.com/www.thisblogrules.com/wp-content/uploads/2010/02/man-and-bear-bath.jpg?resize=550%2C356" class="img_look">
        </div>
      </a>
    </div>
  </li>
</ul>