Achieving Perfect Alignment in HTML Tables

I have created a simple HTML table and I am trying to center it vertically in the middle of the page based on the user's device height. So far, I have tried adding a div with 100% height and width, positioning the table at 50% from the top and left, but it hasn't worked. Any suggestions?

body {
    background-color: #a00000;
}
table {
    margin: 50px auto 25px auto;
    padding: 0px;
    background-color: #fff;
    border-radius: 10px;
}
td {
    background-color: #fff;
    padding: 10px;
    background-color: #fff;
    border-radius: 10px;
}
img {
    max-width: 120px;
    max-height: 120px;
    border: solid 5px #a00000;
    border-radius: 5px;
    background-color: #a00000;
}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Bobcat Menu</title>
        <meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='viewport' />
        <link rel="stylesheet" href="css/style.css">
    </head>
    <body>
        <table align="center">
            <tr>
                <td>
                    <a href="mobincube://action/section/DropDay">
                        <img src="
https://s3-us-west-2.amazonaws.com/s.cdpn.io/837604/D.png" />
                    </a>
                </td>
                <td>
                    <a href="mobincube://action/section/Schedule">
                        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/837604/S.png" />
                    </a>
                </td>      
            </tr>
            <tr>
                <td>
                    <a href="mobincube://action/section/Calculators">
                        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/837604/C.png" />
                    </a>
                </td>
                <td>
                    <a href="mobincube://action/section/News">
                        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/837604/N.png" />
                    </a>
                </td>      
            </tr>    
        </table>
    </body>
</html>

Answer №1

If you want to achieve the desired outcome, consider utilizing flexbox.

  1. Firstly, ensure that both the body and html elements have a height of 100%.

  2. Enclose the table within a wrapper div and apply the following CSS:

    .wrapper {
       display: flex;
       height: 100%;
    }
    
  3. Adjust the margins of the table using margin: auto;

By following these steps, you can guarantee that your table will be perfectly centered.

body, html { 
    height: 100%;
}
body {
    background-color: #a00000;
    margin: 0; /* eliminate default browser-added margins */
}
.wrapper {
   display: flex;
   height: 100%;
}
table {
    margin: auto;
    padding: 0px;
    background-color: #fff;
    border-radius: 10px;
}
td {
    background-color: #fff;
    padding: 10px;
    background-color: #fff;
    border-radius: 10px;
}
img {
    max-width: 120px;
    max-height: 120px;
    border: solid 5px #a00000;
    border-radius: 5px;
    background-color: #a00000;
}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Bobcat Menu</title>
        <meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='viewport' />
        <link rel="stylesheet" href="css/style.css">
    </head>
    <body>
        <div class="wrapper">
        <table align="center">
            <tr>
                <td>
                    <a href="mobincube://action/section/DropDay">
                        <img src="
https://s3-us-west-2.amazonaws.com/s.cdpn.io/837604/D.png" />
                    </a>
                </td>
                <td>
                    <a href="mobincube://action/section/Schedule">
                        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/837604/S.png" />
                    </a>
                </td>      
            </tr>
            <tr>
                <td>
                    <a href="mobincube://action/section/Calculators">
                        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/837604/C.png" />
                    </a>
                </td>
                <td>
                    <a href="mobincube://action/section/News">
                        <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/837604/N.png" />
                    </a>
                </td>      
            </tr>    
        </table>
        </div>
    </body>
</html>

Answer №2

To enhance the appearance of your table, consider incorporating these CSS styles into the table tag:

table{
    /* ... */
    position: fixed;
    top: 50%;
    transform: translateY(-50%);
}

Answer №3

Apply this style to the parent div: display: table-cell;
Then, use vrtical-align on the actual table.

To implement this in your code:
html:
<div id="wrapper">
  <div id="content">Your content here</div>
</div>

css:
#wrapper {display: table}

#content {
  display: table-cell;
  vertical-align: middle;
 }

Answer №4

If you're looking to vertically center yourself on any browser, you could try using the following CSS code:

div {
    margin: 50px auto 25px auto; 
    padding: 0px;
    background-color: #fff;
    border-radius: 10px;
    position: relative;
    top: 50%;
    transform: translateY(50%);
}

https://example.com/css-centering

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

Guide on extracting targeted information from a paragraph using htmlAgilitypack

When scraping data using htmlAgilityPack, everything seems to be working fine. However, I encountered a situation where I need to extract the description from a paragraph p tag. The paragraph p tag contains the following description: <p> <small& ...

Creating a continuous loop in JQuery when clicking on a table row

I seem to be encountering an issue with what appears to be an infinite loop. My problem arises while trying to create a table dynamically using Ajax. Each row of the table contains a button alongside a thumbnail image and some text. I wish for the button ...

