【问题标题】:Why i can not set image style height 100% for height as same as div container?为什么我不能将高度的图像样式高度设置为 100% 与 div 容器相同?
【发布时间】:2025-12-11 02:30:02
【问题描述】:

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

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

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>

【问题讨论】:

  • 使用显示:内联

标签: html css


【解决方案1】:

问题出在 img 标签的父容器上。由于没有为其定义高度,因此您不能使用相对于父容器的 100%。在我的 .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%;
    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;

}
<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>

【讨论】:

  • @Neil 如果您注意到提问者发布的代码,它不会像您提到的那样以其他方式工作。问题在于 .div2 维度,因为它尚未修复,图像只会采用其自身的实际尺寸而不是父容器,这就是我的意思。
【解决方案2】:

您的图像与您的 div 大小相同,但您的 div 与包含它的 a 标签大小不同。因为你的 aoverflow:hidden; 你看不到你的 div 实际上更大。

您不能只将 height:100%;width:100%; 添加到您的 div 中,而是保持图像尺寸相同。

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

height: 197.207px;

【讨论】:

    【解决方案3】:

    我认为你需要这个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>

    【讨论】:

      最近更新 更多