How to make a radio button that is inside a table look like a buttton using bootstrap?

I can't solve a problem - yet - about bootstrap but I hope someone can give me some advice to solve it :)

I have a table where I display different options the user can choose from. I have use radio buttons to show the different options since I want only one of those options to be choose.

The problem that I have when using radio buttons is that I don't want the circle to show.

Here is what I have

What I have

Here is what I want what I want

I tried to put a div that contains a row of buttons to apply class="btn-group btn-group-toggle" data-toggle="buttons" but I don't get it to work. If I write that code in the <tr> tag, it work but the row above displace to the right of the screen.

Also, I tried to use css to hide the circle

input[type=radio]{
    visibility: hidden;
}

but the text inside the button is not centered

Thank you so much for your time and for the help!

Here is my code:

<table class="table table-responsive">
  <thead class="thead-light">
    <tr>
      <th scope="col">Zona Sísmica</th>
      <th scope="col">I</th>
      <th scope="col">II</th>
      <th scope="col">III</th>
      <th scope="col">IV</th>
      <th scope="col">V</th>
      <th scope="col">VI</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">Valor factor Z</th>
      <div class="btn-group btn-group-toggle" data-toggle="buttons">
        <td>
          <label class="btn btn-outline-primary">
             <input type="radio" name="z" id="Zop1" value="0.15"> 0.15                        
          </label>
        </td>
        <td>
          <label class="btn btn-outline-primary">
            <input type="radio" name="z" id="Zop2" value="0.25"> 0.25
          </label>
        </td>
        <td>
          <label class="btn btn-outline-primary">
            <input type="radio" name="z" id="Zop3" value="0.30"> 0.30
          </label>
        </td>
        <td>
          <label class="btn btn-outline-primary">
            <input type="radio" name="z" id="Zop4" value="0.35"> 0.35
          </label>
        </td>
        <td>
          <label class="btn btn-outline-primary">
            <input type="radio" name="z" id="Zop5" value="0.40"> 0.40
          </label>
        </td>
        <td>
          <label class="btn btn-outline-primary">
            <input type="radio" name="z" id="Zop6" value="0.50"> &ge;0.50
          </label>
        </td>
      </div>
    </tr>
  </tbody>
</table>

Upvotes: 2

Views: 1454

Answers (2)

Cat
Cat

Reputation: 4226

Here's a solution based on this tutorial.

You could definitely improve it by using some of the other suggestions they make (such as styling on :hover and :focus) -- which I recommend doing for aesthetic and accessibility reasons.

The radio buttons are hidden with opacity: 0 and width: 0, and the labels cover them because of position: fixed (which requires an ancestor with a position set to something other than static)

Because the label immediately follows the input element in the markup, it can be dynamically styled when the hidden input gets checked/unchecked via .better-radio:checked + label.

.better-radio {
  opacity: 0;
  position: fixed;
  width: 0;
}
label {
  display: inline-block;
  padding: 10px 20px;
  border: 1px solid black;
  border-radius: 4px;
  background-color: lightgrey;
}
.better-radio:checked + label {
  background-color: lightgreen;
  border-color: green;
}
<table class="table table-responsive">
  <thead class="thead-light">
    <tr>
      <th scope="col">Zona Sísmica</th>
      <th scope="col">I</th>
      <th scope="col">II</th>
      <th scope="col">III</th>
      <th scope="col">IV</th>
      <th scope="col">V</th>
      <th scope="col">VI</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">Valor factor Z</th>
      <div class="btn-group btn-group-toggle" data-toggle="buttons">
        <td>
          <input class="better-radio" type="radio" name="z" id="zop1" value="0.15" />
          <label for="zop1" class="btn btn-outline-primary">0.15</label>
        </td>
         <td>
          <input class="better-radio" type="radio" name="z" id="zop2" value="0.25" />
          <label for="zop2" class="btn btn-outline-primary">0.25</label>
        </td>
         <td>
          <input class="better-radio" type="radio" name="z" id="zop3" value="0.30" />
          <label for="zop3" class="btn btn-outline-primary">0.30</label>
        </td>
         <td>
          <input class="better-radio" type="radio" name="z" id="zop4" value="0.35" />
          <label for="zop4" class="btn btn-outline-primary">0.35</label>
        </td>
         <td>
          <input class="better-radio" type="radio" name="z" id="zop5" value="0.40" />
          <label for="zop5" class="btn btn-outline-primary">0.40</label>
        </td>
         <td>
          <input class="better-radio" type="radio" name="z" id="zop6" value="0.50" />
          <label for="zop6" class="btn btn-outline-primary">&ge;0.50</label>
        </td>
      </div>
    </tr>
  </tbody>
</table>

Upvotes: 1

Sameera Herath
Sameera Herath

Reputation: 201

Check this example, it works.

div label input {
   margin-right:100px;
}
body {
    font-family:sans-serif;
}

#ck-button {
    margin:4px;
    border-radius:4px;
    border:1px solid #D0D0D0;
    overflow:auto;
    float:left;
}

#ck-button:hover {
    border:1px solid red;
}

#ck-button label {
    float:left;
    width:4.0em;
}

#ck-button label span {
    text-align:center;
    padding:3px 0px;
    display:block;
}

#ck-button label input {
    position:absolute;
    top:-20px;
}

#ck-button input:checked + span {
  border: 1px solid blue;
}
<div id="ck-button">
  <label>
     <input type="radio" value="1"><span>red</span>
  </label>
</div>

Upvotes: 1

Related Questions