【问题标题】:CSS grid column width of size "auto" is wider than the image in the column大小为 \"auto\" 的 CSS 网格列宽比列中的图像宽
【发布时间】:2023-01-11 12:31:41
【问题描述】:

我的响应式 CSS 网格在特定维度上表现得很奇怪 - 在下面的 sn-p 中,我希望图像占据第一列(自动大小)并跨越给定断点的网格的所有行。

但是,对于这个特定的网格高度/宽度,列宽无意中比实际图像宽。这个奇怪的问题在图像的左侧和右侧留下了不需要的空白区域——应该分配给宽度为 1fr 的第二列的空间。

使网格变宽似乎可以解决这个问题——第一列会立即更改宽度以匹配图像宽度。 (为了您的方便,我使外部 div 可调整大小。)

(笔记:

  • 图像不应拉伸,它的高度应为网格的高度,宽度应按比例缩放
  • 网格最小高度应为其父元素的 100%)

*,
::before,
::after {
  box-sizing: border-box; /* 1 */
  border-width: 0;
  border-style: solid;
}

img {
  max-width: 100%;
  height: auto;
}

.flex {
  display: flex;
}
.grid {
  display: grid;
}
.h-full {
  height: 100%;
}
.h-min {
  height: -moz-min-content;
  height: min-content;
}
.max-h-full {
  max-height: 100%;
}
.max-h-fit {
  max-height: -moz-fit-content;
  max-height: fit-content;
}
.min-h-0 {
  min-height: 0px;
}
.w-full {
  width: 100%;
}
.w-\[20cqw\] {
  width: 20cqw;
}
.w-fit {
  width: -moz-fit-content;
  width: fit-content;
}
.max-w-full {
  max-width: 100%;
}
.max-w-fit {
  max-width: -moz-fit-content;
  max-width: fit-content;
}
.resize {
  resize: both;
}
.grid-cols-\[auto_1fr\] {
  grid-template-columns: auto 1fr;
}
.grid-rows-\[auto_auto_1fr_auto\] {
  grid-template-rows: auto auto 1fr auto;
}
.flex-col {
  flex-direction: column;
}
.justify-self-center {
  justify-self: center;
}
.overflow-auto {
  overflow: auto;
}
.rounded {
  border-radius: 0.25rem;
}
.rounded-sm {
  border-radius: 0.125rem;
}
.border-4 {
  border-width: 4px;
}
.border {
  border-width: 1px;
}
.border-purple-800 {
  --tw-border-opacity: 1;
  border-color: rgb(107 33 168 / var(--tw-border-opacity));
}
.border-sky-500 {
  --tw-border-opacity: 1;
  border-color: rgb(14 165 233 / var(--tw-border-opacity));
}
.border-neutral-400 {
  --tw-border-opacity: 1;
  border-color: rgb(163 163 163 / var(--tw-border-opacity));
}
.border-green-500 {
  --tw-border-opacity: 1;
  border-color: rgb(34 197 94 / var(--tw-border-opacity));
}
.bg-purple-800 {
  --tw-bg-opacity: 1;
  background-color: rgb(107 33 168 / var(--tw-bg-opacity));
}
.p-3 {
  padding: 0.75rem;
}
.p-2 {
  padding: 0.5rem;
}
.p-4 {
  padding: 1rem;
}
.px-4 {
  padding-left: 1rem;
  padding-right: 1rem;
}
.py-2 {
  padding-top: 0.5rem;
  padding-bottom: 0.5rem;
}

.text-white {
  --tw-text-opacity: 1;
  color: rgb(255 255 255 / var(--tw-text-opacity));
}

.\@container {
  container-type: inline-size;
}

