css - 具有明确定义尺寸的 Div 是否会随视口(viewport)宽度缩放尺寸?

标签 css responsive-design flexbox media-queries

我在一个容器中有两个 div:一个包含图像,另一个包含文本。图像容器具有设置的尺寸 width: 250px;height: 240px;,但是当屏幕(和包含的 div)足够窄时,图像容器开始除了另一个设置为 width: auto; 的 div 之外,还要减小尺寸。

我怀疑这是由于第二个内部 div 中的文本需要一定的空间,因此将图像容器向内“推”,但使用 word-break: break-word;没有解决问题。

风格:

/* "imagecontainer" and "credentials" are contained within "info-container" */

.subsection .info-container {
    display: flex;
}

.subsection .imagecontainer {
    width: 250px;
    height: 240px;
    margin: 0;
    padding: 0;
}

.subsection .imagecontainer img {
    width: 90%;
    height: 90%;
    margin-left: auto;
    margin-right: auto;
    border-radius: 50%;
    background-repeat: no-repeat;
    background-position: center center;
    background-size: cover;
    margin-top: 5px;
}

.subsection .credentials {
    width: 600px;
    height: 230px;
    padding: 10px;
    padding-right: 20px;
    position: relative;
    border-right: 1px solid #333;
}

/* This problem occurs at pageWidth<920px, when this media query would be in effect: */

@media only screen and (max-width: 920px) {
    .subsection .credentials {
        width: auto;
        border-right: none;
    }
}

标记:

<div class="subsection">
  <div class="info-container">
    <div class="imagecontainer">
      <img src="data/users/images/5d7d03b1d0013.png">
    </div>
    <div class="credentials" style="width: auto;">
      <div class="name">Test User</div>
      <div class="location">Albany, New York</div>
    </div>
  </div>
  <div class="actions">
    <div class="actions-container">
      <div class="action">
        <a class="btn btn-primary" href="viewprofile?id=5d7d03b1d0013">View profile</a>
      </div>
      <div class="action">
        <a class="btn btn-primary" href="connect?id=5d7d03b1d0013">Make Connection</a>
      </div>
    </div>
  </div>
</div>

预期:只有包含文本的 div 会根据视口(viewport)宽度的变化调整其宽度。

实际:包含图像的 div 和包含文本的 div 的大小都减小了。

编辑: 添加标记

最佳答案

该图像容器的父容器具有 display: flex;。如果您不为子元素定义任何内容,它们将被允许收缩。

为避免这种情况,您可以将 flex-shrink: 0(意味着它不能变得更小)添加到 .subsection .imagecontainer .

关于css - 具有明确定义尺寸的 Div 是否会随视口(viewport)宽度缩放尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57948653/

相关文章:

html - 多行表格单元格,将每一行显示为单独的单元格

html - 默认@media 查询样式

html - 如何使我的导航栏在两个不均匀的元素之间居中?

javascript - 更改具有相同类名的多个 div 的宽度的脚本

html - 使用 flex : 1 to expand the div will collapse everything in IE11

html - 内联 block ,斜体文本部分隐藏在 Chrome 上,隐藏了背面可见性

css - 选择没有 child 的元素

仅 CSS 工具提示,不适用于 Chrome 12.0.742.30 dev

css - 如何修复使用 CSS3 媒体查询时不正常的菜单?

html - (CSS) 拉伸(stretch) div 但内容在中心