Styling checkboxes within a table cell in Bootstrap using CSS: Tips and Tricks

When using labels and spans with Bootstrap 4, the alignment may not appear centered when placed in a table within tr and td. How can I ensure that it is aligned properly?

span.bigcheck {
    font-family: sans-serif;
    font-weight: 500;
    font-size: 2em; 
}

span.bigcheck-target {
  font-family: FontAwesome; /* Using an icon font for the checkbox */
}

input[type='checkbox'].bigcheck {
  position: relative;
  left: -999em; /* Hiding the real checkbox */
}

input[type='checkbox'].bigcheck + span.bigcheck-target:after {
  content: "\f096"; /* Represents an open square (fa-square-o) in FontAwesome */
}

input[type='checkbox'].bigcheck:checked + span.bigcheck-target:after {
  content: "\f046"; /* Represents a checked box (fa-check-square-o) in FontAwesome*/
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<table class='table table-sm tbl1 table-hover borderless font_fake' style='width:auto'>
<thead>
<tr>
<th>
<span class="bigcheck">
<label class="bigcheck">
<input type="checkbox" class="bigcheck" id="selectAll" />
<span class="bigcheck-target"></span>
</label>
</span>
</th>
<th>TH TITLE</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<span class="bigcheck">
  <label class="bigcheck">
    <input type="checkbox" class="bigcheck" name="cheese" value="yes" />
    <span class="bigcheck-target"></span>
  </label>
</span>
</td>
<td>td desc 1</td>
</tr>
<tr>
<td>
<span class="bigcheck">
  <label class="bigcheck">
    <input type="checkbox" class="bigcheck" name="cheese" value="yes" />
    <span class="bigcheck-target"></span>
  </label>
</span>
</td>
<td>td desc 2</td>
</tr>
<tr>
<td>
<span class="bigcheck">
  <label class="bigcheck">
    <input type="checkbox" class="bigcheck" name="cheese" value="yes" />
    <span class="bigcheck-target"></span>
  </label>
</span>
</td>
<td>td desc 3</td>
</tr>
</tbody>
</table>

The code above shows that the checkbox does not align properly within the table cells (th, td). I have tried using margin:0 and padding:0 but the issue persists. Any suggestions on how to achieve proper alignment using Bootstrap 4 @label and Bootstrap 4 @span would be appreciated.

Thank you in advance.

Answer №1

I adjusted the vertical alignment of td and th elements from the default top to middle, and also set the height for checkboxes.

span.bigcheck {
       font-family: sans-serif;
    font-weight: 500;
    font-size: 2em;
    display: inline-block;
    height: 36px;
}

span.bigcheck-target {
  font-family: FontAwesome; /* Using an icon font for the checkbox */
}

input[type='checkbox'].bigcheck {
  position: relative;
  left: -999em; /* Hiding the actual checkbox */
}

input[type='checkbox'].bigcheck + span.bigcheck-target:after {
  content: "\f096"; /* Showing an open square icon (fa-square-o) in fontawesome */
}

input[type='checkbox'].bigcheck:checked + span.bigcheck-target:after {
  content: "\f046"; /* Displaying a checked box icon (fa-check-square-o) from fontawesome */
}
table tr td, table tr th{
   vertical-align: middle !important;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<table class='table table-sm tbl1 table-hover borderless font_fake' style='width:auto'>
<thead>
<tr>
<th>
<span class="bigcheck">
<label class="bigcheck">
<input type="checkbox" class="bigcheck" id="selectAll" />
<span class="bigcheck-target"></span>
</label>
</span>
</th>
<th>TH TITLE</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<span class="bigcheck">
  <label class="bigcheck">
    <input type="checkbox" class="bigcheck" name="cheese" value="yes" />
    <span class="bigcheck-target"></span>
  </label>
</span>
</td>
<td>Description 1</td>
</tr>
<tr>
<td>
<span class="bigcheck">
  <label class="bigcheck">
    <input type="checkbox" class="bigcheck" name="cheese" value="yes" />
    <span class="bigcheck-target"></span>
  </label>
</span>
</td>
<td>Description 2</td>
</tr>
<tr>
<td>
<span class="bigcheck">
  <label class="bigcheck">
    <input type="checkbox" class="bigcheck" name="cheese" value="yes" />
    <span class="bigcheck-target"></span>
  </label>
</span>
</td>
<td>Description 3</td>
</tr>
</tbody>
</table>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Change button text with JS on click

My goal is to update the text on a button click, changing it from 'Copy' to 'Copied!' as part of a custom clipboard implementation. The code I'm currently using is as follows: HTML: <button id="copyButton2">Copy</button& ...

When I incorporate a gradient, the background of my Bootstrap button flickers when clicked

I have implemented Bootstrap 4 with the following code snippet: <!-- Flickers on click --> <a href="javascript:void(0)" class="btn btn-primary">.btn .btn-primary</a> <!-- Does not flicker on click --> <a href="javascript:void(0 ...

Getting values from a textbox in a checkbox using PHP

I need to collect the values from 4 checkboxes, one of them is labeled as Others and has a textbox. The goal is to retrieve all the values that the user has checked and if the user has checked the option labeled as Others, then the values from the textbox ...

Using MDBootstrap for reactjs, incorporating a modal into a table for enhanced functionality

As a newcomer to the world of React.js and Material Design Bootstrap, I am attempting to load a dataset onto a table using a mock JSON file. After some trial and error, I managed to achieve a certain level of success with this task. My goal is to include a ...

Substitute using JQuery

Hello, I am attempting to update some html using Jquery, here is my current progress. $(".old").html($(".new").html()); It's almost working. The problem is that the .new content is being copied instead of replaced. I want to Cut/Paste instead of Co ...

Adding a button label value to a FormGroup in Angular

I've been working on a contact form that includes inputs and a dropdown selection. To handle the dropdown, I decided to use the ng-Bootstrap library which involves a button, dropdown menu, and dropdown items. However, I'm facing difficulties inte ...

unable to access various attributes in an angular directive

I have a particular HTML setup that effectively outlines each attribute's value through a distinct "attachment" directive. These values are located in the attrs list of said directive, which is within a modal file upload form. <p>For Testing Pu ...

I am curious about when the CSS of my website will start impacting its search engine ranking

Initially, my perception of CSS was that it was only used for styling the appearance of a document when viewed in a browser. However, I soon came to realize that search engines also utilize CSS for indexing web pages. It is said that search engines do not ...

How to insert a custom logo into a bootstrap navbar on a Ruby on Rails website

I have encountered a similar issue to this, but I haven't been able to resolve it. I am currently working on my first application using rails and bootstrap. I've successfully created a navbar with a name and two buttons (you can view the screensh ...

Can anyone guide me on repositioning material ui tabs to appear below my content and ensuring that the content expands to full height

Is there a way to position the Material UI tabs from Material UI tabs below the content area instead of above it? I am currently using them in my project and would like this specific layout. <md-tab-group> <md-tab label="Tab 1"> Co ...

How can the horizontal scroll bar width be adjusted? Is it possible to create a custom

Within my div, I have implemented multiple cards that scroll horizontally using the CSS property: overflow-x: scroll; This setup results in a horizontal scrollbar appearing under the div, which serves its purpose. However, I would prefer a customized scr ...

Trigger responsiveness issues with Bootstrap

Currently, I find myself in a somewhat awkward situation. I have developed a website using Bootstrap 4 (with Compass as a Ruby gem). However, due to ongoing discussions about its appearance on mobile devices, the client has requested that I revert the page ...

Utilizing JavaScript Modules to Improve Decoupling of DOM Elements

Currently, I am tackling portions of a complex JavaScript application that heavily involves DOM elements. My goal is to begin modularizing the code and decoupling it. While I have come across some helpful examples, one particular issue perplexes me: should ...

Tips for managing content size in the Bootstrap column system

I am facing an issue with my bootstrap grid setup. I have a card with an image inside a column, but the card does not occupy the full width of the column. How can I fix this? View current state of my column I've experimented with adjusting the width ...

Guide on creating a razor dropdown menu with a dynamic color scheme for the option list

Is there a way to style each option element below with alternate colors? <select asp-for="AssetCode" class="form-control" asp-items="@Model.AssetList"> </select> ...

Class name that changes the cursor to a pointer shape in CSS

My question is: Are there any CSS classes or attributes in Bootstrap 4 specifically designed for applying the pointer: cursor property to buttons or links? <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel= ...

Converting HTML to a Text String (not for display) using Angular

When it comes to displaying HTML in Angular, there are numerous approaches available. For example, using $sce.trustAsHtml(myHtmlVariable) is one way to achieve this. However, I am interested in creating something like the following: myStringVariable = s ...

The drop-down menu in the navbar is always positioned above any fixed elements on the page

I am facing an issue with a drop down menu in a nav-bar. The problem occurs when the drop down menu goes behind a fixed-position div below the nav-bar. I have tried using z-index but it doesn't seem to be working. Setting the position of the dropdown ...

Adding a new tab on button click with React js and Bootstrap 4 - a step-by-step guide

Is there a way to dynamically add a tab when a button is clicked? I've attempted various examples, but none of them have been successful for me. For instance: Initially, there are 2 bootstrap tabs named Tab1 and Tab2. There is also an "Add Tab" butt ...

Having trouble aligning elements in a single line using Bootstrap

I've been experimenting with different methods to align elements on the same line, such as using bootstrap and flexbox. However, I haven't been able to achieve the desired result. The goal is for the elements to stay on the same line even when th ...