使用flex实现两行一列的布局

时间:2018-12-10 13:47:52

标签: html css flexbox

我正在尝试使用flexbox来实现右侧为“浮动”元素的列布局。

类似的东西

enter image description here

是的,我无法更改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>

修改:

我还需要提交按钮来坐在文本区域下,但是由于.submit-wrapperform的直接子代,因此我看到的唯一解决方法是通过将flex-direction: column;添加到form。但是,这会将整个布局变成一列,我不想。

1 个答案:

答案 0 :(得分:2)

您将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%
}