How to align text vertically in a cell alongside a button in another cell using Bootstrap

In my table, I have 3 columns of text and a fourth column with a button. The alignment of the text in the cells does not match the button text vertically due to the button having a vertical-align value of 'middle'. To align them properly, I currently wrap the text cells in a span and add an align-middle class. However, this method seems to introduce unnecessary html bloat. Is there another solution?

Below is an example of a table where some cells align while others do not, using an extra span element.

I am using Bootstrap 5, but I believe this issue applies to previous versions as well.

Edit I have tried different approaches:

  1. No vertical alignment classes added to the table.
  2. Vertical alignment specified on the td elements.
  3. Vertical alignment specified on the table itself.
  4. Vertical alignment specified on a span within the td element.

#2 and #3 produce similar results, however, when cells wrap and increase row height, the text in unwrapped cells centers vertically within the cell.

#4, when cells wrap and increase row height, the text in non-wrapping cells remains close to the top and aligns better with the button text, especially with certain styles applied like table-sm and btn-sm. However, if buttons are of different sizes, the alignment may be off.

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1a7875756e696e687b6a5a2f342a3428">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<span>No classes</span>
<table class="table table-sm table-striped table-hover table-borderless table-stacked-md label-20 mt-2" id="drugSearchList">
  <thead>
<tr class="my-2">
  <th scope="col">Drug Name</th>
  <th scope="col">Number</th>
  <th scope="col" />
</tr>
  </thead>
  <tbody>
<tr>
  <td>wrapping text here this is a test wrapping text here this is a test</td>
  <td>1</td>
  <td class="text-end"><a class="btn btn-sm btn-outline-primary" href="#">Edit</a></td>
</tr>
<tr class="border-bottom">
  <td>wrapping text here this is a test wrapping text here this is a test</td>
  <td>1</td>
  <td class="text-end"><a class="btn btn-sm btn-outline-dark disabled">Edit</a></td>
</tr>
  </tbody>
</table>

<span>Align-middle on td</span>
<table class="table table-sm table-striped table-hover table-borderless table-stacked-md label-20 mt-2" id="drugSearchList">
  <thead>
<tr class="my-2">
  <th scope="col">Drug Name</th>
  <th scope="col">Number</th>
  <th scope="col" />
</tr>
  </thead>
  <tbody>
<tr>
  <td class="align-middle">wrapping text here this is a test wrapping text here this is a test</td>
  <td class="align-middle">1</td>
  <td class="text-end"><a class="btn btn-sm btn-outline-primary" href="#">Edit</a></td>
</tr>
<tr class="border-bottom">
  <td class="align-middle">wrapping text here this is a test wrapping text here this is a test</td>
  <td class="align-middle">1</td>
  <td class="text-end"><a class="btn btn-sm btn-outline-dark disabled">Edit</a></td>
</tr>
  </tbody>
</table>

<span>Align-middle on table</span>
<table class="table table-sm table-striped table-hover table-borderless table-stacked-md label-20 mt-2 align-middle" id="drugSearchList">
  <thead>
<tr class="my-2">
  <th scope="col">Drug Name</th>
  <th scope="col">Number</th>
  <th scope="col" />
</tr>
  </thead>
  <tbody>
<tr>
  <td>wrapping text here this is a test wrapping text here this is a test</td>
  <td>1</td>
  <td class="text-end"><a class="btn btn-sm btn-outline-primary" href="#">Edit</a></td>
</tr>
<tr class="border-bottom">
  <td>wrapping text here this is a test wrapping text here this is a test</td>
  <td>1</td>
  <td class="text-end"><a class="btn btn-sm btn-outline-dark disabled">Edit</a></td>
</tr>
  </tbody>
</table>

<span>Align-middle on td span</span>
<table class="table table-sm table-striped table-hover table-borderless table-stacked-md label-20 mt-2" id="drugSearchList">
  <thead>
<tr class="my-2">
  <th scope="col">Drug Name</th>
  <th scope="col">Number</th>
  <th scope="col" />
</tr>
  </thead>
  <tbody>
<tr>
  <td><span class="align-middle">wrapping text here this is a test wrapping text here this is a test</span></td>
  <td><span class="align-middle">1</span></td>
  <td class="text-end"><a class="btn btn-sm btn-outline-primary" href="#">Edit</a></td>
</tr>
<tr class="border-bottom">
  <td><span class="align-middle">wrapping text here this is a test wrapping text here this is a test</td>
  <td><span class="align-middle">1</span></td>
  <td class="text-end" class="align-middle"><a class="btn btn-sm btn-outline-dark disabled">Edit</a></td>
</tr>
  </tbody>
</table>

Answer №1

To achieve vertical alignment, it is recommended to apply the "align-middle" class directly on the "td" element instead of using a "span" with ".align-middle". This helps simplify the structure and styling.

  • A visual clue for alignment can be added by including a bottom border to the last "tr" in the table.

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="482a27272c2b2c2a392e0927687341617d63">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<table class="table table-sm table-striped table-hover table-borderless table-stacked-md label-20 mt-2" id="drugSearchList">
  <thead>
    <tr class="my-2">
      <th scope="col">Date</th>
      <th scope="col">Drug Name</th>
      <th scope="col">Number</th>
      <th scope="col" />
    </tr>
  </thead>
  <tbody>
    <tr>
      <td data-col="Date" class="align-middle">12/14/2022</td>
      <td data-col="Drug" class="align-middle">hydroxyurea</td>
      <td data-col="Number" class="align-middle">1</td>
      <td class="text-end" class="align-middle">
        <a class="btn btn-sm btn-outline-primary" href="#">Edit</a>
      </td>
    </tr>
    <tr class="border-bottom">
      <td data-col="Date" class="align-middle">12/14/2022</td>
      <td data-col="Drug" class="align-middle">doxorubicin</td>
      <td data-col="Number" class="align-middle">1</td>
      <td class="text-end" class="align-middle">
        <span class="btn btn-sm btn-outline-dark disabled">Edit</span>
      </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

