Align all table row elements vertically within Bootstrap

I'm struggling to vertically align different elements in my Bootstrap table row. I want the "select" dropdown, description text, and button to all appear centered vertically. How can I achieve this?

https://i.sstatic.net/Xh3wm.png

Below is the code snippet:

<!doctype html>
<html lang="en>
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <title>testing...</title>
    <style>
        td {
            height: 1em;
            vertical-align: middle;
        }
    </style>
  </head>
  <body>
    <div style="margin-left: 0px">    
    <table class="table table-striped table-hover">
        <colgroup>
                <col span="1" style="width: 4em">
                <col span="1" style="width: 15em">
                <col span="1" style="width: 4em">
        </colgroup>
        <tbody>
            <tr scope="row"><td>
                    <select class="form-control" id="qty">
                      <option>0</option>
                      <option>1</option>
                      <option>2</option>
                      <option>3</option>
                    </select>
            </td><td>Apples</td><td>10lb</td><td><button type="button" class="btn btn-primary btn-sm">Add</button></td></tr>        
    </table>
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>
</html>

Answer №1

Apply the style vertical-align:middle to the table data within the .table class.

.table td {
    height: 1em;
    vertical-align: middle;
}

Solution 2: You can also use !important to forcefully override the CSS properties.

td {
     height: 1em;
     vertical-align: middle !important;
}

Solution 3: Utilize the Bootstrap class align-middle for vertical alignment in td elements.

<tr scope="row">
    <td class="align-middle">    
        <select class="form-control" id="qty">
            <option>0</option>
            <option>1</option>
            <option>2</option>
            <option>3</option>
        </select>
    </td>
    <td class="align-middle">Apples</td>
    <td class="align-middle">10lb</td>
    <td class="align-middle"><button type="button" class="btn btn-primary btn-sm">Add</button></td>
</tr>       

Answer №2

<table class="table table-striped table-hover" **row**>
        <colgroup>
                <col span="1" style="width: 4em">
                <col span="1" style="width: 15em">
                <col span="1" style="width: 4em">
        </colgroup>
        <tbody>
            <tr scope="row"><td>
                    <select class="form-control" id="qty">
                      <option>0</option>
                      <option>1</option>
                      <option>2</option>
                      <option>3</option>
                    </select>
            </td><td>Apples</td><td>10lb</td><td><button type="button" class="btn btn-primary btn-sm">Add</button></td></tr>        
</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

Replicate the styling of CSS class A and apply it to class B

Let's take a look at some code: <button id="test" class="ui-state-hover" value="Button"> In Context: I'm utilizing the JQuery UI CSS class ui-state-hover on an HTML button to ensure it always appears as if it's being hovered over. H ...

Tips for aligning navbar content in the center using Bootstrap

I'm currently working on creating a Bootstrap 4 navbar, but I'm having trouble positioning objects within the navbar. I want to center a search box and place a button on the right, but so far I have not been able to achieve this as the navbar is ...

When the radio button is selected, show a variety of buttons

I'm facing an issue with rendering different buttons for two radio buttons within a view. Here is the rendered HTML code for the radio buttons: <input checked="checked" id="Isattending_0" name="Isattending" type="radio" value="Yes" /> <inpu ...

What are the steps to vertically aligning rows in bootstrap?

I've been exploring different solutions to vertically center my rows within the container. Adding d-flex and align-items-center did work for centering them vertically but caused the rows to appear side by side. I'm curious if there's an alte ...

Tips for showing a public image in-text when pasted on Skype or various social media platforms

As an avid user of Skype, I have discovered that when pasting a link into Skype (using the desktop app) such as a YouTube link, a thumbnail is displayed in the chat. This got me thinking about whether there is a specific class or area in my code, be it CS ...

The background hue does not cover the entire element

When I hover over my drop-down menu button, the grey color only covers up to where I set the padding. Here is my HTML snippet: Help ▾ Give Get Help Get Involved I want the grey color to expand fully across the "li" ...

Use the powerful $.post() method to transfer an image to another page seamlessly

I have two pages, the first page contains a form where users can enter information such as name and mobile number. Additionally, they can upload an image. I would like to use jQuery to access the uploaded image and send it to another page using the $.post( ...

iOS devices are not displaying the background image as intended

I've searched through previous answers on the SO platform, but I'm still unable to find a solution that works for me. I have a static Gatsby site with an infinite scrolling background implemented on desktop, and a static image displayed on mobile ...

Some devices may start the Flutter web page zoomed in by default

Experiencing Zoom Issue on Flutter Web Project: While working on my Flutter web project, I have encountered an issue where the page loads with a zoomed-in view on certain devices. Despite setting the viewport meta tag correctly, the zoom problem persists. ...

Can I trust PHP to create my stylesheet using file_put_contents securely?

In an effort to build websites that are easy to maintain, I came up with a solution to use PHP to call in HTML lines and generate a stylesheet with only the necessary CSS. This way, I can make changes to the CSS once and have it apply to multiple elements. ...

Certain javascript files have had illegal characters mistakenly added to them

There seems to be an issue with the js files or server on certain pages, as they are not loading in Firefox: with firefox: SyntaxError: illegal character 癡爠浥湵楤猠㵛≶敲瑩捡汭敮產崻 ⽅湴敲⁩搨猩映啌敮畳Ⱐ獥灡牡瑥 ...

How can you position an element at the bottom of a div using CSS?

Could you please assist me in aligning an element to the bottom of a div using CSS? Below is a screenshot of my webpage for reference: http://prntscr.com/5ivlm8 (I have indicated with an arrow where I want the element to be) Here is the code snippet of m ...

The scrollbar is failing to display the entire content on the right side of the HTML document

I am facing an issue with creating a horizontal scroll bar within a div element that contains all the contents. The problem arises when a user scrolls, as it is not fully scrolled to the end of the right side, making it impossible to see the content at the ...

I am experiencing issues with my INSERT INTO statement

I've been working on creating a news system that allows users to fill out a form to post an article. However, despite multiple attempts and methods, my INSERT INTO query doesn't seem to be functioning properly. I have two separate files for this ...

The div is not being displayed as intended with ngShow

When working with AngularJS, I encountered an issue where the ng-show function was not functioning as expected within a specific div element. ng-show="false" If you want to see the code in question, you can view it here. In line 84, I included the ng-sho ...

What is the best way to shift a link to the right within a "div" element?

I've been working on setting up a navigation menu for my website, and I'm having trouble getting the "quit" link to align to the right while keeping the other links aligned to the left. I've tried various methods but just can't seem to ...

CSS styles are combined and included at the beginning of the index.html file

During the creation of a production version of my Angular 9.0.4 application, I noticed that the CSS is bundled and placed at the top of the dist/index.html file as shown below: <link rel="stylesheet" href="styles.6ea28d52542acb20a4c6.css"><!docty ...

Slanted lines HTML CSS

I am encountering an issue with my HTML and CSS coding. The design requires a zig-zag "border" at the top and bottom of the page, creating a white paper space in between. The paper needs to be positioned underneath the zig-zag borders. Currently, this is ...

Guidelines for attaching a div to the bottom of a page?

I have a navigation menu inside div.wrapper, and I am trying to make the div.wrapper stick to the footer. <div class="wrapper"> <div id="menu"> <ul> <li>1.</li> <li>2.</li> ...

Allowing horizontal scrolling in a table cell set to full width

I'm attempting to achieve the desired outcome by utilizing the code provided at What I'd like to know is why the scrolling function isn't working as expected. Instead of scrolling, it simply expands the table. To see the difference, try run ...