Applying the margin auto property and setting display to inline block within a while loop

I am utilizing a while loop to showcase various groups of people in different tables.
I have discovered that when I use inline-block, the margin auto function does not work.
I would like these two tables to be displayed inline and centered in the browser window. Below is my code:

<table style="display: inline-block; margin: auto;" class="table table-striped table-bordered" >
<thead>
     <tr>
        <th colspan="2">
        Manager Group 2
        </th>
    </tr>
    <tr>
        <th>Manager Name</th>
        <th>User Details</th>
    </tr>
</thead>
 <tbody>
    <tr>
        <td>Kelvin</td>
        <td><a href="admin_edit.php?id=<?php echo $mrow["id"]; ?>" >User Details</a></td>
    </tr>
</tbody>
</table>

<table style="display: inline-block; margin: auto;" class="table table-striped table-bordered" >
<thead>
    <tr>
        <th colspan="2">
        Manager Group 1
        </th>
    </tr>
    <tr>
        <th>Manager Name</th>
        <th>User Details</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>James</td>
        <td><a href="admin_edit.php?id=<?php echo $mrow["id"]; ?>" >User Details</a></td>
    </tr>
</tbody>
</table>

Answer №1

margin: auto; does not have any effect on elements with display: inline-block;. To center these elements, you will need to specify a unit value for the margin such as pixels, ems, rems, etc. The element should be set to display block or table (for tables) in order for margin auto to work effectively.

If you are trying to center two tables side by side, it is recommended to wrap each table in its own <div> with a maximum width specified. Then, you can style these divs to arrange your tables accordingly. Here is a basic example provided below:

.txt-center {
  text-align: center;
}

div > div{
  border: 1px solid;
  display: inline-block;
  margin-bottom: 12px;
  max-width: 300px;
  vertical-align: bottom;
  padding: 8px;
}

table {
  border: 1px solid;
  min-width: 300px;
}

td {
  border: 1px solid;
}
<div class="txt-center">
  <div class="example">
    <table>
      <tr>
        <td>
          example
        </td>
        <td>
          example
        </td>
      </tr>
    </table>
  </div>
  <div class="example">
    <table>
      <tr>
        <td>
          example
        </td>
        <td>
          example
        </td>
      </tr>
    </table>
  </div>
</div>

Answer №2

margin: auto won't have the desired effect when applied to a table with a display of inline-block. This is because elements with inline-like behavior, such as tables with inline-block, do not create additional horizontal space for margins like block-level elements. Therefore, setting margin: auto on an inline or inline-block element effectively means no margin will be applied.

To center a table with a display of inline-block, you can instead use text-align: center on the parent container of the table. Below is an example for centering tables within the body:

body {
  text-align: center
}

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

Problem: Challenge in Iterating and Assigning Variables to HTML Table Elements

I am currently working on a project that involves tracking the order statuses of individual shipments from a list of URLs stored in an Excel sheet. The code I have written is capable of looping through the URLs and extracting information from them successf ...

The reason I am unable to extract information from the elements within the <ul> tag

I am currently attempting to extract reviews data from booking.com that is contained within the <ul> tag with the class attribute set to "review_list". There are a total of 10 reviews, each of which is inside an <li> element with the class attr ...

Is there a way to create a table cell that occupies zero space?

I have a very intricate table setup that resembles a tree structure, with only a few cells on the left and many on the right. I am looking to completely hide specific cells, along with their entire rows, using JavaScript based on their class. Using visibi ...

Hover over the text below to see an image appear in its place

I'm looking to swap out some text with an image when hovering over it, using basic HTML/CSS. NO: show the image below the text NO: display the text on top of the image NO: replace one image with another YES: replace the text with an image ...

The issue of drop shadows causing links to not work properly in Internet Explorer

I am currently working on a website design that features a fixed menu positioned behind the body. When the menu icon is clicked, some jQuery code shifts the body to the left. To create the effect of the fixed menu being positioned underneath, I have added ...

I have tried implementing "display:inline-block" but my DIV stubbornly refuses to stay on the same line

