我已经为我的博客网站创建了一个简单的网格系统,在那里我希望所有的框或div,除了第一个将是水平滚动在小屏幕(我的意思是780px).我怎么才能做到这一点?我试了很多方法,但都不行.

我想要的是:

这就是我现在拥有的:

In Large Screen (Currently Have)

这就是我在小屏幕上想要的:

In Small Screen what I want

以下是我的代码:

.parent {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  grid-column-gap: 0px;
  grid-row-gap: 0px;
}

.div {
  padding: 35px;
  background: dodgerblue;
  margin: 5px;
  text-align: center;
  color: #fff;
}

.parent .div:nth-child(1) {
  grid-area: 1 / 1 / 3 / 3;
}

.parent .div:nth-child(2) {
  grid-area: 1 / 3 / 2 / 4;
}

.parent .div:nth-child(3) {
  grid-area: 2 / 3 / 3 / 4;
}

.parent .div:nth-child(4) {
  grid-area: 3 / 3 / 4 / 4;
}

.parent .div:nth-child(5) {
  grid-area: 3 / 2 / 4 / 3;
}

.parent .div:nth-child(6) {
  grid-area: 3 / 1 / 4 / 2;
}
<div class="parent">
  <div class="div">1</div>
  <div class="div">2</div>
  <div class="div">3</div>
  <div class="div">4</div>
  <div class="div">5</div>
  <div class="div">6</div>
</div>

请谁来帮帮我.

推荐答案

获得这种滚动行为是一个挑战,但我认为这里的东西非常接近你想要的,假设你有6个项目.注意,我将容器查询设置为500px以适应代码片段,但您可以将其设置为780px.

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

.parent {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-auto-rows: 100px;
  gap: 5px;
  overflow-x: auto;
}

.parent::before {
  content: "";
  grid-column: span 5;
  grid-row: span 2;
}

.child {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  background-color: dodgerblue;
  min-width: 200px;
}

.child:first-child {
  width: 100cqi;
  height: 205px;
  position: fixed;
}

@container (min-width: 500px) {
  .parent {
    grid-template-columns: repeat(3, 1fr);
  }
  
  .parent::before {
    display: none;
  }
  
  .child:first-child {
    position: static;
    width: auto;
    height: auto;
    grid-area: 1 / 1 / 3 / 3;
  }
}
<div class="container">
  <div class="parent">
    <div class="child">1</div>
    <div class="child">2</div>
    <div class="child">3</div>
    <div class="child">4</div>
    <div class="child">5</div>
    <div class="child">6</div>
  </div>
</div>

<hr />

<div class="container" style="max-width: 400px;">
  <div class="parent">
    <div class="child">1</div>
    <div class="child">2</div>
    <div class="child">3</div>
    <div class="child">4</div>
    <div class="child">5</div>
    <div class="child">6</div>
  </div>
</div>

这里使用的 idea :

  • 在小容器中时,position: fixed可防止第一个项目与所有其他项目一起滚动.
  • .parent::before用于有效地"模拟"第一个元素,因为它现在有固定的位置.
  • 在大模式中,您只需要在第一个元素上使用grid-area: 1 / 1 / 3 / 3,其他元素将填充其余的插槽.
  • Container Queries,因此100cqi是容器大小的Container Queries%,而不是实际网格大小的Container Queries%,因此第一个元素不需要滚动.注意,这是一个相对较新的功能,但它可能(尽管不可靠)使用常规媒体查询和vw.

Html相关问答推荐

一次悬停可触发同一分区中的另一次悬停

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

如何打破长URL,包含连字符在HTML与CSS

隐藏滚动条和禁用文本 Select 的CSS技术?

由于列中的图像,它们不会随FlexBox中的窗口大小进行调整

将网格包装在css中

Div内容防止同级大小增加

为什么在添加文本时网格区域会调整大小?

使用HTML进行DAX测量

浮动Div在CSS中未按预期工作

如何在css中在span中绘制圆弧?

为什么我的网页显示网格区域彼此相邻,而不是我设置它们的方式?

如何保持块扩展以填充视口?

如何在 VS Code 中 Select 和删除 HTML 元素(其标签和内容)?

发送带有图像的Google文档作为HTML格式的邮件正文不再起作用

如何垂直对齐列表中的图像和文本?

将图像的高度限制为固定的纵横比,并在超过时使用 object-fit

th 内的 div 标签没有占据全高

防止按钮溢出到新行

CSS:如何在模糊的背景上剪切文本?