Align side-by-side fields in a form

Asked

Viewed 16,590 times

2

I’m having trouble aligning 2 fields side by side in a form, the first case would be a select next to an input and, the other, two inputs.

Ex:

inserir a descrição da imagem aqui

HTML:

<li>
    <label>Selecione algo</label>
    <select>
        <option disabled>Selecione Algo</option>
    </select>

    <label>Escreva algo</label>
    <input type="text"/>
</li>

<li>
    <input type="text"/>
    <div id="checkbox">
        <label> Marque algo
            <input type="checkbox"/> Marcado
        </label>
    </div>
</li>

3 answers

2

Well, if that’s what I understood, if you want to align the text with the check box, you can’t close the tag on the text and then put the checkbox, you have to put the checkbox inside the text tag, and then close it... so they’re aligned. Test like this:

<li>
<label>Selecione algo</label>
<select>
<option disabled>Selecione Algo</option>


<label>Escreva algo</label>
<input type="text"/></select>
</li>

<li>
<label>Escreva algo</label>
<input type="text"
<div id="checkbox">
<label> Marque algo
<input type="checkbox"/></input>
</label>
</div>
</li>

Then just do the rest with CSS, if that’s what you want, there’s not much secret.

2

HTML

<form class="form">
  <div class="section">
    <div class="control-group">
      <label class="control-label">Selecione algo</label>
      <select class="control">
            <option disabled>Selecione Algo</option>
      </select>
    </div>

    <div class="control-group">
      <label class="control-label">Escreva algo</label>
      <input type="text" class="control"/>
    </div>

  </div>

  <div class="section">
    <div class="control-group">
      <label class="control-label">Escreva algo</label>
      <input type="text" class="control"/>
    </div>

    <div class="control-group">
      <label class="control-label">Marque algo</label>
      <label>
        <input type="checkbox"/>
        Concordo (checkbox)
      </label>
    </div>

  </div>
</form>

CSS

.form {
  display: flex;
  width: 550px;
  background-color: black;
  color: #CCC;
}

.form .section {
  flex-grow: 1;
  flex-direction: column;
  padding: 30px;
}

.form .section .control-group {
  margin-top: 10px;
}

.form .section .control-group .control {
  width: 100%;
}


.form .section .control-group .control-label {
  display: block;
  width: 100%;
  margin-bottom: 4px;
}

jsfiddle result: https://jsfiddle.net/Henrique_Araujo/kkhr5d59/

  • I moved the <form> of place because inside has some Ivs and other fields and it seems that defaced everything, I know very little of css, but I saw something new in this code, display: flex?

  • @NGTHM4R3 display: flex, helps a lot in alignment. I suggest you google for "flex box"! d

1

I did something like this, which is very close to what you want. Look if it helps you.

#formulario {
  background-color: #111;
  width: 60%; 
  margin-left: 20%;
  padding: 45px 15px;
  height: auto;
}

.campo {
  width: 50%;
  float: left;
  color: #c0c0c0;
}

.campo input {
  margin: 10px 1%;
  padding: 8px 1%;
  width: 90%;
}

.campo select {
  margin: 10px 1%;
  padding: 8px 1%;
  width: 94%;
}
<form id="formulario">

<div class="campo">
  <label>Selecione algo</label>
  <select value="Selecione(select)">
      <option disabled>Selecione Algo</option>
  </select>
</div>

<div class="campo">
    <label>Escreva algo</label>
    <input type="text" placeholder="Escreva algo (input)"/>
</div>

<div class="campo">
    <input type="text"  placeholder="Escreva algo (input)"/>
</div>

<div id="checkbox" class="campo2">
    <label> Marque algo</label>
    <input type="checkbox"> Marcado  
</div>

</form>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.