我正在try 让我创建的网格正确地换行以便于移动访问,但当窗口大小足够小时,网格不会换行.

我一直在try 使用auto-fillauto-fit来换行,这确实有效,但按钮不会自动间隔以适应更大的窗口大小的整个页面.当页面变小时,有没有办法让按钮绕过来,但仍然自动向右间隔.我在使用自动填充和适应时遇到的问题还要求所有按钮的大小相同,否则按钮的间距将不均匀.

下面的代码是我现在有的版本,它使用了自动作为网格间距.

.bttns {
  display: grid;
  grid-template-columns: auto auto auto auto auto;
  border-bottom: 7px solid #4A3E94;
  background-color: #d19dfc;
  font-family: 'Lucida Console', "Lucida Console", monospace;
}

.b1 {
  a:link,
  a:visited {
    background-color: #4A3E94;
    color: white;
    padding: 3px 10px;
    text-decoration: none;
    display: inline-block;
    margin-top: 10px;
    margin-bottom: 10px;
    border: 2px solid #4A3E94;
    border-radius: 7px;
  }
  margin-right: 20px;
  font-size: 30px;
  text-align: center;
}

.b2 {
  a:link,
  a:visited {
    background-color: #4A3E94;
    color: white;
    padding: 3px 10px;
    text-decoration: none;
    display: inline-block;
    margin-top: 10px;
    margin-bottom: 10px;
    border: 2px solid #4A3E94;
    border-radius: 7px;
  }
  margin-right: 20px;
  font-size: 30px;
  text-align: center;
}

.b3 {
  a:link,
  a:visited {
    background-color: #4A3E94;
    color: white;
    padding: 3px 10px;
    text-decoration: none;
    display: inline-block;
    margin-top: 10px;
    margin-bottom: 10px;
    border: 2px solid #4A3E94;
    border-radius: 7px;
  }
  margin-right: 20px;
  font-size: 30px;
  text-align: center;
}

.b4 {
  a:link,
  a:visited {
    background-color: #4A3E94;
    color: white;
    padding: 3px 10px;
    text-decoration: none;
    display: inline-block;
    margin-top: 10px;
    margin-bottom: 10px;
    border: 2px solid #4A3E94;
    border-radius: 7px;
  }
  margin-right: 20px;
  font-size: 30px;
  text-align: center;
}

.b5 {
  a:link,
  a:visited {
    background-color: #4A3E94;
    color: white;
    padding: 3px 10px;
    text-decoration: none;
    display: inline-block;
    margin-top: 10px;
    margin-bottom: 10px;
    border: 2px solid #4A3E94;
    border-radius: 7px;
  }
  font-size: 30px;
  text-align: center;
}
<div class="bttns">
  <div class="b1">
    <a href="Imagess/pholdr.png" target="_blank">Art</a>
  </div>

  <div class="b2">
    <a href="Imagess/pholdr.png" target="_blank">Blog</a>
  </div>

  <div class="b3">
    <a href="/About/abt.html" target="_self">About</a>
  </div>

  <div class="b4">
    <a href="Imagess/steviekitty.png" target="_blank">Music</a>
  </div>

  <div class="b5">
    <a href="Imagess/kittr.png" target="_blank">WIP</a>
  </div>
</div>

推荐答案

在这里,网格似乎有点过头了.如果有更简单的规则,Flexbox会很好地处理这一问题.在这里,我将flex的内容居中对齐,这样就不会在大屏幕上笨拙地展开.例如,您可以使用justify-content: space-around,如果这是您想要的.

仅供参考,您错误地使用了类.除非您的计划中有其他内容,否则您应该为您的菜单项使用one个类,而不是每个菜单项使用一个类.在您的css中重复这么多是一个很大的危险信号.

.bttns {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  border-bottom: 7px solid #4A3E94;
  background-color: #d19dfc;
  font-family: 'Lucida Console', "Lucida Console", monospace;
}

.menu-btn {
  margin: 0 10px;
  font-size: 30px;
  text-align: center;
  a:link,
  a:visited {
    background-color: #4A3E94;
    color: white;
    padding: 3px 10px;
    text-decoration: none;
    display: inline-block;
    margin-top: 10px;
    margin-bottom: 10px;
    border: 2px solid #4A3E94;
    border-radius: 7px;
  }
}
<div class="bttns">
  <div class="menu-btn">
    <a href="Imagess/pholdr.png" target="_blank">Art</a>
  </div>

  <div class="menu-btn">
    <a href="Imagess/pholdr.png" target="_blank">Blog</a>
  </div>

  <div class="menu-btn">
    <a href="/About/abt.html" target="_self">About</a>
  </div>

  <div class="menu-btn">
    <a href="Imagess/steviekitty.png" target="_blank">Music</a>
  </div>

  <div class="menu-btn">
    <a href="Imagess/kittr.png" target="_blank">WIP</a>
  </div>
</div>

Html相关问答推荐

z—index总是 destruct 我的头,我该如何修复它?

如何删除在小屏幕上单击按钮时出现的蓝色方块?

如何在htmlAngular 17的@for中使用索引

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

Angular 分量被循环

未知高度正在应用于前标记

当光标悬停在(相同)父元素上方时,为多个子元素创建不同的过渡动画

每个元素的CSS网格高度相等,以保持响应性

如何保持嵌套进度条的背景色?

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

布局更改时的转换

多次使用组件时计数Angular 14中嵌套数组的指令

如何使元素具有粘性

我的 *ngFor 中的 array.reverse() 在生产中不起作用

在 laravel 中使用 DomPDF 将数据导出为 pdf 时,第二列被拉伸

第 117 行的块标记无效:endblock,应为empty或endfor.您是否忘记注册或加载此标签?

Angular:从服务器下载 HTML 并打印

如何将自定义图像插入 Shiny plot header?

我的卡片内容无法在 bootstrap 5 中正确居中