Ways to centrally position elements in a Bootstrap table

I came across this query and tried implementing the style mentioned, but unfortunately, my table items are not aligned vertically:

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

This is how the HTML appears:

.table > tbody > tr > td {
        vertical-align: center;
        background-color: lightgreen;
    }
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

<div class="table-responsive">  
      <table class="table table-sm table-hover">
          <thead>
              <tr class="d-flex">
                  <th class="col-2">Item 01</th>
                  <th class="col-2">Item 02</th>
                  <th class="text-center col-4">Buttons</th>
                  <th class="text-right col-2">Item 04</th>
              </tr>
          </thead>
          <tbody>
              <tr class="d-flex">
                  <td class="col-2">
                      <div style="background-image: url(//placehold.it/1026x600/CC1111/FFF);width: 80px;
                          height: 80px;
                          border-radius: 100%;
                          background-size: cover;">
                      </div>
                  </td>
                  <td class="col-2 text-center">Hey!</td>
                  <th class="col-4">
                      <div class="row no-gutters">
                          <div class="col-2">
                              <button class="btn btn-secondary btn-block">
                                  -
                              </button>
                          </div>
                          <div class="col-8">
                              7 in cart
                          </div>
                          <div class="col-2">
                            <button class="btn btn-secondary btn-block">
                                +
                            </button>
                          </div>
                      </div>
                  </th>
                  <th class="col-2 text-right">Item 04</th>
              </tr>
          </tbody>
      </table>
    </div>

I am puzzled as to why the items are not vertically aligned even though the background-color has been set. Can someone please suggest how I can align them vertically?

Any assistance on this matter would be highly appreciated.

Here is a codeply sample for reference.

Answer №1

 <tbody>
          <tr class="d-flex">
              <td class="col-2">
                  <div style="background-image: url(//placehold.it/1026x600/CC1111/FFF);width: 80px;
                      height: 80px;
                      border-radius: 100%;
                      background-size: cover;">
                  </div>
              </td>

Should be(updated with the .align-items-center class):

 <tbody>
          <tr class="d-flex align-items-center">
              <td class="col-2">
                  <div style="background-image: url(//placehold.it/1026x600/CC1111/FFF);width: 80px;
                      height: 80px;
                      border-radius: 100%;
                      background-size: cover;">
                  </div>
              </td>

Answer №2

After reviewing your Html table code and testing it on my browser, I have implemented some modifications to the th and td classes. Additionally, I have included CSS in the style sheet to ensure proper vertical alignment of your column elements.

