Achieving a compressed display of an HTML table

My boss recently asked me to enhance the responsiveness of a table on our website. I initially followed the solution discussed in my previous query about making tables responsive (Responsive Table Display)

However, after completing that implementation, I was informed that they simply want the 5-column table to be "squished" in a way that all columns remain visible but fit within the mobile viewport effectively.

This is an example of the current HTML structure:

<table>
    <thead>
        <tr>
            <th>A</th>
            <th>B</th>
            <th>C</th>
            <th>D</th>
            <th>E</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td rowspan="2">1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
    </tbody>
</table>

Considering that I am self-taught and have only been working in this field for a few months, I would appreciate any guidance on how to make the table compress its width effectively for mobile view?

Answer №1

To ensure your table fits the screen width, adjust its size accordingly. If the container is set to 100% of the screen's width, then set the table's width to 100%. In cases where the container's width varies, you can use 100vw, representing a percentage of the viewport width. Keep in mind that the vw unit does not work on IE8 or older versions.

If column widths are unspecified, they will automatically expand to fill the table.

You can customize column widths using the media query discussed earlier:

@media only screen and (max-width: 760px), (min-device-width: 768px) and (max-device-width: 1024px) {
    table {
        width: 100%; /* or 100vw based on your HTML structure */
    }
...
}

To control column sizes, set percentages for th and td elements that add up to 100%, or adjust the table-layout property. Using table-layout: fixed; will evenly space all columns.

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

Is it possible to convert the information from <input type="file"/> into an image on the client-side?

As mentioned, I currently have this code: <input type="file" accept="image/png, image/jpeg" onChange={(e) => onHandleProfileImageChange(e)} ></input> I receive the file's data and send it to the server. On the server side, ...

Unleashing the power of specific dates with the angularJS datepicker directive

I am looking to develop a custom Datepicker directive using Angular JS, with the requirement of allowing only specific dates for selection by the user. For instance, I have a predefined list of numbers such as 1,3,5,7, and my goal is to make these particu ...

The placement of text within a <div>

I'm new at this, so please forgive me if my question sounds naive. I've been struggling with this issue the whole day. I'm working on a student project: https://codepen.io/kremlevmax/pen/EomEyb Here is the HTML: <body> <div ...

Moving images displayed on a webpage

Is animating 5 pictures/photos a simple task? Take a look at this example: Which programming languages are recommended for achieving this effect? Thank you Tea ...

Can you please explain the significance of this error for me?

I'm attempting to showcase specific elements from an array in my code. <?php $data = array( 1000 => array( "id" => 1000, "number" => 2380, "name" => "CS2", "instructor" => "Chen", "rating" => null ), 1001 => a ...

Guide on sending two values from a form using Ajax and the POST method

How can I send two values, A and B, from a form to check.php using Ajax with the POST method? In the code snippet below, I managed to send two input values to check.php and assign them to variables $A and $B. However, I want to accomplish this without ref ...

Having both forms and submit buttons on a single HTML page

<form id = "msform" action = "" method = "POST"> <input type = "text" name = "name" /> <input type = "submit" name = "submit1" value "Submit" /> </form> <form id = "msform" action = "" method = "POST"> <b>Name:</b> ...

What steps can I take to ensure my website maintains a consistent appearance across various internet browsers?

I'm fairly new to web development; my knowledge is limited to CSS and HTML, just enough to make a website look good. I would appreciate it if you could explain things in simple terms. I've been researching how to ensure that a website looks the ...

Enable players to share their accomplishments from my online game on Facebook

I am currently working on a feature to allow users to share their scores and achievements from my web-based HTML5 game on Facebook. Specifically, I want to: Customize the text that accompanies the post Include a snapshot of their achievement badge by spe ...

How about optimizing HTML by utilizing jQuery's magic?

For my website, I aim to allow users to choose a grid size by clicking on elements of a large grid display. This can be compared to selecting a table size in Microsoft Word. Each grid element will have a designated position ID for organization. Instead of ...

AngularJs Number Formatting: A Step-By-Step Guide

Is there a way to format a number from 10000 to 10,000.00 using only HTML and the ng-Model directive, without involving the controller file? I attempted to achieve this in the controller file through logic, but it always returns the number in the "10,00 ...

Placing an image on top of a background image in CSS while maintaining a fixed aspect ratio

My CSS code is currently resizing the background image to fit the browser size while keeping the center of the image in the center of the window. Now, I need to add another image, like an icon in a circle, at the center following the same aspect ratio rule ...

"Incorporating a hyperlink into a newly added table row with the help

When utilizing jQuery, I am able to dynamically add rows to a table with each row containing an anchor tag. However, when attempting to populate the anchor tags with links using jQuery, the links do not appear as expected. Strangely enough, the other data ...

Open $_POST in a new tab that has been edited for your convenience

<form method="post" target="_blank" action="battle.php" onsubmit="window.open('battle.php','','width=700,height=500,toolbar=0,menubar=0,location=0,status=0,scrollbars=0,resizable=0,left=30,top=0');" > < ...

Custom control unable to display MP3 file

Hey, I came across this awesome button that I am really interested in using: https://css-tricks.com/making-pure-css-playpause-button/ I'm currently facing two issues with it. First, I can't seem to play the sound. I've placed the mp3 file ...

Step-by-step guide on incorporating an additional element into the Yii radioButtonList layout

I am trying to display a radioButtonList with tooltips after each element. Here is the code I am using: <?php echo $form->radioButtonList($model, 'Field', CHtml::listData(FieldDataModel::model()->findAll(), 'id', 'name&a ...

What could be causing the absence of the input elements on my webpage?

Finally Got It to Work! After troubleshooting, I managed to fix the code on the first page and successfully link it to this second page. Despite using the same CSS and similar HTML, I couldn't figure out why the buttons were not displaying at all. Im ...

How to position a child element beneath its parent using CSS

Can you position a child element (C) beneath its parent (B), but above B's neighboring element (D)? Explaining this can be tricky, so feel free to check out an example here. The main question is: how do you place the blue div.inner above the red div ...

Unable to modify the hover color on the image or icon

After creating a circle icon with an image in the center, I wanted to make the image change colors on hover. The main focus of the icon is the circle itself. In my attempt to achieve this effect, I included the following code for the circle icon: .circle- ...

Ways to extract information from JSON files

Currently, I am working on a script to extract viewer count and follower count data from Twitch. While I have successfully retrieved the viewer count information, I am encountering issues with extracting the follower count. The essential information can be ...