can a table tag without a specified width still be successfully centered?

While learning a trick to center an element, I came across an interesting method. The suggestion was to place the ul element inside a table and set the table's margin left/right to auto in order to center it. However, I found myself wondering why the table itself can be centered when it also has no width..

table{
    margin:0 auto;
}
<table>
  <tbody>
    <tr><td>
<ul>
    <li><a href="#">1</a></li>
        <li><a href="#">2</a></li>
        <li><a href="#">3</a></li>
    </ul>
    </td></tr>
  </tbody>
</table>

Answer №1

A table is considered a block level element by default, with an automatic width that adjusts to fit its content. If the content results in a width smaller than 100% of the table's containing block, applying left/right margins set to auto will center the table within that block.

If you define a specific width for the table, using margin: 0 auto will still center it as long as the specified width is less than the containing block width.

To Center the UL Without Resorting to Using a Table

Below are two examples. The first example sets a width for the ul, while the second example uses display: table to alter the behavior of the ul from a block element to a table-like element.

.ex1 {
  width: 50%;
  margin: 0 auto;
  border: 1px dotted gray;
}

.ex2 {
  display: table;
  margin: 0 auto;
  border: 1px dotted gray;
}
<h3>Example 1 - Specify a width</h3>
<ul class="ex1">
  <li>1</li>
  <li>2</li>
  <li>3</li>
</ul>  

<h3>Example 2 - Use a CSS table</h3>
<ul class="ex2">
  <li>1</li>
  <li>2</li>
  <li>3</li>
</ul>  

Answer №2

When working with tables, the width of the table as a block level element will expand to fit the content, unlike an unordered list.

Instead of placing the ul inside a table (which is not recommended for semantic reasons unless a table is specifically needed), you can achieve the desired layout by setting ul {text-align: center} and ul li {display: block}

ul {
    text-align: center;
}

ul li {
  display: block;
}
<ul>
    <li><a href="#">1</a></li>
        <li><a href="#">2</a></li>
        <li><a href="#">3</a></li>
</ul>

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

``How can I lock the top row of an HTML table containing input fields in place while

Below is the table structure: <table> <tr> <th>row1</th> <th><input type="text"></th> <th><input type="text"></th> <th><input type="text"></th& ...

Implementing a collapsible full-width button that contains multiple elements (Bootstrap)

I am currently in the process of developing a responsive calendar that will display additional information in an expandable "well" when the user clicks on a specific event. My goal is to have the button, date, and icon all appear on the same line at full ...

Changes in styling applied via ajax are only supported by Webkit when resizing the browser window

Can you help point me in the right direction? It's been bothering me for quite some time now. In my content editor (using javascript + jquery), I have the ability to change fonts on the fly. Whenever a font is changed in the editor, an ajax request is ...

Rotation of the SVG coordinate system distorts angles

My comprehension of SVG, particularly the viewport, viewBox, and the user coordinate system, seemed fairly solid. In the initial example provided below, we utilized a viewBox with a matching aspect ratio as the viewport. As expected, there was no distorti ...

Using jQuery to animate the grayscale filter with a value of 0

Currently, I am implementing the Waypoints jQuery plugin to add animation to a series of images as a user scrolls over them. My goal is to apply certain CSS properties - opacity:1, filter:grayscale(0), -webkit-filter:grayscale(0), and -moz-filter:grayscale ...

Remove interactive data tables from the onclick event

I have a dynamically rendered DataTable from https://datatables.net. I implemented an on-click event for the rows based on this tutorial: https://datatables.net/examples/advanced_init/events_live.html In the row, there is a select box that I excluded by ...

Stop using the bootstrap dl-horizontal class for responsive design

I utilized Bootstrap to design a list and used the dl-horizontal class in the following way: <dl class="dl-horizontal"> <dt>1</dt> <dd>deta1</dd> <dt>1</dt> <dd>deta1</dd> <dt>1</dt> ...

The CSS styling feature is not functional within the JavaMail API

After receiving an email sent using the JavaMail API, I noticed that the CSS styles are not being applied and only the HTML content is rendered. HTML CODE <!doctype html> <html lang="en"> <head> <meta charset="u ...

Ways to add Bootstrap Icon to Button

I am trying to create a button using the createElement method in JavaScript. I want to include a bootstrap delete icon within this button. This is my current code: const deleteButton = document.createElement('button'); deleteButton.innerHTML = & ...

When the flex-grow property is set to 1, the height of the div extends beyond the space that

Here is an example of some HTML code: .container { height: 100%; width: 100%; display: flex; flex-direction: column; padding: 10px; background-color: aqua; } .box { width: 100%; height: 100%; flex-grow: 1; background-color: cadetb ...

Tips for eliminating the gap separating the authentication design from the additional elements within the Laravel, Vue, and Inertia framework

I'm currently working with Laravel and Vue using Inertia. When I log into the system, the authentication layout creates a space at the top of the page. How can I resolve this issue? Here is an image highlighting the space I need to remove, marked wit ...

Animating a div using a changing scope variable in AngularJS

As a newcomer to Angular, I am looking to add animation to a div element using ng-click within an ng-repeat loop. Here is what I have attempted: app.js var app = angular.module( 'app', [] ); app.controller('appController', function($ ...

What is the reason for using <div class="clear"></div> instead of <div class="clear"/>?

It dawned on me that: <div class="clear"/> can wreak havoc on the layout after a sequence of floating divs, whereas <div class="clear"></div> works perfectly fine. Does anyone have an explanation for this? Here is the CSS being used ...

Mobile browsers consistently display a horizontal scroll bar

My website is displaying a horizontal scrollbar on Android devices in browsers like Chrome, Firefox, and Opera Mini. It does not show correctly on the Android Default browser, appearing broken. In contrast, when comparing my site to the full desktop versi ...

When attempting to load a CSS file, Java automatically redirects to the login page

EDIT** After successfully loading CSS, I encountered an issue where the page kept redirecting back to login.jsp instead of displaying the CSS. Any insights on what might be causing this?https://i.sstatic.net/Ij7Oh.png I attempted to incorporate a custom ...

Creating adaptable breakpoints for content using Bootstrap or pure CSS

I'm trying to create a responsive layout using the Bootstrap "row" and "col" classes, but I'm having trouble figuring out how to structure the content. Here's what I mean: https://i.sstatic.net/WkMmE.png This is my current HTML structure: ...

What is the best way to activate a scrollbar exclusively for the final section in fullpage.js?

Is there a way to enable a scrollbar for the last section only in fullpage.js? This is what I attempted: $("#section3").fullpage({ scrollBar: true, fitToSection: false, }); Unfortunately, it didn't work as expected. ...

Is the order of elements in a CSS file important?

In my situation, I am dealing with a nested list and enclosing div that was created by Drupal's menu system, so I am unable to modify it. My goal is to replicate the menu from a hardcoded website (link removed). How can I include the sub-items (ul l ...

Web pages occasionally fail to load HTML files

After embarking on my first HTML and CSS project, I am facing some challenges that I hope someone can help me with. Firstly, my CSS files are not loading properly - could anyone provide insight into why this might be happening? Secondly, I have noticed t ...

Navigational Dropdown implemented on Bootstrap 4 Navigation Bar

I'm encountering a problem with aligning a dropdown menu on the navbar-brand section of the Bootstrap 4 Navbar. Instead of being in the correct position, it's showing up at the top left corner of the page. https://i.sstatic.net/xU6Wc.png I&apos ...