【问题标题】:Using flex to achieve two rows and one column layout [duplicate]使用flex实现两行一列布局[重复]
【发布时间】:2018-12-10 22:23:07
【问题描述】:

我正在尝试使用 flexbox 来实现列布局,其中元素“浮动”到右侧。

类似这样的

问题是,我无法更改 HTML,因为它是嵌入式表单,因此尝试通过纯 CSS 来实现。

到目前为止,我有这个:

.form {
  display: flex;
  flex-direction: column;
  text-align: center;
}

.form-container {
  display: flex;
  flex-direction: column;
  text-align: center;
  justify-content: space-between;
}

.form-form {
  width: 100%;
}

form {
  display: flex;
  justify-content: center;
}

fieldset {
  border: 0;
}

textarea {
  resize: none;
}

.form-columns-2 {
  display: flex;
}

.form-columns-2 input:first-child {
  margin-bottom: .5em
}

.form-columns-1 textarea {
  height: 100%
}
.submit-wrapper{
  flex-direction:column;
}
<div class="form">
  <div class="form-container">
    <div class="form-form">
      <form>
        <fieldset class="form-columns-2">
          <input type="text" placeholder="test">
          <input type="text" placeholder="test">
        </fieldset>
        <fieldset class="form-columns-2">
          <input type="text" placeholder="test">
          <input type="text" placeholder="test">
        </fieldset>
        <fieldset class="form-columns-1">
          <textarea placeholder="test">Text</textarea>
        </fieldset>
        <div class="submit-wrapper">
          <div class="actions">
            <input type="submit" value="Submit">
          </div>
        </div>
      </form>
    </div>
  </div>
</div>

编辑:

我还需要提交按钮来位于 textarea,但由于.submit-wrapperform 的直接子级,我可以看到解决此问题的唯一方法是添加@987654327 @到form。但是,这会将整个布局变成一列,这是我不想要的。

【问题讨论】:

  • 必须是flexbox吗?你试过在这里使用 display: grid 吗?
  • 另请注意,flexbox 和 fieldset 玩得不好

标签: html css flexbox


【解决方案1】:

您将display: flex 应用于错误的元素,并且应用于比您需要的更多的元素。

检查这个:https://codepen.io/anon/pen/dwPoEP

* {
  box-sizing: border-box;
}
fieldset{
  border:0;
}
textarea{
  resize:none;
}

form { // you want the form to be a flex box, not everything!
  display: flex;
  justify-content: center;
}
.form-columns-2 {
  display: flex; //you can use other options here, this works but you can use display: block on the inputs for example
}

// this 2 are just to make it look like the image
.form-columns-2 input:first-child {
  margin-bottom: .5em
}
.form-columns-1 textarea {
  height: 100%
}

【讨论】:

  • 啊,干杯!后续问题,我试图让提交按钮位于文本区域下方。我已经更新了我的原始帖子以显示最新的代码和查询。如果你能看一下,那会很酷吗?
  • 嗯,有了这个新要求,我将使用网格布局,例如 display: grid; grid-template-areas: 'inputs1 inputs2 textarea' 'inputs1 inputs2 submit';,然后您可以使用 .submit-wrapper {gird-area: submit} .form-columns-1 {grid-area: textarea} 定位项目。
猜你喜欢
  • 2021-04-22
  • 2020-08-13
  • 1970-01-01
  • 2020-01-03
  • 2016-05-05
  • 2016-07-19
  • 2019-02-10
  • 2021-03-08
  • 2023-01-19
相关资源
最近更新 更多