【问题标题】:Adaptive 3-columns layout with flexible height of the items without JS?没有 JS 的具有灵活高度的自适应 3 列布局?
【发布时间】:2021-03-21 21:37:12
【问题描述】:

我为整个网站制定了一个非常标准的布局——页眉/页脚、两个侧块和中心的主要内容。让我们把页脚/页眉放在一边,它们并不是很有趣。我想要实现的是两件事:

  1. 红色块放置在青色和绿色之间,在手机上进行自适应布局。
  2. 同时保持桌面布局灵活。也就是说,我不想在蓝色和红色块之间有额外的空间(当青色块有很多内容时),我也不希望在青色和绿色块之间有额外的空间(当蓝色块有很多内容时)

首先我是用 f​​lexbox 做的,但是第 1 点在 Flex 中是不可能的。现在我将其重写为使用 Grid 并遇到了 #2 的问题。

问题 - 如何使每列中的网格元素在高度方面相互“独立”(当然,保持确定性行为)?

这是示意图布局(顶部 - 桌面,底部 - 移动):

这里是CodePen example(单击洋红色块以检查它的行为方式和问题所在)

var testing = true;

function startTime() {
  if (testing) {

    document.getElementById('info').innerHTML = "<br>LongLongContent<br>LongLongContent<br><br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>";

    document.getElementById('content').innerHTML = "Short content (logs block should start just after me)";
  } else {

    document.getElementById('content').innerHTML = "<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>";
    document.getElementById('info').innerHTML = "Short content (photos block should start just after me)";
  }
  testing = !testing;
}
.grid-container {
  display: grid;
  grid-template-columns: 210px 1fr 200px;
  grid-template-rows: repeat(4, auto);
  gap: 0px 0px;
  grid-template-areas: "header header header" 
                       "info content sidebar"
                       "photos logs sidebar"
                       "footer footer footer";
}

.grid-container>* {
  border: 1px solid red;
}

.header {
  grid-area: header;
}

.info {
  grid-area: info;
  background-color: blue;
  color: white;
}

.photos {
  grid-area: photos;
  background-color: red;
}

.footer {
  grid-area: footer;
}

.sidebar {
  grid-area: sidebar;
  background-color: magenta;
}

.content {
  grid-area: content;
  background-color: cyan;
}

.logs {
  grid-area: logs;
  background-color: green;
}
<header class="grid-container">
  <header class="header">Header</header>
  <div class="info" id="info">Info<br>Info<br>Info<br>Info<br>Info<br>Info<br><br>Info<br></div>
  <div class="photos">Photos<br>Photos<br>Photos<br>Photos<br>Photos<br></div>
  <div class="content" id="content">content<br>asdasd<br><br><br>[I'm some random empty space =(]</div>
  <div class="logs">logs</div>
  <div class="sidebar" id="sidebar" onclick="startTime()">Click me to toggle between different sizes of content</div>
  <footer class="footer">Footer</footer>
  </div>

【问题讨论】:

    标签: html css


    【解决方案1】:

    以下代码显示了可变高度网格结构的实现。注意对应列的高度必须相同。

    使用grid-template-areas:,您可以实现所需的结构。

    注意grid-container类如何实现

    .grid-container {
      display: grid;
      grid-template-columns: 25% 50% 25%;
      grid-gap: 0px;
      background-color: black;
      grid-template-areas: 
                "one two three"
                "one four three"
                "five six three"
    }
    

    grid-template-columns: 25% 50% 25%; 定义了 3 列。

    然后用

     grid-template-areas:
                     "one two three"
                     "one four three"
                     "five six three"
    

    3 行由 3 列定义。

    我在响应模式下使用了以下代码

    @media only screen and (max-width: 600px) {
     .grid-container {
      display: grid;
      grid-template-columns: 100%;
      grid-gap: 10px;
      background-color: black;
      grid-template-areas: 
                "two"
                "four"
                "five"
                "six"
      }
    }
    

    完整代码:

    .grid-container {
      display: grid;
      grid-template-columns: 25% 50% 25%;
      grid-gap: 0px;
      background-color: black;
      grid-template-areas: "one two three" "one four three" "five six three"
    }
    
    #one {
      grid-area: one;
      height: auto;
      width: 100%;
      background-color: blue;
    }
    
    #two {
      grid-area: two;
      height: 50px;
      width: 100%;
      background-color: yellow;
    }
    
    #three {
      grid-area: three;
      height: auto;
      width: 100%;
      background-color: #365263;
    }
    
    #four {
      grid-area: four;
      height: auto;
      width: 100%;
      background-color: aqua;
    }
    
    #five {
      grid-area: five;
      height: auto;
      width: 100%;
      background-color: red;
    }
    
    #six {
      grid-area: six;
      height: auto;
      width: 100%;
      background-color: green;
    }
    
    .grid-container>div {
      text-align: center;
      padding: 20px 0;
      font-size: 30px;
    }
    
    @media only screen and (max-width: 600px) {
      .grid-container {
        display: grid;
        grid-template-columns: 100%;
        grid-gap: 10px;
        background-color: black;
        grid-template-areas: "two" "four" "five" "six"
      }
    }
    <h1>The grid-column Property</h1>
    
    
    
    <div class="grid-container">
      <div id="one">2<br>2<br>2<br>2<br>2<br>2<br>2<br>2<br>1</div>
      <div id="two"></div>
      <div id="three">3</div>
      <div id="four">4<br>4<br>4<br>4<br>4<br>4<br>4<br>4<br>4<br>4<br>4<br>4<br>4<br>4<br>4<br></div>
      <div id="five">5</div>
      <div id="six">6</div>
    </div>

    【讨论】:

    • 这对我没有帮助,因为您将多个 div 放在一个列容器中。这不适用于项目符号 #1。它看起来也与我建议的布局完全不同。
    • 我更改了代码以满足您的需求。你可以去看看。
    • 没有?这并不完全有效,列不灵活,甚至比我在问题 (bullet #2) codepen.io/lega4/pen/eYBqozP 中的代码更糟糕。请参考我最初的 codepen 示例并查看问题。
    • 我将高度改为自动。相应的列具有相同的高度。
    • ...您得到的示例与我的问题中的示例完全相同,但使用空格而不是动态高度...。正是我所质疑的...您是否真的阅读并理解了我的问题,在回答之前点击了我的初始代码笔?
    猜你喜欢
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多