【问题标题】:CSS column responsive designCSS 列响应式设计
【发布时间】:2021-10-01 18:38:57
【问题描述】:

我有一个侧边栏,我需要根据视口做出响应。这是它的外观

我想知道是否有一种方法可以编写一段代码和一些 css,而不必为每个设备编写不同的 html。

这是我迄今为止为桌面视图编写的内容

<div class="mycontainer">
    <div>
        <p>A</p>
        <p>B</p>
    </div>
        <div>C</div>
        <div>D</div>
    </div>
    <div>E</div>
    <div>F</div>
    <div>
        <p>G</p>
        <p>H</p>
    </div>
</div>

CSS:

.mycontainer{
  width:194px; 
  height:291px; 
  border-radius: 8px; 
  box-shadow: 0px 3px 6px #00000029; 
  margin-left: 30px;
  background: #FFF; 
  z-index:100;
}
.mycontainer div{
  border-bottom:  1px solid #E5E5E5;
  text-align: center;
}

.mycontainer div:first-child, .mycontainer div:nth-child(2){
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 10px 0;
}

.mycontainer div:nth-child(3), .mycontainer div:nth-child(4){
  display: flex;
  flex-direction: row;
  align-items: center;
  
}
.mycontainer div:nth-child(5){
  border:  none;
}

任何帮助将不胜感激

【问题讨论】:

  • 希望您至少尝试自己编写代码。我建议你做一些additional research,无论是通过谷歌还是通过搜索,尝试一下。如果您仍然遇到问题,请返回您的代码并解释您尝试过的操作。
  • 为什么 a,b,g,h 在段落中,而 c,d,e,f 在 div 中?此外,a,b 是段落。你的桌子上没有分隔。 c,d 是 div。他们也没有分离。重点是什么? e,f 是您在表格中将它们分开的 div。非常不清楚伙计。
  • 另外,CSS 中的每个元素都是白色背景色。看不到两个 div 之间的区别。请先更正您的帖子。

标签: html css flexbox responsive-design


【解决方案1】:

其中一种方法是使用媒体查询和 CSS 网格

.mycontainer{
  width:194px; 
  height:291px; 
  border-radius: 8px; 
  box-shadow: 0px 3px 6px #00000029; 
  margin-left: 30px;
  background: #FFF; 
  z-index:100;
  display: grid;
  justify-items: center;
  align-items: center;
}
.ef {
  justify-self: left;
}
@media (max-width:801px) { 
  .mycontainer{
    grid-template-columns: auto auto;
  }
  .ab, .cd, .ef, .gh {
    display: flex;
  }
  .ef {
    justify-self: center;
  }
}
@media (max-width:481px)  { 
  .mycontainer{
    grid-template-columns: auto auto;
  }
  .ab {
    display: grid;
  }
  .cd, .gh {
    display: flex;
  }
  .ef {
    justify-self: center;
    grid-area: 2 / 1 / 3 / 3;
  }
  .gh {
     grid-area: 3 / 1 / 4 / 3;
  }
}
<div class="mycontainer">
    <div class="ab">
      <p>A</p>
      <p>B</p>
    </div>
    <div class="cd">
      <div>C</div>
      <div>D</div>
    </div>
    <div class="ef">
      <div>E</div>
      <div>F</div>
    </div>
    <div class="gh">
      <p>G</p>
      <p>H</p>
    </div>
</div>

【讨论】:

  • 印象深刻。我没有意识到网格可以解决问题。只有一个小问题。在电话视图中,我需要 EF 在一行,而 GH 在另一行。使用您的实际代码,我将它们排成一行。我一直在尝试解决这个问题,但无济于事。你知道这是怎么做到的吗?谢谢
  • 带网格区域,立即尝试
  • 非常感谢。多亏了你,我才学会了网格
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-22
  • 1970-01-01
  • 2018-08-02
  • 2016-12-09
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
相关资源
最近更新 更多