<tr class="d-flex">
                        <td class="col-2">
                            <div style="
                                background-image: url(//placehold.it/1026x600/CC1111/FFF);
                                width: 80px;
                                height: 80px;
                                border-radius: 100%;
                                background-size: cover;
                                ">
                            </div>
                        </td>
                        <td class="col-3 text-center coltd">Hey!</td>
                        <th class="col-4 colth">
                            <div class="row no-gutters">
                                <div class="col-2">
                                    <button class="btn btn-secondary btn-block">
                                        -
                                    </button>
                                </div>
                                <div class="col-8">
                                    7 in cart
                                </div>
                                <div class="col-2">
                                  <button class="btn btn-secondary btn-block">
                                      +
                                  </button>
                                </div>
                            </div>
                        </th>
                        <th class="col-2 text-right coltd">Item 04</th>
                    </tr>

You may include the following CSS code in your style sheet:

.colth {
    padding: 30px 0 !important;
    text-align: left !important;
}
.coltd {
    padding: 30px 0 !important;
    text-align: left !important;
}

Feel free to incorporate these changes into your code for better functionality. Thank you!

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

Creating a static line or separator based on the content using HTML and CSS

Is there a way to adjust the height of a line or divider based on the content of either the left or right section? For instance, if we have a left panel and a right panel, and we draw a line or divider on the left side panel, is it possible for the line ...

A dynamically-generated dropdown element in the DOM is being duplicated due to JavaScript manipulation

Currently, I am dynamically adding a dropdown element to the page using javascript. The goal is for this dropdown to modify the properties displayed on a map. However, when attempting to add the element after the map loads, I encounter an issue where the d ...

Preserving data and retrieving it

I've been working on a program for my diving coach that calculates the dive score as judges input their judgments. However, I'm encountering an issue where all fields clear themselves when I hit submit. Moreover, I'd like the ability to save ...

best practices for choosing items from a dropdown menu using Angular

I have a dropdown list displaying existing tags/chips created by users in the past. However, I'm having an issue where when I select a tag from the dropdown list, it doesn't show up in my input field (similar to the Chart tag currently). I am abl ...

What is preventing me from being able to reposition certain tables on my site?

I have successfully implemented certain aspects of my responsive website, but I am encountering some difficulties in moving specific tables. I can adjust the table in various directions, except for moving it downwards. Even when adding the word !important ...

Sending data via Ajax using the multipart/form-data format

I am encountering an issue with a script that is not working correctly. When I submit the form without using the script, everything works as expected. However, when I use the script to submit the form, only the category and description are included in the ...

Rearrange the order of divs dynamically to prevent wrapping/overflow, no need for media queries

Can someone help me with this issue? I am trying to create a navigation bar with centered menus/links, a logo on the left, and call-to-action buttons and icons on the right. If the content overflows or wraps, I want the center column to move to the next l ...

What is the best way to create a video that takes up the entire height of a half-sized

page url: I am facing a challenge with displaying a video that has dimensions of 4096x2160 pixels. I want to show the centered part of the video with full height on my left half screen. Despite trying various methods, I have not been able to find a suita ...

Tips for evenly spacing items in a navigation bar

I'm having trouble centering the logo on the navbar as it always leans more towards the left. Is there a way to resolve this problem using Bootstrap classes? The navbar consists of a dropdown toggle button on the left, the logo in the center, and two ...

My code written using Visual Studio Code is not displaying properly when I view it in my browser

I have been following along with a tutorial series that can be found at this link. This link will take you to the third video in the series, but I have also followed and programmed alongside the first and second videos. After installing Visual Studio Code ...

The XML request fails to retrieve any output from the PHP script

I am seeking tools to enable the calling of .php from .html files. Here is an issue: while this example successfully works in a .php file: <html> <head> <meta http-equiv="content-type" content="text/html" charset="utf-8"> &l ...

Encountering a Javascript Error when trying to begin

As a beginner in Javascript, I am venturing into designing a simple website that incorporates Javascript. Currently, my focus is on building a calculator. However, upon loading my website, nothing seems to initiate and there are no error messages displayed ...

resetting the page's scroll position using a hamburger icon

After adding a hamburger icon to my web project, I noticed that whenever I click on the icon, it scrolls back to the top of the page. I tried adjusting the margin and padding properties, but it still behaves the same way. Even after removing the animation ...

What are some strategies for creating a smooth transition with a max-height of 0 for a single-line div?

I have a div that is one "line" in height. When a button is clicked, I want it to disappear and simultaneously another div with more lines should appear. I have managed to make this work, but no matter what I do, the one-line div disappears very quickly al ...

"Failure to manipulate the style of an HTML element using Vue's

<vs-tr :key="i" v-for="(daydatasT, i) in daydatas"> <vs-td>{{ daydatasT.Machinecd }}</vs-td> <vs-td>{{ daydatasT.Checkdt }}</vs-td> <vs-td>{{ daydatasT.CheckItemnm }}< ...

Include new items in the li tag using JavaScript

Hello, I am looking to dynamically add elements to an LI element when a link is clicked. I have successfully added elements to a DIV using the following code snippet: I came across a similar question on Which "href" value should I use for JavaScript links ...

Efficiently refine your search using the combination of checkboxes and dropdown menus simultaneously

I am currently in the process of creating a highly sortable and filterable image gallery that utilizes numerous tags. The inspiration for this project stems from a similar question on Stack Overflow regarding dropdown menus and checkboxes. You can view the ...

The :focus pseudo-class is failing to target the input element

I'm dealing with a simple input field of type text: <input matInput type="text" placeholder="{{placeholder}}" class="input-field" [ngClass]="{'error': hasErrors}" [readonly]="isReadonly&quo ...

Tips for adjusting font sizes within the same HTML header?

Looking to design an HTML header in the following format: "(10.3.4) Version1: vs (10.3.4) Version2:" Need the version numbers to be smaller than "Version1" and "Version2." Any ideas on how to achieve this? ...

Border utilities in Bootstrap 4 provide easy ways to add borders

I have been searching for a solution, but I can't seem to find it. Bootstrap 4 offers utility classes for adding and removing borders, but is there a way to define the border width or style like solid or dashed? <span class="border"></span&g ...