@container (min-width: 20rem) {

  .\@xs\:row-span-full {
    grid-row: 1 / -1;
  }

  .\@xs\:h-\[min\(100\%_20cqi\)\] {
    height: min(100% 20cqi);
  }

  .\@xs\:h-\[min\(100\%\2c _20cqi\)\] {
    height: min(100%, 20cqi);
  }

  .\@xs\:w-\[20cqw\] {
    width: 20cqw;
  }

  .\@xs\:grid-cols-\[auto_1fr\] {
    grid-template-columns: auto 1fr;
  }

  .\@xs\:grid-rows-\[auto_1fr_auto\] {
    grid-template-rows: auto 1fr auto;
  }
}
<div
  class="resize overflow-auto border border-sky-500 p-2"
  style="width: 405px; height: 160px"
>
  <div class="flex h-full flex-col border border-green-500 @container">
    <h4>Example</h4>
    <div
      class="grid h-full min-h-0 grid-rows-[auto_auto_1fr_auto] rounded border border-neutral-400 @xs:grid-cols-[auto_1fr] @xs:grid-rows-[auto_1fr_auto]"
    >
      <img
        alt="300"
        src="https://via.placeholder.com/300"
        width="400"
        height="400"
        class="@xs:max-w-[min(100%,_20cqi) max-h-full min-h-0 w-fit max-w-full justify-self-center @xs:row-span-full"
      />
      <div>
        <div class="">Featured product</div>
        <div class="">Nutri Ninja Foodi 6L Multi Cooker</div>
      </div>
      <div class="">$299</div>
      <button
        class="rounded bg-purple-800 px-4 py-2 text-white"
      >
        Buy
      </button>
    </div>
  </div>
</div>

【问题讨论】:

  • 抱歉,如果我直言不讳,但您的代码甚至不接近 minimal reproducible example。不要只是发布所有代码然后看看会发生什么。至少删除您认为我们需要调查的所有禁用代码和名称类/ID。
  • 抱歉,我已经清理了 sn-p,但我将致力于简化我直接从 tailwind 中提取的类,这使得重现变得有点困难。希望检查开发控制台中的相关元素足以理解相关的 CSS 网格问题。

标签: css css-grid


【解决方案1】:

您可以尝试使用 grid-template-columns 属性为图像所在的网格列指定固定宽度来解决此问题。

  • 无论图像有多大,这都将保证列的宽度始终相同。您可以尝试使第一列的宽度等于图像的宽度,或者您可以使用媒体查询根据屏幕大小更改网格列的宽度。
  • 网格模板列:minmax(auto,1fr);将分别使用 auto 和 1fr 的最小值和最大值。图像的宽度将被网格使用,而不是 1fr 的较大值。
  • 另一种选择是从 img 类中删除 max-width: 100% 指令,因为网格已包含它。
  • 此外,查找任何冲突的 CSS 类并根据需要修改高度、宽度和对象适合度可能会有好处。

(我希望它对你有帮助。谢谢)

【讨论】:

  • 你能修改我的 sn-p 并给我看吗?我已经尝试过这种变化 - 第一列的宽度为 auto,第二列的宽度为 1fr,因此第一列应该缩小到图像的宽度,但事实并非如此。请注意,我不希望第一列的宽度固定 - 图像宽度应与图像高度成比例增加/减少,这应该是整个网格的高度。
  • img { 高度:100%;宽度:自动;适合对象:包含; } .grid-columns-auto 1fr { grid-template-columns: minmax(auto, 1fr); } (抱歉代码对齐问题。无法在评论中对齐)
  • 这就是我内联您的img 和网格样式后的样子:stackblitz.com/edit/web-platform-76wxhw?file=script.js - 图像被隐藏,因为它不再是 2 列。这是它最初的样子(与有问题的 sn-p 相同):stackblitz.com/edit/web-platform-hywn2e?file=script.js
【解决方案2】:

图片不显示是因为父元素的高度是固定的,没有空间来渲染图片,所有其他元素都占用空间。

所以解决方案是让父级高度为 min-content 使你 图像灵活且反应灵敏

*,
::before,
::after {
  box-sizing: border-box; /* 1 */
  border-width: 0;
  border-style: solid;
}

img {
  max-width: 100%;
  height: auto;
}

