使用 css 的动态响应表单

Dynamic Responsive Forms using css

我正在尝试以 table 格式创建包含多个 fields/values 的用户输入表单。如果它是桌面,它应该显示 6 列(每个字段 name/value 一对),如果它是 phone(纵向模式)应该只显示 2。如果它是横向显示 4 列。

预期输出:Phone(纵向)

+--------+---------+
+  Field + Value   +
+--------+---------+
+  Field + Value   +
+--------+---------+
+  Field + Value   +
+--------+---------+

Phone(横向模式)

+--------+---------+--------+---------+
+  Field + Value   +  Field + Value   +
+--------+---------+--------+---------+
+  Field + Value   +  Field + Value   +
+--------+---------+--------+---------+
+  Field + Value   +  Field + Value   +
+--------+---------+--------+---------+

iPad 或更大

+--------+---------+--------+---------+--------+---------+--------+---------+
+  Field + Value   +  Field + Value   +  Field + Value   +  Field + Value   +
+--------+---------+--------+---------+--------+---------+--------+---------+
+  Field + Value   +  Field + Value   +  Field + Value   +  Field + Value   +
+--------+---------+--------+---------+--------+---------+--------+---------+

我希望它是纯粹的 CSS(尽可能多),因为内容稍后将由 angular 构造函数填充。

但是,我没有得到正确的流程,因为 div 似乎粘在一起并且没有像一行一样干净地分开!可能我在这里遗漏了一些东西。它在 phone 纵向模式下工作正常,但正如您在下面的钢笔中看到的那样,到处都是。

码笔:Link

目前的代码如下:

            <div class='table-type'>
            <div class='row-type'>
                <div class='cell-type'>Account</div>
                <div class='cell-type'>Acme Incorporated US and Seychelles</div>
                <div class='cell-type'>Geography</div>
                <div class='cell-type'>North America</div>
                <div class='cell-type'>Premier</div>
                <div class='cell-type'>Yes</div>
                <div class='cell-type'>Countact Count</div>
                <div class='cell-type'>23</div>
                <div class='cell-type'>Account Manager</div>
                <div class='cell-type'>Dustin Brown</div>
                <div class='cell-type'>Customer Engineer</div>
                <div class='cell-type'>David Hoff</div>
                <div class='cell-type'>Exec Sponsor</div>
                <div class='cell-type'>Jeff Larabee</div>
            </div>
        </div>

现在 CSS:

    .table-type
{
    display: table;
    margin-bottom: 5px;
    width:100%;
    table-layout: fixed;
}
.row-type
{
    display: table-row;
}
.cell-type
{
    float: left;
    margin-top: 2px;
    margin-bottom: 2px;
    padding: 5px;
    line-height: 16px;
    font-family: 'Lato', sans-serif;
    border: 4px solid;
    border-color: #fff;
    border-radius: 15px;
    border-spacing: 15px 15px;
}
.cell-type:nth-child(odd)
{
    text-align: right;
}
.cell-type:nth-child(even)
{
    text-align:left;
    font-weight: bold;
    -webkit-border-radius:15px;-moz-border-radius:15px;
    background-color: #e3f2f2;
}
@media (max-width: 500px)
{
    .cell-type:nth-child(3n+3)
    {
        display: block;
    }
    .cell-type:nth-child(odd)
    {
        width:30%;
    }
    .cell-type:nth-child(even)
    {
        width:70%;
    }
}
@media (max-width: 700px) and (min-width: 501px)
{

    .cell-type:nth-child(odd)
    {
        width:20%;
    }
    .cell-type:nth-child(even)
    {
        width:30%;
    }
    .cell-type:nth-child(5n+5)
    {
        display: block;
    }
}
@media (min-width: 701px)
{
    .cell-type:nth-child(odd)
    {
        width:13%;
    }
    .cell-type:nth-child(even)
    {
        width:20%;
    }
    .cell-type:nth-child(7n+7)
    {
        display: block;
    }

}

我认为使用 flexbox:

会更容易达到预期的结果
.row-type {
  display: flex;
  flex-wrap: wrap;
}
.cell-type {
  flex: 0 0 10% 
  /* translates to: flex-grow: 0;
   *                flex-shrink: 0;
   *                flex-basis: 10%;
   */
}
.cell-type:nth-child(even) {
  flex-basis: 15%; 
  /* 10% + 15% = 25% */
}

但是您需要解决的第一个问题是将 box-sizing:border-box 应用于所有单元格。这将改变考虑填充和边框的方式,将其包含在 width 属性(或 flex-basis 用于 flexbox)中,因此您不必设置百分比和扣除填充和边框大小。

