我正在try 创建响应网格的正方形(3x3,然后是5x5).下面的代码正确地创建了一个随着窗口缩小而变小的正方形网格.但是,我想将整个容器的最大高度限制为浏览器窗口的高度.这意味着我必须限制单个正方形的高度.但当我将min-heightmax-height设置为正方形时,宽度没有变化.

.square-container {
  display: grid;
  grid-template-columns:repeat(3,1fr);
  grid-gap: 10px;
}

.square {
  border: 1px solid;
  border-radius: 5px;
  aspect-ratio:1/1;
}
<div class="square-container">
  <div class="square">1</div>
  <div class="square">2</div>
  <div class="square">3</div>
  <div class="square">4</div>
  <div class="square">5</div>
  <div class="square">6</div>
  <div class="square">7</div>
  <div class="square">8</div>
  <div class="square">9</div>
</div>

推荐答案

将逻辑应用于容器

.square-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: 1fr; /* equal rows*/
  gap: 10px;
  aspect-ratio: 1; /* make the container a square */
  /* limit the width and height */
  max-height: 100vh;
  max-width: 100%;
  /* center horizontally */
  margin: auto;
}

.square {
  border: 1px solid;
  border-radius: 5px;=
  /* full width and height*/
  height: 100%;
  width: 100%;
  /**/
  box-sizing: border-box;
}

body {
  margin: 0;
}
<div class="square-container">
  <div class="square">1</div>
  <div class="square">2</div>
  <div class="square">3</div>
  <div class="square">4</div>
  <div class="square">5</div>
  <div class="square">6</div>
  <div class="square">7</div>
  <div class="square">8</div>
  <div class="square">9</div>
</div>

Html相关问答推荐

如何防止下拉菜单内的Bootstrap列表行包装?

如何通过将项目包裹在迪夫中来保持最右列的对齐方式来对齐项目?

如何将grid—template—column应用于元素中的子元素

如何仅 Select 与子代CSS类匹配的第一个元素

在网页上的 Select 器中显示选项时出现问题:未在GO模板中传递循环{{range}}的数据

不呈现孙子元素,使用R htmlTools::Taglist为分页的pdf生成HTML

使用CSS Flexbox时,我的图像未对齐

你将如何为手机制作导航栏

如何制作带有粘性头和脚的可滚动车身

为什么 Select 元素在带有数据绑定的Blazor上行为怪异?

希望平稳过渡到悬停状态

如何使VS代码支持以模仿嵌套的方式对已经包含注释的HTML进行注释?

为什么要添加换行符(至少在 Google Chrome 中)

为什么 Flexbox 中相邻元素上的 flex:1 会影响另一个元素的固定宽度?

白色栏超出页面100%的右侧

使用flex wrap时,如何设置另一个元素的宽度与换行后的元素对齐?

如何截断一些文本并用双引号引起来?

使用 css 将内容居中并向左对齐?

在 html 邮箱的左侧和右侧制作多个元素很热门吗?

有没有办法使用 CSS border-radius 创建 HTML 视频标签的三角形显示?