"Using the inline style 'vertical-align:middle' functions properly, while it does not work in an external CSS file

This issue has been bothering me lately. Every time I search for related information, all I find is Stack Overflow posts about vertical-align not working on div elements or similar situations.

I have an HTML table where I have set the style of each td to vertical-align:middle through inline styles:

<div ng-hide="getShoppingCart().length===0" class="col-md-10 col-md-offset-1">
<table class="table table-striped">
    <tr>
        <th class="col-md-2"></th>
        <th class="col-md-3">Name</th>
        <th class="col-md-2">Size</th>
        <th class="col-md-2">Price</th>
        <th class="col-md-2">Quantity</th>
        <th class="col-md-1"></th>
    </tr>
    <tr ng-repeat="article in getShoppingCart()" style="height:120px;">

        <!-- Image -->
        <td class="col-md-2" align="center" style="vertical-align:middle;">
            <img ng-src="{{article.media.images[0].smallHdUrl}}" class="img-responsive" style="height:120px;" >
        </td>

        <!-- Name -->
        <td class="col-md-3" style="vertical-align:middle;">
            <p>{{ article.name }}</p>
        </td>
        <!-- Size -->
        <td class="col-md-2" style="vertical-align:middle;">
          <p>{{ article.unit.size }}</p>
        </td>

        <!-- Price -->
        <td class="col-md-2" style="vertical-align:middle;">
          <p>£ {{ getTotalPriceForArticle($index) | number : 2 }}</p>
        </td>

        <!-- Quantity -->
        <td class="col-md-2" style="vertical-align:middle;">
            <button class="btn minusButtons" ng-click="decrementQuantity(article, $index)">–</button>
                <input type="number" class="form-control" style="position:relative;top:2px;width:4vw;display:inline-block;" ng-model="getQuantities()[$index]"/>
            <button class="btn plusButtons" ng-click="incrementQuantity(article, $index)">+</button>
        </td>
        <td class="col-md-1" align="left" style="vertical-align:middle;">
            <button ng-click="removeArticleAtIndex($index)" class="btn redButtons" style="margin-left:0;">
                <span class="glyphicon glyphicon-trash"></span>
            </button>
        </td>

    </tr>
</table>
<div class="col-md-12" style="font-size:2vw; text-align:right;">
    Total Price: £ {{ getTotalPrice() | number : 2 }}
</div>

To try and solve this, I decided to remove all inline styles and add a global rule in my external CSS file:

td {
    vertical-align: middle;
}

However, when I did that, the content within the table's cells no longer aligned to the middle. The CSS file is correctly linked as other elements are affected by it. Additionally, there are no conflicting rules with higher priority overriding this property for the table. Any suggestions?

Note: It's worth mentioning that AngularJS is being used in this code and the table rows are generated using ng-repeat, which may be relevant to the issue.

Answer №1