然后,只需根据父级宽度布置正确大小的单元格即可。为了做到这一点,选择一个方向(从大到小或从小到大)。我在下面从大到小挑选,但从小到大的作品也是如此。

@media 查询 之前的任何 @media 查询 之前,为您第一个选择的间隔编写规则(这很重要,因为您的 @media 查询将与外部查询具有相同的特异性,@media 内的规则将仅适用于它们稍后放置在 CSS) 中的事实。
记住: @media 查询不会改变特异性。他们只根据提供的条件设置里面的规则是否适用。 因此,在 @media 查询之前设置的规则仅在后续 @media 查询(覆盖它们)的 none 为真时适用。

然后,使用完全相同的选择器,在适当的 @media 规则内编写下一个间隔的规则,并继续直到到达最后一个 @media 规则。您只需要覆盖更改的 属性,在本例中为 flex-basis。在 tables 的情况下,它将是 width.

我也碰巧认为你的断点有点偏离,因为 700px500px 作为你的 table 的断点太低了(我决定将它们从1200px 时每行 8 到 6,950px 时每行 6 到 4,650px 时每行 4 到 2)。不同意也可以根据自己的需要和喜好随意调整。

这里是:

.table-type {
  margin-bottom: 5px;
  width: 100%;
}

.row-type {
  display: flex;
  flex-wrap: wrap;
  align-items: stretch; /* you might also try `center` here */
}

.cell-type {
  display: block;
  flex: 0 0 10%;
  margin: 3px 0;
  padding: 7px 12px;
  line-height: 16px;
  min-height: 46px; /* added for equal height cells. removed on mobile */
  font-family: 'Lato', sans-serif;
  border-radius: 16px;
  box-sizing: border-box;
}

.cell-type:nth-child(odd) {
  text-align: right;
}

.cell-type:nth-child(even) {
  flex-basis: 15%;
  font-weight: bold;
  background-color: #eaf5f5;
  box-shadow: inset 0 1px 3px rgba(0,0,0,.06), inset 0 1px 1px rgba(0,0,0,.04), inset 0 2px 1px rgba(0,0,0,.03)
}

@media(max-width: 1200px) {
  .cell-type:nth-child(odd) {
    flex-basis: 13.3333%;
  }
  .cell-type:nth-child(even) {
    flex-basis: 20%;
  }
}

@media(max-width: 950px) {
  .cell-type:nth-child(odd) {
    flex-basis: 20%;
  }
  .cell-type:nth-child(even) {
    flex-basis: 30%;
  }
}

@media(max-width: 650px) {
  .cell-type {
    min-height:0;
  }
  .cell-type:nth-child(odd) {
    flex-basis: 40%;
  }
  .cell-type:nth-child(even) {
    flex-basis: 60%;
  }
}
<div class='table-type'>
  <div class='row-type'>
    <div class='cell-type'>Account</div>
    <div class='cell-type'>Acme Incorporated US and Seychelles</div>
    <div class='cell-type'>Geography</div>
    <div class='cell-type'>North America</div>
    <div class='cell-type'>Premier</div>
    <div class='cell-type'>Yes</div>
    <div class='cell-type'>Countact Count</div>
    <div class='cell-type'>23</div>
    <div class='cell-type'>Account Manager</div>
    <div class='cell-type'>Dustin Brown</div>
    <div class='cell-type'>Customer Engineer</div>
    <div class='cell-type'>David Hoff</div>
    <div class='cell-type'>Exec Sponsor</div>
    <div class='cell-type'>Jeff Larabee</div>
  </div>
</div>


这也可以通过盒子模型实现(使用floats)。请注意 display:table(及其子元素的相关属性)和 box-model 不要一起玩 。我的意思是:要让细胞包裹在你想要的地方,你需要以下之一:

  • 实际上用 display:table-row 将单行的单元格包裹在一个不同的 DOM 元素中(这就是 table 模型的工作方式)。
  • 通过在行和单元格上设置 display:block 并在单元格上设置 float:left 来使用浮动(盒模型)。此技术还需要在正确的行尾单元格后进行清除,以避免单元格之间存在高度差异时单元格浮动在错误的位置。

这是你想要的吗?

.row-type
{
    display: grid;
    justify-items: center;
    align-items: center;
}
...
@media (max-width: 500px)
{ 
    .row-type{
      grid-template-columns: repeat(2,1fr);
    }
}
@media (max-width: 700px) and (min-width: 501px)
{
    .row-type{
      grid-template-columns: repeat(4,1fr);
    }
}
@media (min-width: 701px)
{   
    .row-type{
    grid-template-columns: repeat(8,1fr);
    }
}

https://codepen.io/smollet777/pen/mzypJo?editors=0100