CSS - Positioning Tables in the Center of a div and Ensuring table Rows Break Horizontally

I've been struggling for days to center a table, but no matter what I try, it just won't cooperate.

For some reason, using margin-right:auto and margin-left:auto within the DIV is not producing the desired centering effect!

I managed to make it responsive for mobile by applying specific margins in @media queries, but it feels like a hacky solution. It should be a simple task, yet I can't figure out why it's not centering properly and being responsive.

Another issue that should be straightforward.

Whenever I add images above the table, it messes up the layout of the table, making it look like this:

IMG
        TABLE   TABLE 
TABLE

Any suggestions on how to solve either of these problems?

Here is the code :

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">


table{
    text-align: center;
display:inline-table;
max-width: 33%;
height: 300px;
background-color: white;
margin: 15px;
border-collapse: collapse;

}
td,tr{

padding: 0;
vertical-align: middle;
}
table img{

max-height: 100%;
max-width: 100%;
}

#cont table
{
margin-left:auto;
margin-right:auto;
max-width:300px;
float: right;



}
#cont
{
margin-left:auto;
margin-right:auto;

max-width: 100%;


}
tr > td 
{

padding-bottom: 0.5em;


}
@media only screen and (max-width: 480px) {
 #cont
 {


margin:60px;
}


}
</style></head>

<body>

<div id="cont">
<table>
    <tr>
        <td>
            <img src="2.jpg"/>
        </td>
    </tr>
   <tr>
    <td>

 a
    </td>


   </tr>
    <tr>
    <td>

        a
    </td>


   </tr>
    <tr>
    <td>
        a

    </td>


   </tr>
</table>
<table>
    <tr>
        <td>
            <img src="2.jpg"/>
        </td>
    </tr>
   <tr>
    <td>

a   </td>


   </tr>
    <tr>
    <td>

        a
    </td>


   </tr>
    <tr>
    <td>
 a          
    </td>


   </tr>
</table>
<table>
    <tr>
        <td>
            a
        </td>
    </tr>
   <tr>
    <td>

 a      </td>


   </tr>
    <tr>
    <td>

a   </td>


   </tr>
    <tr>
    <td>
a           </td>


   </tr>
</table>
</div>
</body>

</html>

Answer №1

Take out the float:right property from #cont table. Center the table by centering the outer block in this way:

See Demo

CSS:

  table {
    max-width: 33%;
    height: 300px;
    background-color: white;
    margin: 15px;
  }
 #cont table { 
    display:inline-block;
    max-width:300px;
    text-align: center;
  }

#cont {
    text-align: center;
    margin-left:auto;
    margin-right:auto;
    max-width: 100%;
 }

Answer №2

update your #cont with the following code:

#cont {
    margin-left:auto;
    text-align:center; /*keeping text centered*/
    margin-right:auto;

    max-width: 100%;
    /*Removed the float:right;*/
}

this solution worked for me: JSFiddle

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

Can someone assist me in successfully incorporating a web font into a template?

After downloading a Wordpress template, I found the font used for h1 and h2 text to be difficult to read due to its slim design - Century Gothic. However, I prefer using Noteworthy, a font available on my Mac. I converted Noteworthy from OTF to web-font st ...

Gauging Screen Size: A Comparison between Media Queries and JavaScript for Adjusting Div Position

I am currently facing an issue with the banner on my website. It contains a slider and has a position set to absolute. The problem arises when viewing it on smaller screens, as only the left side of the wide banner is visible. Initially, I tried using med ...

Increasing the size of text in CSS with the use of ":hover" and then causing it to return to its original smaller size

Allow me to explain my goal here. I have text displayed on my website that I would like to enlarge when the user hovers over it and then shrink back down when they move their cursor away. The issue I'm facing is that after enlarging the text using t ...

Elevate the block when encountering errors in HTML/CSS

Here is a picture of what I want, and below I will provide a more detailed description: Link to the image (I cannot paste it here) I am explaining the elements involved. There are three main components: 1) The topmost text "Bla bla bla bla" 2) The butt ...

Using "-webkit-overflow-scrolling: touch" can cause issues with the CSS 3D perspective rendering

