-1

I want to align the radio button and their label in center and vertical but I am only able to the centre the alignment by using text-align: centre. I have tried every method but it is not working. I am including the code for reference.

        <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <link rel="stylesheet" href="radio_button.css" />
    </head>

    <body>
        <div style="text-align: center;">
            <form id="algorithm">
                <input type="radio" class="option-input radio" name="algorithm" value="fcfs" checked />
                <label>FCFS</label>
                <br>
                <input type="radio" class="option-input radio" name="algorithm" value="sjf" />
                <label>SJF</label><br>
                <input type="radio" class="option-input radio" name="algorithm" value="priority" /><label>HRNN</label><br>
                <input type="radio" class="option-input radio" name="algorithm" value="robin" /><label>ROUND ROBIN</label>
            </form>
        </div>

    </body>

    </html>

This above is the HTML file.

    @keyframes click-wave {
    0% {
      height: 40px;
      width: 40px;
      opacity: 0.35;
      position: relative;
    }
    100% {
      height: 200px;
      width: 200px;
      margin-left: -80px;
      margin-top: -80px;
      opacity: 0;
    }
  }
  
  .option-input {
    -webkit-appearance: none;
    -moz-appearance: none;
    -ms-appearance: none;
    -o-appearance: none;
    appearance: none;
    position: relative;
    top: 13.33333px;
    right: 0;
    bottom: 0;
    left: 0;
    height: 40px;
    width: 40px;
    transition: all 0.15s ease-out 0s;
    background: #cbd1d8;
    border: none;
    color: #fff;
    cursor: pointer;
    display: inline-block;
    margin-right: 0.5rem;
    outline: none;
    position: relative;
    z-index: 1000;
  }
  .option-input:hover {
    background: #9faab7;
  }
  .option-input:checked {
    background: #40e0d0;
  }
  .option-input:checked::before {
    position: absolute;
    display: inline-table;
    font-size: 40.66667px;
    line-height: 30px;
  }
  .option-input:checked::after {
    -webkit-animation: click-wave 0.65s;
    -moz-animation: click-wave 0.65s;
    animation: click-wave 0.65s;
    background: #40e0d0;
    content: '';
    display: block;
    z-index: 100;
  }
  .option-input.radio {
    border-radius: 50%;
  }
  .option-input.radio::after {
    border-radius: 50%;
  }
  /* MY ATTEMPT
  input[type="radio"] {
    margin-top: -1px;
    vertical-align: middle;
  }
  */

This I was able to achieve till now. enter image description here This the CSS file of the input radio button.

SE_net4 the downvoter
  • 21,043
  • 11
  • 69
  • 107

4 Answers4

1

You need to add define the form wrapper div as a container. In your html, change <div style="text-align: center;"> to

<div class="container">

And in your css, define container class as

.container{
  height: auto;
  width: 200px; 
/* set the width to whatever you want, basically */
  margin: auto;
  display: flex;
  justify-content: flex-start;
}
Adil Ahmed
  • 343
  • 1
  • 9
0

I did it by adding to the div container a defined height and flexbox with justify-content and align-items to center. You can remove 'text-align: center now', i think it will be better looking. I hope this is what you want.