The reason for this behavior is due to bootstrap taking precedence over the specific styling. In the chrome developer console, I noticed the following CSS rules applied to table cells (td's):

.table>tbody>tr>td {
    padding: 8px;
    line-height: 1.42857143;
    vertical-align: top;
    border-top: 1px solid #ddd;
}

To address this issue, I suggest implementing the following solution:

.table.td-vertical-center>tbody>tr>td {
    vertical-align: middle;
}

You can then apply this custom class to your table element like so:

<table class="table table-striped td-vertical-center">

By encapsulating the style within a unique class, you can prevent bootstrap from overriding it by default and ensure that the table cells are styled as intended.

For a demonstration, you can view a live example on bootply here

Answer №2

Give this a shot

table-cell, table-header {
    align-content: center !necessary;
}

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

Achieving Inline Icons with Text in Bootstrap 5 Button

Looking for some assistance <div class="col-auto mt-lg-0 mt-2"> <a href="#" style="--bs-btn-font-size: .95rem;" class="btn btn-outline-secondary"> <i class="bi bi-download"></i&g ...

What's the best way to format my blog post with HTML in Django?

I am looking to integrate HTML formatting directly into my blog post from the admin page. In the models.py file, you can see that the description field is set as a TextField. My goal is to have the ability to include HTML tags in my blog post content on th ...

Moving from one state to another while eliminating an element

I am utilizing angularjs and materialisedcss for designing my webpage. I have a collection of cards which will be exhibited using ng-repeat. When a user clicks on a button (with id=btnConfirm), the card will be deleted from the view. The following code sni ...

Utilizing Loopback Callbacks within a Looping Structure

While working in a for-loop, I encountered an issue where I needed to access the loop variable 'i' from within a callback function but it was not accessible due to closure restrictions. Despite attempting different methods such as using (i) or ca ...

CSS selectors following an ajax request (commenting system)

Issue: After submitting a comment on a webpage, the comments are not displaying correctly. I want them to slide down below the last added comment. It seems to be related to CSS selectors. Can someone help me figure out which selector I need to use? Any a ...

Element with absolute positioning and full width extends past its containing element

I'm trying to achieve a display: flex with position: fixed that has the same width as its parent. However, my current code is extending beyond the boundaries of the parent container. Here's what I've attempted so far. Please review the fidd ...

Positioning a Duplicated Element with Precision in jQuery

I am having trouble positioning a cloned div using jQuery. Even when I try to add CSS to the cloned element using the .css() method, it only affects the text and not the entire element. Here is a link to the fiddle for reference: https://jsfiddle.net/emil ...

The deletion of elements in an array using $timeout is experiencing issues with functionality

After 5 seconds, the directive notification should automatically delete itself. However, there seems to be an issue where some elements are missed and others get deleted more than once. Each notification has a unique identifier property. Thank you for any ...

Template for class watching in AngularJS

I need assistance with creating a directive in my Angular app that is limited to a specific dynamic class injected into the DOM using jQuery. I have experience using the $watch directive, but not in this particular scenario. My challenge is restricting the ...

403 Forbidden: You do not have permission to access this resource

Whenever I attempt to execute a npm install on a project that has Angular as a dependency, I encounter the following error: error 403 Forbidden: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84e5eae3f1e8e5f6c4b5aab2aab0"> ...

What steps can I take to address this particular issue with Javascript/CSS?

I need to figure out how to get the smaller div box to display properly within the larger div. Every time I try to drag and drop it, the result is not what I expect: What could be causing the border of the small box to not show correctly in the larger bo ...

How come block elements do not automatically adjust to the explicit width of their children when a horizontal scroll is present?

Whenever I resize my browser window horizontally in Chrome or Safari and the window width becomes less than an element's width, a scrollbar appears. When I scroll to the right, the content width matches the viewport width only if it doesn't have ...

Arrange images of different dimensions in a CSS grid layout

I have a large collection of resized images (around 100) that are all either 130 pixels wide or 130 pixels high. I'm looking for a way to style these images so that they appear perfectly centered within a background box that is also 130x130 pixels. T ...

Ensure that the input button for uploading is only accessible when checked and is marked as

Most of my previous questions have been about input boxes and SQL, as I am still in the learning process. Any help is greatly appreciated. My current question revolves around displaying a button to upload an image using PHP (although for this example, I w ...

Expanding the padding within a box results in an increase in the overall height of the table row that encapsulates it

My goal is to create a table displaying marks with a box around each mark that expands slightly when hovered over to indicate activity. Below is the code I am using: .container { position: absolute; width: 80%; left: 50%; transform: translateX(-5 ...

Require assistance with displaying a menu on a website using JQuery

Is there a way to create a menu similar to the one shown in the image below?https://i.sstatic.net/QxNPL.png To learn more about this menu, you can visit their website by clicking on this Link. I have copied some code (HTML code and links to CSS and .js fi ...

Using JavaScript to dynamically render a variable amount of tiles

Let's say you have between 4 and 6 images or tiles that you need to display on a webpage. Depending on the specific number of tiles, different formatting is required. For example, if you have five images, they need to be arranged in two rows with two ...

the image srcset fails to resize when the viewport decreases

I'm starting to think that maybe I don't fully understand the srcset attribute. Take a look at this HTML snippet: <img srcset=" /path/to/image1 343w, /path/to/image2 768w, /path/to/image3 304w" sizes=" ...

Issues with Collapse Feature on Bootstrap 3 Navbar

I need your help with a problem I'm facing. The navigation bar is not collapsing when I click the link in mobile view. Check out this video for more details: https://www.youtube.com/watch?v=2dBpcFMjqY0 ...

The $OnChange function fails to activate when passing an object by reference

Hi there, I've encountered a problem in my code that I'd like some help with. In my example, I have two components: Parent Component and Child Component. Both components share a field called rules. The Parent Component passes the rules field to ...