【问题标题】:Resizing image to fit responsive column cell using css使用 css 调整图像大小以适应响应列单元格
【发布时间】:2018-08-28 08:48:09
【问题描述】:

我正在使用响应式列格式来创建我的移动网站。每列的内容是使用我希望填充 100% 宽度并自动调整高度的图像形成的。

我已经尝试过 max-width:100% 和 max-height:100% 并且它不会将图像宽度填充到列区域的 100%。此解决方案调整列高以适应图像,但不会调整图像大小以满足 100% 的列宽。

源代码:

<div class="container">
    <div class="column" id="column1">
        <img id="top_block_graphic" src="graphics/top-block.png" alt=""/>
    </div>
</div>

CSS:

.container {
display: grid;
grid-template-columns: 50% 50%;
grid-gap: 7px;
clear: both;
padding: 10px;
font-size: 1.6em;
width: 100%;
}

.column {
background-color: rgba(173,167,133,0.5);
padding: 10px;
border: 1px solid black;
}

#column1 {
grid-column: 1/3;
width: 100%;
height: auto;
position: relative;
overflow: hidden;
padding: 35.66% 0 0 0;
}

#top_block_graphic {
display: block;
max-width: 100%;
height: auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}

【问题讨论】:

  • #column1 img{ width:100%; } ?
  • #column1 img is #top_block_graphic

标签: html css


【解决方案1】:

试试这个:

#column1 {
  width: 100% !important;
  height: auto !important;
}

或者这个:

#column1 {
  width: 100vw !important;
  height: auto !important; /* 100vh */
}

【讨论】:

  • 仍然没有将图像大小调整为 100% 宽度。
  • 你有这个代码:
    在其他 DIV 里面?
  • 是的。它位于一个容器 div 中,其中包含其他列元素以创建网格外观。
  • 必须先修改父div的宽度。
  • 我已更新代码以包含父 div 代码
猜你喜欢
  • 2014-05-05
  • 2012-09-15
  • 2021-09-20
  • 1970-01-01
  • 2017-02-10
  • 1970-01-01
  • 2011-02-03
  • 2020-06-03
  • 2011-04-17
相关资源
最近更新 更多