Reformat a table in Asp.Net Bootstrap to ensure optimal display on mobile devices through a media query,

I came across this code on the following website: https://css-tricks.com/responsive-data-tables/, but unfortunately, it is not functioning as expected for my specific table within a Razor view.

Here is the table code that I am using:

<table class="table table-hover">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.FirstName)
            </th>

            <th>
                <a asp-action="Index" asp-route-sortOrder="@ViewData["NameSortParm"]">@Html.DisplayNameFor(model => model.LastName)</a>
            </th>

            <th>
                <a asp-action="Index" asp-route-sortOrder="@ViewData["EmailSortParm"]">@Html.DisplayNameFor(model => model.Email)</a>
            </th>
       </tr>
   </thead>
   <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.FirstName)
                </td>

                <td>
                    @Html.DisplayFor(modelItem => item.LastName)
                </td>

                <td>
                    @Html.DisplayFor(modelItem => item.Email)
                </td>

            </tr>
        }
    </tbody>
</table>

Below is the CSS that I have applied:

table {
    width: 100%;
    border-collapse: collapse;
}

th {
    background: #ccc;
    font-weight: bold;
}

td, th {
    padding: 6px;
    border: 1px solid #ccc;
    text-align: left;
}

/*Media Query*/

@media only screen and (max-width: 760px), (min-device-width: 768px) and (max-device-width: 1024px) 
{

    /* Force table to not be like tables anymore */
        table, thead, tbody, th, td, tr {
        display: block;
        }

        tr {
        border: 1px solid #ccc;
        }

        td {
        /* Behave  like a "row" */
        border: none;
        border-bottom: 1px solid #eee;
        position: relative;
        padding-left: 50%;
        }

        td:before {
        /* Now like a table header */
         position: absolute;
        /* Top/left values mimic padding */
         top: 6px;
         left: 6px;
         width: 45%;
         padding-right: 10px;
         white-space: nowrap;
        }

        /*Lable data*/
        td:nth-of-type(1):before {
            content: "FirstName";
        }
        td:nth-of-type(2):before {
            content: "LastName";
        }
        td:nth-of-type(3):before {
            content: "Email";
        }
}

Preview of the table before the media query kicks in:

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

Preview of the table after the media query takes effect:

https://i.sstatic.net/SgB6H.jpg

I simplified the code for readability purposes. Do you have any insights on what might be causing the issue?

Answer №1

Actually, I just realized what the issue was. It turns out that position absolute is unnecessary in this case: https://i.sstatic.net/uHVeL.png

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

Magento theme installation on the website encountered unexpected obstacles

Hi everyone, I'm currently in the process of constructing a website with Magento. However, I've encountered some issues after trying to install a theme. It seems like certain files, including CSS, are not loading properly. To provide better conte ...

Transforming the responsive design of Twitter Bootstrap 2.0 into a fully fixed layout

The GitHub repository for Twitter Bootstrap's 2.0-wip branch now features an innovative responsive layout system. While it's ideal for applications requiring a high degree of responsiveness, I am currently working on a web-exclusive app that dema ...

Blade Form is currently not displaying the flash message and also failing to validate the form fields

Hey there, I'm facing an issue with a contact form setup in my Laravel project. Everything seems to be working fine as the form is successfully submitting data to the database. However, when I submit the form with blank fields, the validation does not ...

Is the absence of http(s) in <link ...> causing any issues?

I recently noticed a peculiar link on the Font Awesome homepage that seems to work without including http or https. <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"> This made me wonder, what does // a ...

The button is not properly aligned at the center of the flex container

My attempt to center a button in a fluid container with 100% vertical height and provide it with an offset from the bottom of the screen is almost successful, but the button is not perfectly centered - it is offset from the left. You can view the example i ...

Conceal Bootstrap4 navigation link on iPhone screen to provide a cleaner user experience

Once again, I am facing a dilemma and seeking assistance. How can I selectively hide one of my bootstrap4 nav links (Link2) when the page is accessed on an iPhone? Any guidance on this matter would be greatly appreciated. Thank you in advance. <div c ...

The behavior of the jQuery slick slider is acting oddly

Currently, I have implemented the Slick Slider for my product loop in order to display the products in a slider format. However, upon the initial page load, there appears to be a significant white gap below the products. Interestingly, when I interact with ...

Preventing the page from auto-refreshing upon button click

Recently, I have encountered an issue with a button on my webpage. Every time the button is clicked, the onclick code executes and then the page refreshes. This was not a problem before, but now it seems to be automatically refreshing the page. The curren ...

Issue with iPad CSS floats causing layout problems?

Having a css challenge with the layout on ipad for this particular website: When viewing on a browser, the social media icons align correctly to the right. However, on an iPad or iPhone, they are not aligned all the way to the right. Is there anything tha ...

Error Occurs When Attempting to Delete in ASP.NET MVC

I am encountering an issue while attempting to delete something, as it is displaying the following error message: "Error: Unknown Action" Below is my controller code: [Authorize(Roles = "Admin, Staff")] [HttpPost] [ValidateAntiForgeryT ...

"Despite attempts to use workarounds, inline-block elements still create unnecessary line breaks due to their inherent

My header is behaving unexpectedly when I break a sentence into separate elements. I have attempted all the suggested solutions here: How do I remove the space between inline-block elements?. I even tried applying font-size: 0 to the parent element. The e ...

A minimalist dropdown menu using only HTML and CSS with an onclick function

I have been working on creating an onclick dropdown menu for mobile devices that should disappear when viewed on wider screens, but I've encountered an issue where the links in the menus are not clickable even though the @media query is set up correct ...

Guide on triggering a bootstrap modal by clicking on the image within an AngularJS application

In my angularjs project, I am currently working on a UI feature that involves cards. I have successfully implemented a modal box that opens when a button below the card image is clicked. However, I am looking to enhance the user experience by allowing th ...

How to make a HTML div background fill the entire page

Is there a way to extend the background in a div so that it adjusts to fit the screen resolution? I also want to include a navigation bar at the top right corner of this div for the intro page. There will be additional content in other divs positioned be ...

What is the best way to create a separate text box for each image on a page?

Is it possible to modify this code so that each text box only shows the text corresponding to its respective image? I am still relatively new to coding and am struggling to achieve the desired outcome. function displayImageInfo(text) { document.getEle ...

Issues with the jQuery UI Slide Animation

I am in the process of revamping our company website and incorporating a lot of effects using jQuery, Flash, and more. While I haven't encountered any major issues so far, there are two minor problems that keep popping up. Since I have received great ...

Issues with the alignment of the navbar-right in Bootstrap are causing

Just starting out with bootstrap and encountered an issue I have two different classes for the navbar-header, one I want on the left and the other on the right. I need both to be responsive. <nav class="navbar navbar-default navbar-fixed-top topnav " ...

Using ng-if to compare dates in AngularJS without considering the year

I am facing a comparison issue with dates in my code. I have one date that is hardcoded as the first day of the month, and another date coming from the database (stored in a JSON object). When I compare these dates using ng-if, it seems to ignore the year ...

Map will be visible correctly only when the window is resized

I'm currently working on a project that utilizes a map engine from a private company which I am unable to disclose. The initial system was built in JQuery, but we recently underwent significant changes and now have it running on AngularJS. One side ...

Adjusting the size of content with CSS

I've been attempting to get an image to cover the entire screen on a device using CSS, but so far I haven't had any success. The image is within an :after pseudo element and content tag. Since it needs to be laid over the HTML, I can't use t ...