Using flex to achieve two rows and one column layout [duplicate]

。_饼干妹妹 提交于 2020-02-23 07:35:44

问题


I'm trying to use flexbox to achieve a column layout with an element "floated" to the right.

Something like this:

Thing is, I cannot alter the HTML since it's an embedded form so trying to achieve this via pure CSS.

So far, I have this:

.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>

Edit:

I also need the submit button to sit under the textarea, but since the .submit-wrapper is a direct child of form, the only way I can see addressing this is by adding flex-direction: column; to form. However, this turns the whole layout into a column, which I do not want.


回答1:


You are apllying display: flex to the wrong elements and to more elements than you need to.

Check this: 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%
}


来源:https://stackoverflow.com/questions/53707051/using-flex-to-achieve-two-rows-and-one-column-layout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!