How to use buttons in HTML form to choose between several options?

4

I am designing a registration form and the section of choice of sex is not designed as a dropdown but as three buttons that when choosing one is active and I do not know how to do this in HTML and CSS.

I have tried to put a type button but it does not work, I get a button without text inside and you can only click without results.

<div class="infoPersonal">
<form class="formPersonal">
    <span class="textForm">Nombre:</span><br>
    <input class="boxName" type="text" name="nombreForm"><br>
    <span class="textForm">Fecha de nacimiento:</span>
    <span class="textForm">Población:</span>
    <input class="boxDate" type="date" name="fechaForm">
    <input class="boxCity" type="text" name="cityForm"> 
    <span class="textForm">Sexo:</span>
    <input class="boxMujer" type="button" name="mujerForm" placeholder="Mujer">
</form>

I enclose an image so that it is better understood.

Thanks!

    
asked by Alba 20.02.2018 в 14:27
source

2 answers

7

Using only HTML and CSS you could do it in the following way:

.formPersonal span{
  display: block;
  margin-bottom: 20px;
}

.formPersonal .labelsex{
  padding: 6px 30px;
  border-radius: 20px;
  border: 2px solid #ccc;
}

.boxSexo:checked + label{
  background: purple;
  color: white;
  border: 2px solid purple;
}

.boxSexo{
  display: none;
}
<div class="infoPersonal">
  <form class="formPersonal">
      <span class="textForm">Sexo:</span>
      
      <input class="boxSexo" type="radio" name="sexo" value="Mujer" id="mujerForm">
      <label class="labelsex activo" for="mujerForm">Mujer</label>
      
      <input class="boxSexo" type="radio" name="sexo" value="Hombre" id="hombreForm">
      <label class="labelsex" for="hombreForm">Hombre</label>
      
      <input class="boxSexo" type="radio" name="sexo" value="Otros" id="otrosForm">
      <label class="labelsex" for="otrosForm">Otros</label>   
  </form>
</div>

What we are doing is by means of CSS select the element that is immediately after the input checked and assigning a different style to the rest to give the impression that it is "active".

If you have any questions, do not hesitate to ask.

    
answered by 20.02.2018 / 14:52
source
2

I invite you to study a little more about the html form elements and explore each of the different types and their uses.

Although there is no option that automatically allows you to give the style you want, natively, if there are ways to simulate them, for example the item you are looking for is the input of "radio buttons" or radio in English. These allow you to associate a question with multiple answers, where there can only be a single answer unlike the type "check" that allows you to select more than one answer.

<h5>Inputs radio</h5>

<input name="asociacion" type="radio">
<input name="asociacion" type="radio">
<input name="asociacion" type="radio">
<input name="asociacion" type="radio">

<h5>Inputs Checkbox</h5>

<input name="asociacion" type="checkbox">
<input name="asociacion" type="checkbox">
<input name="asociacion" type="checkbox">
<input name="asociacion" type="checkbox">

The relationship and / or grouping between the different inputs , is done using the "name" attribute as you can see in the previous example.

Once this is understood, you should understand that there is a direct relationship between the label elements and the inputs, these are correlated using the for attribute in the case of the label and the attribute id for the inputs .

label{
  background: whitesmoke;
  padding: 0.5em 1em;
  cursor: pointer;
}

label:hover{
  background: gray;
  color: white;
}
<form>

<h5>Inputs radio</h5>

<input name="asociacion" type="radio" id="input_1">
<input name="asociacion" type="radio" id="input_2">
<input name="asociacion" type="radio" id="input_3">

</form>
<br><br>
<div>
  <label for="input_1">Input 1</label>
  <label for="input_2">Input 2</label>
  <label for="input_3">Input 3</label>
</div>

Already the rest would be the magic of css, for this, hide the radio inputs and only make the label visible . As you see it does not matter if the inputs or labels are not side by side, they can be separated or even nested, they will remain related. Now if we want the label style to look as if we had checked them (as in the image you share), the order is important, since the only way to relate them via CSS is to fulfill the following conditions:

  • The inputs and labels can not be nested in different html elements.
  • The inputs should not be nested, above the whole structure.
  • If the labels are consistent with the inputs, we can use the following syntax:

  • #input_1:checked ~ label[for="input_1"]{
      /*Estilos...*/
    }
    
  • If the labels are nested in an html element, you have to relate the parent and then the labels, something like this:

  • #input_1:checked ~ .padre_ancestro label[for="input_1"]{
      /*Estilos...*/
    }
    

    An example all together would be, something like this:

    .pregunta{
      padding: 1em;
    }
    
    input.esconder{
      position: fixed;
      opacity: 0;
    }
    
    /*Esto es para crear una excepción en el label que tenga la clase group*/ 
    /*Podría haber colocado los estilos del 3er ejemplo aquí, pero para que no te confundas, los separe más abajo*/
    label:not(.grupo){
      background: whitesmoke;
      padding: 0.5em 1em;
      cursor: pointer;
      border-radius: 3em;
    }
    
    label:not(.grupo):hover{
      background: gray;
      color: white;
    }
    
    /*Estilos para estructura 1*/
    #input_1:checked ~ div label[for="input_1"],
    #input_2:checked ~ div label[for="input_2"],
    #input_3:checked ~ div label[for="input_3"],
    /*Estilos para estructura 2*/
    #input_4:checked + label[for="input_4"],
    #input_5:checked + label[for="input_5"]{
      background: black;
      color: white;
    }
    
    /*Estilos para estructura 3*/
    label.grupo span{
      display: inline-button;
      background: whitesmoke;
      padding: 0.5em 1em;
      cursor: pointer;
      border-radius: 3em;
    }
    
    label.grupo:hover span{
      background: gray;
      color: white;
    }
    
    #input_6:checked + span,
    #input_7:checked + span{
      background: black;
      color: white;
    }
    <form>
        
        <fieldset class="pregunta uno">
    
        <legend>Estructura 1</legend>
    
          <input name="asociacion" type="radio" id="input_1" class="esconder">
          <input name="asociacion" type="radio" id="input_2" class="esconder">
          <input name="asociacion" type="radio" id="input_3" class="esconder">
    
          <div>
            <label for="input_1">Input 1</label>
            <label for="input_2">Input 2</label>
            <label for="input_3">Input 3</label>
          </div>
          
        </fieldset>
        <fieldset class="pregunta dos">
    
        <legend>Estructura 2</legend>
    
          <input name="asociacion" type="radio" id="input_4" class="esconder">
          <label for="input_4">Input 4</label>
    
          <input name="asociacion" type="radio" id="input_5" class="esconder">
          <label for="input_5">Input 5</label>
          
        </fieldset>
        <fieldset class="pregunta tres">
    
        <!--Esta 3ra estructura es para anidar o agrupar tanto el label como el input, en este caso el estilo que simula ser un botón se lo aplicamos al <span> que contendrá el <label> y que deberá estar consiguiente del <input type="radio">-->
    
        <legend>Estructura 3</legend>
    
        <label for="input_6" class="grupo">
          <input name="asociacion" type="radio" id="input_6" class="esconder">
          <span>Input 6</span>
        </label>
        
        <label for="input_7" class="grupo">
          <input name="asociacion" type="radio" id="input_7" class="esconder">
          <span>Input 7</span>
        </label>
          
        </fieldset>
    
    </form>

    If you have another question, leave me your comment. Successes!

        
    answered by 20.02.2018 в 15:38