Unusual vertical-align quirks within a td cell

This specific piece of HTML displays as intended (JSFiddle):

<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<style media="screen" type="text/css">
    thead th, td {
        text-align: center;
    }
</style>
<body>
    <table class="table table-bordered table-hover">
        <tbody>
            <tr>
                <th>
                    X<br>Y
                </th>
                <td style="vertical-align: middle">
                    <span class="glyphicon glyphicon-remove"></span>
                </td>
            </tr>
        </tbody>
    </table>
</body>

However, I am looking to consolidate all CSS properties related to the td element in one place like this (JSFiddle):

<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<style media="screen" type="text/css">
    thead th, td {
        text-align: center;
    }
    td {
        vertical-align: middle;
    }
</style>
<body>
    <table class="table table-bordered table-hover">
        <tbody>
            <tr>
                <th>
                    X<br>Y
                </th>
                <td>
                    <span class="glyphicon glyphicon-remove"></span>
                </td>
            </tr>
        </tbody>
    </table>
</body>

Regrettably, when attempting this structure, it distorts the vertical alignment of the glyphicon.

I'm curious to understand why this occurs and how it can be resolved?

Answer №1

The default bootstrap styling showcased here includes a sibling selector, giving it higher priority than your embedded styles. However, inline styles take precedence.

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


You have the option to use !important (not recommended)

or

stick with inline styles

or

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

Check out the demo!

Answer №2

After inspecting with Chrome developer tools, I noticed that the Bootstrap CSS is overriding your own CSS. This is due to its higher priority and more specific selectors, similar to how an id supersedes a class.

This explains why it works when using inline styles but not when specified externally. To resolve this issue, you can either use:

td {
        vertical-align: middle !important;
    }

Alternatively, if you prefer not to use !important, you can replicate the selector used by Bootstrap like so:

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

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

I am trying to make my CSS divs remain in the exact same position on the background image, even when the browser is resized both horizontally and vertically. How can I achieve this?

I need the red dot divs to remain fixed on a world map, regardless of browser resize. Not sure if I should be adjusting div properties or background. Here's my CSS code, any feedback is appreciated as I'm new to web design: HTML: <!DOCTYPE ...

Utilizing jQuery for Dynamically Loading Child Elements

As users select choices from a side menu, a set of items is dynamically added. The more choices they make, the more items are displayed. Some items have a 'stats' div while others do not. I want to check if an item has a 'stats' div, a ...

Position the button at the corner of the textarea

How can I position two buttons below a text_area, aligned with the bottom right corner of the text area? Here is what I have tried so far: https://i.sstatic.net/UcD2F.png Current code snippet: form with modal <%= form_for @note, :url => registrant ...

Achieve mobile responsiveness for search fields with Bootstrap 4

Attempting to center my search field using the code below did not yield a responsive result. On mobile, it remains right on top with the footer, unlike on desktop where I'd like to see some space between them. This discrepancy only surfaces in small ...

Adjusting the background width results in the container position being altered as well

Currently, I am working with two background images, each accompanied by a red box positioned absolutely inside a bootstrap container. The first red box is located on the right side and the second red box is on the left side. So far, everything is running ...

Implementing jQuery stylesheet using a bookmarklet

Here's a question that may seem silly, but I'm looking to quickly apply CSS styles to a webpage by clicking on a bookmarklet. It will definitely save me time :) For example, this code does the trick: javascript:void( jQuery( ".fancybox-overlay- ...

Python Application Engine - Streamlining Responses from HTML Control Arrays

In my attempt to use App Engine (Python), I am facing a challenge in sending POST values from a variable array of select controls within an HTML form. Each select control is associated with a text describing what the user is rating. For instance, let&apos ...

Fixing CreateJS in Adobe Animate HTML5 Advertisement for Internet Explorer versions 9 and 10

As I work on creating an HTML5 ad using Adobe Animate CC, I encounter a minor setback. While testing the ad, I notice that it displays perfectly in most browsers except for Internet Explorer versions 9 and 10. It's interesting to see that according ...

Parallax Page with a Stylish CSS Navigation Bar

I've been searching for tutorials on parallax effects and I've managed to implement it successfully. However, when I tried adding a navigation bar to the parallax page, the results were not what I expected. Can someone assist me with this? Below ...

Is there a way to reduce the height of the border-left property in CSS?

In creating a simple address panel, I have divided it into three sections: address, phone number, and email. Each section includes an icon and a paragraph next to it. To align these sections side by side, I used the vertical-align: top and display: table p ...

Exclude striped tr elements within a subtable

I have a main table (Maintable) containing information, with each row having a sub-table for additional details that can be collapsed if needed. I am trying to stripe the rows in the Main table but using `tr:nth-child(even)` is not working as it also affec ...

Combining Three HTML Tags Into a Single Page

I currently have a standard PHP webpage that consists of the header, body, and footer sections. Within this setup, I use the following code: <?php include('header.html'); ?> The header.html file contains <html>... content </html& ...

Send the user to a specified destination

Currently, I am working on creating a form that allows users to input a location and have the page redirect to that location after submission. However, I am facing difficulty in determining how to set the value for action="" when I do not know what the loc ...

Is it possible for a font to exclude the space character?

I'm currently developing a font detection library that must be extremely compact in size, perfect for direct inclusion on every page of a website. I've managed to shrink it down quite a bit already (compressed to 417 bytes). Take a look at it on ...

Exploring AngularJS to search for and present data in a tabular layout

Can anyone guide me on searching for data within a JSON file and presenting it in a table format? I want the table to be visible only when I click the search button. I would really appreciate it if someone could provide me with instructions or a helpful ...

Selenium's XPath locator unable to locate element despite being visible on the browser

Attempting to extract the WHO coronavirus data from the left sidebar of this website: using Xpath. Here is the code snippet: from selenium import webdriver import time driver = webdriver.Firefox() driver.get("link_to_the_page") time.sleep(30) coun ...

Guide to implementing onhashchange with dynamic elements

Hey there! I've encountered a problem with two select boxes on my webpage, each in different anchors (one on the page, the other in an iframe). What I'm trying to achieve is for the code to recognize which anchor it's in and then pass the se ...

How to align text in the center of a card using Angular Bootstrap

I have a column div that I need to center the text in Here is my code: <div class="card mb-4"> <div class="card-header"> <div class="card-title-wrap bar-info"> ...

I am looking to generate div elements using JavaScript to avoid the tedious task of individually creating numerous divs

Instead of manually typing out multiple div tags in the HTML, I would like to dynamically generate them using JavaScript and display them on the page. Below is an attempt at achieving this, but it appears to not be functioning correctly. var arr = {}; f ...

Issue with jquery .click function not triggering after CSS adjustment

In the process of creating a fun game where two players take turns drawing on a grid by clicking <td> elements. Currently, I have implemented the functionality to toggle the background color of a <td> element from black to white and vice versa ...