How do I align my radio buttons horizontally on a table?

32 Views Asked by At

How do I align my radio buttons in a. 1 to "E", "VG", "G", "F", and "P"?

It appears vertically and I want it horizontally. Here's the picture of what I'm working on below.

problem

I tried adjusting it by putting more <td> as a spacer but it's the wrong way.

body {
  font-family: Arial, sans-serif;
  background-color: #f0f0f0;
  padding: 20px;
}

table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
  background-color: #fff;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

th,
td {
  border: 1px solid #ccc;
  padding: 5px;
  text-align: center;
}

th {
  background-color: #f2f2f2;
}

tr:nth-child(even) {
  background-color: #f9f9f9;
}

.radio-container {
  display: flex;
  justify-content: center;
  align-items: center;
}

.radio-container input[type="radio"] {
  cursor: pointer;
  margin: 0 5px;
}
<center>
  <h2>|5 - Excellent||4 - Very Good||3 - Good||2- Fair||1 - Poor|</center>

<table>
  <tr>
    <th>CRITERIA</th>
    <th>E<br>5</th>
    <th>VG<br>4</th>
    <th>G<br>3</th>
    <th>F<br>2</th>
    <th>P<br>1</th>
  </tr>
  <tr>
    <th>1. INSTRUCTION</th>
    <th colspan="5"></th>
  </tr>
  <tr>
    <th>a. Mastery of the Subject Matter</th>
    <th colspan="5"></th>
  </tr>
  <tr>
    <td>a.1 Explains the lesson clearly.</td>
    <td class="radio-container">
      <label for="a1_5">5</label>
      <input type="radio" id="a1_5" name="a1" value="5" onclick="rate('E', 5)">
    </td>
    <td class="radio-container">
      <label for="a1_4">4</label>
      <input type="radio" id="a1_4" name="a1" value="4" onclick="rate('VG', 4)">
    </td>
    <td class="radio-container">
      <label for="a1_3">3</label>
      <input type="radio" id="a1_3" name="a1" value="3" onclick="rate('G', 3)">
    </td>
    <td class="radio-container">
      <label for="a1_2">2</label>
      <input type="radio" id="a1_2" name="a1" value="2" onclick="rate('F', 2)">
    </td>
    <td class="radio-container">
      <label for="a1_1">1</label>
      <input type="radio" id="a1_1" name="a1" value="1" onclick="rate('P', 1)">
    </td>
  </tr>
  <tr>
    <td>a.2 Relates the subjects/topics to local/global developments whenever feasible and fitting.</td>
    <td class="radio-container">
      <label for="a2_5">5</label>
      <input type="radio" id="a2_5" name="a2" value="5" onclick="rate('E', 5)">
    </td>
    <td class="radio-container">
      <label for="a2_4">4</label>
      <input type="radio" id="a2_4" name="a2" value="4" onclick="rate('VG', 4)">
    </td>

1

There are 1 best solutions below

0
Guillaume Rey On

you can just remove display: flex on your .radio-container class.

But, if you want to keep the horizontal alignment on your radio to display the number beside the radio, you can nest your labels and your radios in a div :

    <td class="radio-container">
      <div class="flex">
         <label for="a1_4">4</label>
         <input type="radio" id="a1_4" name="a1" value="4" onclick="rate('VG', 4)">
      </div>
    </td>
    <td class="radio-container">
      <div class="flex">
         <label for="a1_3">3</label>
         <input type="radio" id="a1_3" name="a1" value="3" onclick="rate('G', 3)">
      </div>
    </td>
    <td class="radio-container">
      <div class="flex">
         <label for="a1_2">2</label>
         <input type="radio" id="a1_2" name="a1" value="2" onclick="rate('F', 2)">
      </div>
    </td>

and add this style to your divs :

.flex {
    display: flex;
    justify-content: center;
    align-items: center;
}