Aligning the :before pseudo element and cell contents vertically using CSS

I am encountering a very specific issue with a web view I am designing for mobile devices. Within the webview, I have a table that is being re-arranged vertically for smartphone screens. To achieve this rearrangement, I hide the table header and use the column name as a :before pseudo element from a data-label attribute on the original HTML element.

The problem arises when the pseudo element and the content in the td do not align vertically, despite both elements being floated in opposite directions. It seems that the length of the text within the td element is causing this misalignment, but I am unsure of the exact reason behind it.

Attached below is an image illustrating the issue:

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

This is how my HTML is structured:

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

Here is the computed CSS code for reference:

  1. td Style

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

  1. :before Style

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

If anyone can provide insight into why this alignment issue is occurring and what adjustments need to be made to rectify it, I would greatly appreciate it.

Thank you in advance.

Answer โ„–1

The issue you're encountering stems from utilizing the float style in td:before.

index.html

<table class="table table-hover theme-1">
     <thead>
        <tr>
           <th colspan="2">Table 1</th>
        </tr>
    </thead>
    <tbody>
    <tr>
       <td>
        <span>Table col</span>
       </td>                           
    </tr>                                                                                                                                                                          
 </tbody>

To resolve this, place a span tag in the td cell with the max-width property specified as follows:

max-width: 50%;
display: block;

Additionally, include the remaining max-width in the before section like so:

td:before {
        position: absolute;
        content: "shaane shane";
        right: 0;
        max-width: 50%;
        top: 50%;
        transform: translateY(-50%);
        }

Note: Use the position attribute instead of float.

Answer โ„–2

Get ready

td { align-items: center; }

Answer โ„–3

After hours of troubleshooting, I finally uncovered the solution on my own...

The issue stemmed from the compilation of the .cshtml razor syntax.

Because the date fields were enclosed within a C# if-statement, I followed my usual coding format:

@if(startDate != DateTime.MinValue)
{
  <td class="ta-left" data-label="Start">@startDate</td>
}

The problem lay in the text indentation and line breaks for maintaining clean code.

The razor compiler was automatically adding line breaks at certain points, causing errors. Fortunately, the fix involved adjusting the razor markup like so:

@if(startDate != DateTime.MinValue){<td class="ta-left" data-label="Start">@startDate</td>}

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

Tips for compiling Bootstrap SCSS to specifically include only the extended classes

Is there a way to compile my custom bootstrap SCSS file into a CSS file that only includes the classes I created in my custom file? For example, I want to eliminate the extra CSS classes added to my HTML code. Instead of including unnecessary classes lik ...

Retrieve the targeted row from the table and then employ a function

Here is a datatable that I am working on: http://jsbin.com/OJAnaji/15/edit I initially populate the table with data and display it. Then, I add a new column called "kontrole." data = google.visualization.arrayToDataTable([ ['Name', &apo ...

What is the best way to align text to the center in a bootstrap table?

Is there a way to center-align the text in this table? The text-center class doesn't seem to be working. <!-- Bootstrap-5 --> <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfema ...

Concealing the pathway to a background image

Currently, I have a large grid made up of divs that display different sections of an image. By using background-image and background-position, I am able to showcase parts of the image. However, one issue is that users can easily view the complete image by ...

What is the best way to define the location of a button's on-click event using jQuery?

I'm having an issue with my website. It's a Wordpress site using the Contact Form 7 plugin. On the homepage, there is a button labeled "Get a quote" that I want to link to google.com when clicked. I tried using jQuery but it's not working pr ...

housing the picture within a CSS grid

I am new to working with css grids and I have encountered an issue. The background image I want to use is larger than the size of the grid itself. How can I make the image fit within the grid without exceeding its boundaries? When I attempt to insert the ...

generate radio buttons and corresponding labels in real-time

I am trying to automate the creation of elements for each record retrieved from my webservice. <label for="ZAALID_1">Zaal 1</label> <input id="ZAALID_1" type="radio" name="RESERVATIE.ZAALID" value="1" MSGCHECKED="~IF(CHK ...

Optimal method for identifying all inputs resembling text

I'm in the process of implementing keyboard shortcuts on a webpage, but I seem to be encountering a persistent bug. It is essential that keyboard shortcuts do not get activated while the user is typing in a text-like input field. The approach for hand ...

Having trouble accessing DOM elements in Durandal

Running on Durandal, this mobile app features names and images on an HTML page. The function below helps format the page: define(function (){ function getAutors(myUrl) { $.ajax({ type: "GET", url: myUrl, data: { num ...

Style applied directly to .col elements

I want to customize the styling of the .col elements on a single page without affecting other pages. I've tried adding the following code snippet to my CSS file: .col { border: 1px solid #7851A9; } However, this also styles the element ...

Concluding Engagement After Announcing a Victor in a Virtual Competition

I have developed a straightforward JavaScript tic-tac-toe game that is functioning smoothly at the moment. The game involves an array that stores X's and O's, prevents the same spot from being selected twice, and determines the computer's mo ...

Is there a way to collapse just one specific row in Angular?

I am struggling to toggle only the selected row, any suggestions? Take a look at my code and demonstration here: https://stackblitz.com/edit/test-trainin-2-gv9glh?file=src%2Fapp%2Fapp.component.scss Currently, all rows are being toggled when clicked, but ...

Incorporating hyperlinks into icons

This is my first time creating a website for myself and I have added social media icons to share my social media pages. However, I am unsure of how to add links to these icons. Any assistance would be greatly appreciated. Here is the HTML code: <i ...

HTML5, canvas covering every element

Below is the code that I am working with: <html> <head> <title>canvas</title> </head> <body> <canvas width="1745" height="569" style="width: 1730px; height: 1680px; position: absolute; z-index: 1"></canvas> ...

Is there a way to display the value of a script function within an HTML block in an Angular application?

I need assistance displaying the "timeStops" value in an HTML block using a script. The "timeStops" output is in array format and I would like to showcase this array value within an HTML block. Here is the output of timeStops: [ "21:00", " ...

Creating multiple columns and adding new columns to the right side

Can CSS be used to dynamically fill columns in a way that each additional column is added on the left side of a full one? e.g. ---------- | 4 | 1 | | | 2 | | | 3 | ---------- I've seen the webkit-columns properties, but they align from left t ...

Designing exquisite button navigation

Hey everyone, I'm currently working on creating a circular button navigation for my website. I want to have 4 buttons, each with a different color, and I want them to glow when the user hovers over them. So far, this is what I have in my HTML: HTML: ...

How to properly utilize HTML <a> tags with accents in the Python Telegram API

I am facing an issue where a part of the href link is not working due to the accent (รต). Can someone guide me on how to resolve this? _bot_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" _bot_chatID = "@xxxxxxxxxx" ...

Tips for ensuring the border matches the size of the image

I am in the process of creating a website that includes a filter option. My plan is to have the filters displayed on the left side and the items on the right side. To achieve this layout, I have implemented a scrollable div for the items. However, I notic ...

Add a border to the navigation bar item to indicate the current page being displayed

This section shows the current navigation bar item https://i.sstatic.net/6RAGJ.png This is the desired outcome when clicking on the tab https://i.sstatic.net/8nFeB.png Uncertain about the best approach for achieving this. I have attempted to submit a val ...