【问题标题】:Positioning grid with flexbox使用 flexbox 定位网格
【发布时间】:2020-02-24 19:15:41
【问题描述】:

我正在尝试使用 flexbox定位项目列表,我已经用 comun 方式解决了这个问题,但我想改进我的代码以使用 更好的工具,比如 flexbox,我有这样的东西:

但我想用 Flexbox 来做到这一点,这样定位:

HTML

<div
  v-for="item in list"
  :key="item.id+Math.random()"
  class="neo-form-toggle-list__item neo-form-toggle neo-form-toggle--checkbox slideout--checkbox container--checkbox"
>
  <div class="checkbox--group">
    <input
      class="neo-form-toggle__field"
      type="checkbox"
      :id="item.id"
      name="rule"
      :checked="checked"
    />
    <label
      class="neo-form-toggle__label checkbox--label rule"
      :class="item.templateRule.title.length > 5 ? 'rule__big--title' : 'rule__small--title'"
      :for="item.id">
      {{ item.templateRule.title }}
    </label>
    <p class="item">{{ item.templateRule.description }}</p>
  </div>
</div>

我的 CSS 如下:

.neo-form-toggle__label {
  &.checkbox--label {
    text-transform: none;
    font-weight: normal;
    font-size: 12px;
    float: right;
    margin-right: 3%;
    &.rule {
      font-weight: bold;
      &__big--title {
        margin-right: 1rem;
      }
      &__small--title {
        margin-right: 5rem;
      }
    }
  }
}

.neo-form-toggle {
  &.neo-form-toggle--checkbox {
    &.checkbox--margin {
      margin-right: 2.25rem !important;
    }
  }
}

.neo-form-toggle {
  &.neo-form-toggle--checkbox {
    &.slideout--checkbox {
      width: 13rem;
    }
    &.checkbox--margin {
      margin-right: 2.25rem !important;
    }
    &.container--checkbox {
      margin-right: 5rem;
      margin-bottom: 0;
    }
  }
}

我开始学习 Flexbox,但我仍然不明白它是如何工作的,对改进这个 css 有什么帮助吗?

【问题讨论】:

  • 提供 HTML 会很有帮助,以便用户知道 CSS 适用于哪个 HTML。
  • 您没有使用 Grid,但对于这项工作,您可能应该使用(而不是 Flexbox)。
  • 我提供了更多有关问题的详细信息。

标签: html css vue.js flexbox grid


【解决方案1】:

我建议使用网格。请记住,flexbox 仅适用于一维位置(行或列,但不能同时用于两者)。网格(就像名字告诉你的那样)用于二维布局。您可以预定义行和列等。尤其是使用“grid-template-areas”,它可以非常容易地构建网格。

【讨论】:

    【解决方案2】:

    这是一个简单的例子:

    <head>
        <title>Parcel Sandbox</title>
        <meta charset="UTF-8" />
        <style>
          * {
            margin: 0;
            padding: 0;
          }
    
          .container {
            display: flex;
            width: 300px;
            padding: 5px;
          }
    
          .checkbox {
            margin: 2px 6px 0 0;
          }
    
          .labels {
            flex: 1;
          }
    
          .label__title {
            font-size: 14px;
          }
    
          .label__subtitle {
            font-size: 11px;
          }
        </style>
      </head>
    
      <body>
        <div class="container">
          <input type="checkbox" class="checkbox" />
          <div class="labels">
            <p class="label__title">Faturamento presumido</p>
            <p class="label__subtitle">Modelo atualizado mensalmente</p>
          </div>
        </div>
      </body>
    

    【讨论】:

      猜你喜欢
      • 2020-10-22
      • 2017-07-25
      • 2017-04-22
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 2023-02-09
      相关资源
      最近更新 更多