Trigger keydown and click events to dynamically update images in Internet Explorer 7

There is a next button and an input field where users can enter the page number to jump to a specific page. Each page is represented by an image, like: <img src="http://someurl.com/1_1.emf" > // first page <img src="http://someurl.com/2_1.emf" ...

Javascript Steps to trigger a function when selecting the Stay on Page option in Google Chrome

If you're testing my code in the Chrome Browser, hitting refresh will prompt you with 2 options. Leave This Page Stay on This Page By clicking on the Stay on this page button, it should trigger my custom function displayMsg(). Can anyone pr ...

Tips for customizing the background of form inputs in React-Bootstrap

Currently, I have a next.js project with react-bootstrap. I am attempting to change the default blueish gray background color (#e8f0fe or rgb(232, 240, 254)) that bootstrap uses for form inputs to white. To achieve this, I am customizing the bootstrap var ...

Using Angular 2 to trigger an event when a native DOM element is loaded

I am working towards my main objective of having a textarea element automatically focused upon creation. I recently came up with an idea to use e.target.focus() on the onload event. It would look something like this: <textarea rows="8" col="60" (load)= ...

Is there a way for me to modify the position of a cursor graphic?

Similar Question: Custom cursor interaction point - CSS / JQuery I've been experimenting with changing the image of the mouse pointer by using the CSS cursor property ("cursor: url(images/target.png), crosshair;") and have observed that when a us ...

Unable to convert cursor to jpg format

I am facing an issue where I have a jpg file stored in the same folder as my styles.css, and I want to change the cursor on a webpage to this particular jpg file. Despite my efforts, it seems like the cursor is not changing as expected. Currently, I am us ...

When initializing a new instance of Stripe using cartalyst/stripe-laravel, an error occurs stating "The Stripe API key has not been properly defined!"

I am eager to explore Stripe and learn how to process payments. Currently, I am utilizing the cartalyst/stripe-laravel package to create a new instance of Stripe following the instructions provided here. The code snippet should resemble this: $stripe = ...

The click event listener only seems to fire on the second attempt

This block of code is designed to function as a search algorithm that also provides keyword suggestions. When a user clicks on a keyword, it is supposed to be placed into an input textbox. The possible keywords are "first" and "second". However, there is ...

Is it possible in PHP to iterate through a query and sort it into a designated column according to a specific value?

I'm working with Laravel and trying to display data from a query in an HTML table. The table contains columns such as id, user, and shift. There are around 100 entries, with the shift column containing values like A, B, C, or D. My goal is to organize ...

Is there a valid substitute for using hot links with images?

Currently, I am instructing a group of individuals on the intricacies of web development with a specific focus on AngularJS. As part of an exercise that I designed for them, they are required to fetch data from the freely accessible pokeapi.co. One interes ...

Utilize data attributes for streamlined markup efficiency

I'm facing a challenge with two almost identical animations, the only difference being the positioning of "left vs right". I want to find a way to reuse the same block of code for both .forward and .backward. I have considered using HTML 5 data-attrib ...

Discovering the method for retrieving JavaScript output in Selenium

Whenever I need to run JavaScript code, the following script has been proven to work effectively: from selenium import webdriver driver=webdriver.Firefox() driver.get("https:example.com") driver.execute_script('isLogin()') However, when I atte ...

Background image not showing on div element

As someone who is new to coding, I could really use some assistance with a problem I've encountered. Specifically, I'm attempting to reference an image within a div element, but unfortunately it's not displaying when I check through my brows ...

I am facing an issue where my Tailwind classes are not functioning properly when passed through props, but they work seamlessly when I directly link the Tailwind CSS using

//Button.jsx import React from 'react'; const Button = (props) => { let color = props.color || 'blue'; return ( <button className={`px-4 py-2 font-bold text-black bg-${color}-500 rounded-full hover:bg-${color}-700 focus ...

Is there a smooth and effective method to implement a timeout restriction while loading a sluggish external file using JavaScript?

Incorporating content from an external PHP file on a different server using JavaScript can sometimes be problematic. There are instances where the other service may be slow to load or not load at all. Is there a method in JavaScript to attempt fetching th ...

Increase the options available in the dropdown menu by adding more selected items, without removing any already selected

I came across this code on the internet http://jsfiddle.net/bvotcode/owhq5jat/ When I select a new item, the old item is replaced by the new item. How can I add more items without replacing them when I click "dropdown list"? Thank you <select id=&qu ...

"Use AJAX to dynamically update a div element when a button is clicked with a specific value

I have a calendar that is being displayed server side using PHP. My goal now is to allow users to scroll through the months with two buttons (<< / >>) without having to reload the entire page. I have come across many similar questions and examp ...

Simple HTML files on a local server are having trouble loading images and JavaScript, yet the CSS is loading

Having worked in web design for quite some time, I recently began working on a basic Angular app that is meant to be very simple. For this project, I am only using the angular files and a single html file as the foundation. As I started setting up the bas ...

Title vanishes when gradient is added to a div

I have encountered an issue with a recent update on my webpage. Previously, I had multiple divs with the title attribute that displayed information when users hovered over them. However, after adding a gradient background, the titles stopped appearing. Th ...

The storing of data in an HTML form input

Within my submission form, there are five distinct inputs that users can enter and submit. Currently, the entered data is being written to a .txt file successfully. Nevertheless, the objective is to organize and display the information in an easily readabl ...