.flex {
  display: flex;
}
.grid {
  display: grid;
}
.h-full {
  height: 100%;
}
.h-min {
  height: -moz-min-content;
  height: min-content;
}
.max-h-full {
  max-height: 100%;
}
.max-h-fit {
  max-height: -moz-fit-content;
  max-height: fit-content;
}
.min-h-0 {
  min-height: 0px;
}
.w-full {
  width: 100%;
}
.w-[20cqw] {
  width: 20cqw;
}
.w-fit {
  width: -moz-fit-content;
  width: fit-content;
}
.max-w-full {
  max-width: 100%;
}
.max-w-fit {
  max-width: -moz-fit-content;
  max-width: fit-content;
}
.resize {
  resize: both;
}
.grid-cols-[auto_1fr] {
  grid-template-columns: auto 1fr;
}
.grid-rows-[auto_auto_1fr_auto] {
  grid-template-rows: auto auto 1fr auto;
}
.flex-col {
  flex-direction: column;
}
.justify-self-center {
  justify-self: center;
}
.overflow-auto {
  overflow: auto;
}
.rounded {
  border-radius: 0.25rem;
}
.rounded-sm {
  border-radius: 0.125rem;
}
.border-4 {
  border-width: 4px;
}
.border {
  border-width: 1px;
}
.border-purple-800 {
  --tw-border-opacity: 1;
  border-color: rgb(107 33 168 / var(--tw-border-opacity));
}
.border-sky-500 {
  --tw-border-opacity: 1;
  border-color: rgb(14 165 233 / var(--tw-border-opacity));
}
.border-neutral-400 {
  --tw-border-opacity: 1;
  border-color: rgb(163 163 163 / var(--tw-border-opacity));
}
.border-green-500 {
  --tw-border-opacity: 1;
  border-color: rgb(34 197 94 / var(--tw-border-opacity));
}
.bg-purple-800 {
  --tw-bg-opacity: 1;
  background-color: rgb(107 33 168 / var(--tw-bg-opacity));
}
.p-3 {
  padding: 0.75rem;
}
.p-2 {
  padding: 0.5rem;
}
.p-4 {
  padding: 1rem;
}
.px-4 {
  padding-left: 1rem;
  padding-right: 1rem;
}
.py-2 {
  padding-top: 0.5rem;
  padding-bottom: 0.5rem;
}

.text-white {
  --tw-text-opacity: 1;
  color: rgb(255 255 255 / var(--tw-text-opacity));
}

.@container {
  container-type: inline-size;
}

@container (min-width: 20rem) {

  .@xs:row-span-full {
    grid-row: 1 / -1;
  }

  .@xs:h-[min(100%_20cqi)] {
    height: min(100% 20cqi);
  }

  .@xs:h-[min(100%c _20cqi)] {
    height: min(100%, 20cqi);
  }

  .@xs:w-[20cqw] {
    width: 20cqw;
  }

  .@xs:grid-cols-[auto_1fr] {
    grid-template-columns: auto 1fr;
  }

  .@xs:grid-rows-[auto_1fr_auto] {
    grid-template-rows: auto 1fr auto;
  }
}
<div
  class="resize overflow-auto border border-sky-500 p-2"
  style="width: 405px; height: min-content"
> <!-- changed here height to min-content -->
  <div class="flex h-full flex-col border border-green-500 @container">
    <h4>Example</h4>
    <div
      class="grid h-full min-h-0 grid-rows-[auto_auto_1fr_auto] rounded border border-neutral-400 @xs:grid-cols-[auto_1fr] @xs:grid-rows-[auto_1fr_auto]"
    >
      <img
        alt="300"
        src="https://via.placeholder.com/300"
        width="400"
        height="400"
        class="@xs:max-w-[min(100%,_20cqi) max-h-full min-h-0 w-fit max-w-full justify-self-center @xs:row-span-full"
      />
      <div>
        <div class="">Featured product</div>
        <div class="">Nutri Ninja Foodi 6L Multi Cooker</div>
      </div>
      <div class="">$299</div>
      <button
        class="rounded bg-purple-800 px-4 py-2 text-white"
      >
        Buy
      </button>
    </div>
  </div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 2011-04-18
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多