Applying Bootstrap Style to an Inline Select Element: A Step-by-Step Guide

Currently working on a page layout using Thymeleaf and Bootstrap 5. I have divided the page into two parts, with this section behaving as desired: <main role="main" class="pb-3"> <br> <p>Post office ex ...

Instructions on how to automatically play multiple video tags on one HTML page while also opening a modal

I'm working on an HTML page that needs to display two different videos in separate modals. My goal is to have each video start playing automatically when the play-video icon is clicked and the modal opens. I attempted this using the code snippet (vid ...

Tips on concealing the visibility of a table row using PHP

My HTML table structure is quite simple: <style> .demo { width:100%; border:1px solid #C0C0C0; border-collapse:collapse; padding:5px; } .demo th { border:1px sol ...

The CSS style tag does not function in Safari

I am trying to dynamically load background images using CSS based on the display resolution using the @media keyword. This method works perfectly in Firefox and Chrome, but unfortunately, it does not seem to work in Safari. Is there something I might be ...

Extract all class elements from HTML code and save them to a text file using PHP

Is there a way to extract all span elements with the class "msgsource" from my website's HTML code and then save it as a .txt file for downloading? I tried using the following code, but it only downloads an empty text file: <?php // Grabbing conte ...

I am experiencing an issue where the Flex table is not displaying more than 50 rows

I am experiencing an issue where I can't scroll back up with the scrollbar. The table is dynamically filled with data from a database, and when it gets too long, I am unable to scroll back up. Any suggestions on how to solve this problem? If you need ...

Achieving a sidebar that remains full height with CSS

Currently working on this page () where my aim is to create a sidebar that fills the entire height of the screen. The goal is for the sidebar, along with the black line on its right, to always reach the bottom of the viewport. JSFiddle Link Here's t ...

Display the HTML code in its formatted text output

I am currently facing an issue while trying to take input from the user using the WYSIWYG text editor plugin. When I attempt to display the text in a label or paragraph, all the HTML tags become visible. For instance, @string str = "<b>sample text&l ...

How to disable a select list in HTML using vue.js when it is checked

I need assistance with a form that includes an "age" field. There are two possible scenarios: if the "unknown" box is checked, then the user should not be able to enter/select an age (the option should be disabled). If the box is unchecked, then the user s ...

I am encountering difficulties in accessing my component's property within the corresponding template while working with Angular 5

When I call an HTTP POST method to retrieve table names from the backend, I attempt to display them in the template using ngFor. However, the table names are not appearing on the screen. The tNames property is inaccessible in the template. As a beginner i ...

What steps can be taken to resolve the issue of Ajax not retrieving any data from

I'm struggling to identify the error in this code. I have created a search bar and implemented AJAX to automatically fetch data. Here is the HTML file: <!DOCTYPE html> <html> <head> <title></title> <script ...

Confirm that the phone number is in the correct format using HTML

Hello, I am attempting to validate a phone number using the following format: 080******** 081******** 082******** 083******** I have tried the following code: <input type="tel" name="signupMobileTel" placeholder="08121234567" size=11 pattern="/^(08( ...

The Angular 2 view will remain unchanged until the user interacts with a different input box

I am currently working on implementing form validation using Reactive Forms in Angular 2. Here is the scenario: There are two input fields Here are image examples for step 1 and step 2: https://i.stack.imgur.com/nZlkk.png https://i.stack.imgur.com/jNIFj ...

Using a Node.js module to shrink HTML files

Is there a way to minify HTML before rendering it? I'm aware of console minifiers like: html-minifier However, I am looking for a solution that can be implemented in the code itself. Something along the lines of: var minifier = require('some- ...

"Items within mui Grid do not properly align within the Grid container

I'm struggling to fit two large Grid items inside a Grid container. The Grid container needs to be 100% of the screen's height (or parent container) Grid items are large and need to fill the container while remaining scrollable Image #1 shows t ...

Tips for centering text inside a div using a separator

During my side project, I decided to use the angular material divider but encountered issues with aligning the text correctly. https://i.stack.imgur.com/zg1mu.png The problem can be seen in the image provided - the text on the right side is not aligned b ...

Guide to implementing the offcanvas sidebar with bookmarks using Bootstrap

On my website, I have implemented a convenient offcanvas sidebar using Bootstrap 5. This sidebar contains html bookmark links that allow users to quickly navigate to specific points on the page by clicking on them. Here is an abbreviated version of the cod ...

Text arranged in a vertical orientation with a group of text aligned along the baseline

I am trying to create the following layout: https://i.sstatic.net/HYNDw.png Here is what I need: The word total and the number 120 should align vertically in the center The words per month should align at the bottom with respect to the number 120 The ...