I need to align two block elements horizontally, one being a FORM and the other a DIV. <div id="miningArea"> <form id="curWorkForm">...</form> <div id="buttonDescription"> To start, click the "Start" button. < ...

Maximize the YouTube video for full screen viewing

Take a look at the code snippet from our page here: <div className={styles.videoPreview}> <iframe src={`${videoData[router.locale]}?autoplay=0`} title="Flytime" ...

What is the best way to extract text from a dynamically changing element using jQuery?

I've been struggling with a coding issue. Despite trying numerous approaches, I keep encountering the same problem where every new button I add ends up having the same text or, alternatively, nothing seems to work as expected. $j serves as my variabl ...

Tips for adjusting the CSS styles within a jQuery plugin

I need some help with styling the form on my test page located at While using FireBug, I noticed a lot of padding space surrounding the tabs within this div: <div id="tabs-1" class="ui-tabs-panel ui-widget-content ui-corner-bottom"> How can I redu ...

Utilizing jQuery's Chained Selectors: Implementing Selectors on Previously Filtered Elements

DO NOT MISS: http://jsfiddle.net/gulcoza/9cVFT/1/ LATEST FIDDLE: http://jsfiddle.net/gulcoza/9cVFT/4/ All the code can be found in the above fiddle, but I will also explain it here: HTML <ul> <li id="e1">1</li> <li id="e2" ...

CSS Border Animation Enhances the Entire Div Components

I have a div with an animated pulsing border, achieved through CSS keyframes. The issue I am facing is that the animation affects the entire content within the div, whereas I want it to apply only to the border itself. Here's my code: <div class=" ...

Tips for building a modal box with HTML, CSS, and jquery!

Looking to create a modal box using HTML, CSS, and jQuery? <button id="myBtn">Click here to open the Modal</button> This code will help you achieve that: <!-- This is the Modal content --> <div class="modal-content"> <sp ...

Using jQuery UI to create sortable lists that are connected and have hidden overflow features

How can I hide overflow from two fixed-height divs containing sortable lists connected to each other without affecting their connection? For example, if overflow is set to hidden, the list item doesn't display when dragged outside of the div. I' ...

Incorporate a map (using leafletjs or Google Maps) as a subtle backdrop

I am currently working on a one-page website and I would like to include a map as a background behind the "contact" section. The map can be set to float, draggable, or positioned at the back. I have experience using both the Google Maps API and LeafletJS, ...

Having trouble adding a button to a GridLayout in my NativeScript application

My attempt to use a Button alongside some Images in a single row within a GridLayout resulted in the Button covering the entire row along with the images. Here is the HTML code I used: <GridLayout columns="4*,*,*,*,*,*" rows="*"> <Button text ...

Determining if a URL links to an image when the file extension is not informative

I am currently working on building an AJAX call to retrieve data from an API with a messy data structure. The challenge I'm facing is that the array returned by each AJAX call can contain up to 30 elements, some of which have image URLs without a file ...

What is the best way to extract and analyze CSS code from an HTML page using Java programming language

I'm looking to extract and parse the content of HTML pages using the Jsoup API, which can be found at . However, I've encountered a challenge in extracting CSS that is located in a different folder. How can this be achieved? Currently, my code ...

Speeding up the loading time of my background images

body { background: url(http://leona-anderson.com/wp-content/uploads/2014/10/finalbackgroundMain.png) fixed; background-size:100% auto; } I have unique background images on each of my sites, but they are large in size and take some time to load due to bein ...

Using a CSS button to activate a JavaScript function: A step-by-step guide

My current project involves writing a script to change the color of text when a specific button is clicked. The idea is that clicking the "Change color1" button triggers the text color change using the following code snippet: <button onclick="myFunction ...

The border-color feature in Bootstrap 5 does not function properly when applying responsive border-start/end/top/bottom styles

My Objective: For mobile: I want to have borders only at the top and bottom. For desktop: I want borders only at the start and end, in a border-dark color. My Attempts: I have added custom responsive borders to my main.scss file // responsive borders @ ...