Looking for a solution specifically for iOS Safari Using -webkit-overflow-scrolling: touch seems to disrupt the 3d perspective on iOS devices. Check out the demonstration on CodePen. HTML <div class="pers"> <div class="scroll"> <di ...

Dropdown Menu in Bootstrap fails to display any items

My customized bootstrap navbar dropdown is behaving oddly in my custom navbar. Any thoughts on why this is happening and how to resolve the issue? The screenshot below illustrates the problem - the dropdown menu items are not visible unless I hover over th ...

What is the best way to eliminate the border in IE edge that surrounds special characters?

Here is the current appearance of the cross button in IE/Edge: https://i.sstatic.net/JZ37V.png And here is how it is supposed to look: https://i.sstatic.net/Uqafg.png Upon inspecting the CSS, it reveals that the #10006 html character has a color code o ...

HTML: Accept only images in PNG, GIF, and JPG format for file upload

How can I customize the HTML upload dialog window to only show JPG, GIF, and PNG files when users upload an image? ...

Finding the closest button using Selenium in Python

I am attempting to click on the "Delete Comment" button once I have identified a comment that contains the specific hashtag, husky, which is a clickable link. Since there are multiple "Delete Comment" buttons on the page, my approach is to locate the comm ...

CSS // code that works on any device or platform

I have since the beginning been annoyed with the compatibility of CSS. Whats the best practice for coding css, that works with the most common platforms... ( IE7+, Firefox, Safari, Chrome AND iPad / iPhone, Blacberry, Android) Are there any list to be fo ...

Having trouble connecting local stylesheet on server?

Currently, I am developing an HTML-based game with Node.js, focusing on enhancing the front-end interface. While trying to include a CSS stylesheet, I encountered an unusual issue. The structure of my folders is organized as follows: C:\Users\m ...

Tips on customizing the appearance of the "time" input type

Are there any resources that explain how to style the input type "time" completely? I have been searching but unable to find a comprehensive guide with all the necessary style attributes! The only example I came across is: input[type="time"]{ /**styl ...

Switch up the Thumbnail Image based on the selected category

While testing a shopping site I created, I encountered an issue with changing the banners at the top of the page based on the category the user is viewing. This site is hosted on a server on my localhost, not using wordpress by the way. <div class=" ...

Tips for avoiding Blob data and GUIDs from showing up in your browser's title

Currently, my process involves creating a PDF file on our server and displaying it in the browser using javascript as shown below: file = new window.Blob([data], { type: 'application/pdf' }); var fileUrl = URL.createObjectURL(file); var wn ...

How can I retrieve an attribute from another model in Ember using the current handlebar in the HTML file?

I'm attempting to achieve the following: {{#if model.user.isAdmin}} <div> My name is {{model.user.name}} </div> {{/if}} within a handlebar that is being used in a controller unrelated to users: <script type="text/x-handlebars" data- ...

Achieve hidden and visible elements using CSS without redundancy

Is there a way to hide a div on specific devices and show it on others without resorting to duplicating the code or using CSS hacks? This is my current approach: <div class="container"> <div class="row"> <div class="col-md-8 d-none d- ...

Trouble with activating Bootstrap panel

I have integrated bootstrap with Angular, but I am facing an issue with the panels not displaying correctly. After verifying that the files are in the correct locations,here is a screenshot of how it currently looks and this is the expected result. While ...

Troubleshooting issues with lxml Xpath functionality

Struggling to extract information from a webpage code provided below. While I managed to retrieve the users using xpath, I am facing difficulty in obtaining their scores. Any suggestions on what might be going wrong here? import requests from lxml impor ...

Crafting an interactive SVG element that maintains its clickability without interfering with mouseLeave events

Trying to Achieve: Changing color and displaying SVG when hovering over a row Clicking the SVG triggers a function, including external functions (not limited to ones defined inside script tags) Returning the row to its original form when mouse leaves Cha ...

PHP functionality to generate a table with provided data, followed by enabling the user to access the information by

I have a web page created with HTML that utilizes PHP to retrieve information from a database and populate it into a table. How can I set it up so that when a user clicks a button, a specific action is performed based on the data in the table using PHP? I ...