@keyframes click-wave {
    0% {
      height: 40px;
      width: 40px;
      opacity: 0.35;
      position: relative;
    }
    100% {
      height: 200px;
      width: 200px;
      margin-left: -80px;
      margin-top: -80px;
      opacity: 0;
    }
  }
  
  div{
    height:100vh;
    display:flex;
    justify-content:center;
    align-items:center;
  }
  
  .option-input {
    -webkit-appearance: none;
    -moz-appearance: none;
    -ms-appearance: none;
    -o-appearance: none;
    appearance: none;
    position: relative;
    top: 13.33333px;
    right: 0;
    bottom: 0;
    left: 0;
    height: 40px;
    width: 40px;
    transition: all 0.15s ease-out 0s;
    background: #cbd1d8;
    border: none;
    color: #fff;
    cursor: pointer;
    display: inline-block;
    margin-right: 0.5rem;
    outline: none;
    position: relative;
    z-index: 1000;
  }
  .option-input:hover {
    background: #9faab7;
  }
  .option-input:checked {
    background: #40e0d0;
  }
  .option-input:checked::before {
    position: absolute;
    display: inline-table;
    font-size: 40.66667px;
    line-height: 30px;
  }
  .option-input:checked::after {
    -webkit-animation: click-wave 0.65s;
    -moz-animation: click-wave 0.65s;
    animation: click-wave 0.65s;
    background: #40e0d0;
    content: '';
    display: block;
    z-index: 100;
  }
  .option-input.radio {
    border-radius: 50%;
  }
  .option-input.radio::after {
    border-radius: 50%;
  }
  /* MY ATTEMPT
  input[type="radio"] {
    margin-top: -1px;
    vertical-align: middle;
  }
  */
    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <link rel="stylesheet" href="radio_button.css" />
    </head>

    <body>
        <div style="text-align: center;">
            <form id="algorithm">
                <input type="radio" class="option-input radio" name="algorithm" value="fcfs" checked />
                <label>FCFS</label>
                <br>
                <input type="radio" class="option-input radio" name="algorithm" value="sjf" />
                <label>SJF</label><br>
                <input type="radio" class="option-input radio" name="algorithm" value="priority" /><label>HRNN</label><br>
                <input type="radio" class="option-input radio" name="algorithm" value="robin" /><label>ROUND ROBIN</label>
            </form>
        </div>

    </body>

    </html>
Kev Rob
  • 322
  • 1
  • 7
0

I use flexbox in the algorithm to align the items, also i wrap the input and label in to div for flex to target children DOM properly.

 @keyframes click-wave {
    0% {
      height: 40px;
      width: 40px;
      opacity: 0.35;
      position: relative;
    }
    100% {
      height: 200px;
      width: 200px;
      margin-left: -80px;
      margin-top: -80px;
      opacity: 0;
    }
  }
  
  #algorithm {
    display: flex;
    flex-direction: column;
    width: 400px;
    margin: auto;
  }
  
  #algorithm > div {
    align-self: flex-start;
  }
  
  .option-input {
    -webkit-appearance: none;
    -moz-appearance: none;
    -ms-appearance: none;
    -o-appearance: none;
    appearance: none;
    position: relative;
    top: 13.33333px;
    right: 0;
    bottom: 0;
    left: 0;
    height: 40px;
    width: 40px;
    transition: all 0.15s ease-out 0s;
    background: #cbd1d8;
    border: none;
    color: #fff;
    cursor: pointer;
    display: inline-block;
    margin-right: 0.5rem;
    outline: none;
    position: relative;
    z-index: 1000;
  }
  .option-input:hover {
    background: #9faab7;
  }
  .option-input:checked {
    background: #40e0d0;
  }
  .option-input:checked::before {
    position: absolute;
    display: inline-table;
    font-size: 40.66667px;
    line-height: 30px;
  }
  .option-input:checked::after {
    -webkit-animation: click-wave 0.65s;
    -moz-animation: click-wave 0.65s;
    animation: click-wave 0.65s;
    background: #40e0d0;
    content: '';
    display: block;
    z-index: 100;
  }
  .option-input.radio {
    border-radius: 50%;
  }
  .option-input.radio::after {
    border-radius: 50%;
  }
    <!DOCTYPE html>
    <html lang="en">

    <head>
      <meta charset="UTF-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>Document</title>
      <link rel="stylesheet" href="radio_button.css" />
    </head>

    <body>
      <div style="text-align: center;">
        <form id="algorithm">
          <div>
            <input type="radio" class="option-input radio" name="algorithm" value="fcfs" checked />
            <label>FCFS</label>
          </div>
          <div>
            <input type="radio" class="option-input radio" name="algorithm" value="sjf" />
            <label>SJF</label>
          </div>
          <div>
            <input type="radio" class="option-input radio" name="algorithm" value="priority" /><label>HRNN</label>
          </div>
          <div>
            <input type="radio" class="option-input radio" name="algorithm" value="robin" /><label>ROUND ROBIN</label>
          </div>
        </form>
      </div>

    </body>

    </html>
Hamza Zaidi
  • 547
  • 4
  • 8
0

I think I have a solution. You must stylize #algorithm

#algorithm {
  display: inline-block;
  width: auto;
  text-align: left;
  margin: auto;
}

Is this what You expected?