What is the best way to horizontally center my HTML tables and ensure that they all have uniform widths?

I'm currently facing an issue where I'd like to center align all tables similar to the 'Ruby Table.' However, I'm unsure of how to achieve this.

You may also observe that some tables have varying widths. What CSS rule can I apply to adjust the width of the entire table so they are consistent?

Below is my HTML code:

<!DOCTYPE html>
<html>

<head>
  <title>Personal Bests</title>
  <link rel="stylesheet" type="text/css" href="css/home.css">
  <meta charset="UTF-8">
</head>

<body>
  <main>

   <div id="section">
      <article>
        <h2>FireFalcons1 Best Times</h2>
     <p>
    Below you will find all of my best times on Time Trials Max Speed (100%) and Story Mode (100%)
    </p>
    <p class="TA">
    TIME ATTACK - MAX SPEED (100%)
      </article>
    </div>

<!--Ruby HMTL Table-->

<table class="rubyTable">
  <tr>
    <th class="rubyHeader" colspan="6">Ruby Cup</th>
  </tr>
  
  <!-- More table content here -->
  
  </table>

<!--Sapphire HMTL Table-->

  <table class="sapphireTable">
    <!-- Sapphire table content goes here-->
  </table>

<!--End of Table-->
    <hr>
    <footer>
      <strong>
                Copyright &copy; 2016 Stephen Fawcett, All rights reserved
        </strong>
    </footer>
  </main>
</body>

</html>

And here is my CSS related to 'Personal Bests':

 
/* Personal Bests CSS */
.TA {
    /* Custom styles for TA */
}

.rubyTable {
    /* Styling for Ruby Table */

}
/* Additional CSS for other tables */

.tg .tg-mzmv {
    /* Styling for specific elements */
}

Please focus only on the CSS located under the "Personal Bests CSS" comment.

Answer №1

Here is a helpful suggestion:

.amethystTable, .topazTable, .opalTable, .rubyTable, .emeraldTable {
    width: 90%;
    margin: 0 auto;
}

Answer №2

Starting off, it might be beneficial to end your

<p class="TA"> with a </p>, 

This way, you can begin testing out your CSS styles and properties.

Answer №3

To horizontally center a static block-level element in CSS, you can utilize the margin:auto; property.

.rubyTable, .sapphireTable, .diamondTable {
    /* add any necessary table class names above */
    margin:auto;
}

Please note that in this shorthand version, no top or bottom margin is specified for clarity. It is recommended to refer to the documentation on Margin and shorthand CSS syntax.

This method automatically sets the left and right margins to centrally position the object, in this case, a table. Since tables are block-level elements by default, their width is 100%. Therefore, it's advisable to also specify a width value along with the margin property for values greater than zero.

For instance, if you want to assign a specific width and margin to ALL table objects, regardless of their class, you can target the table element type in your CSS:

table {
    width:80%;
    margin:auto; 
    min-width:300px; /* for mobile screens */
}

It's important to note that the reference to "table" in the above code does not have a preceding period (.), indicating that it targets an element rather than a class rule.


Optimizing Repetition:

Upon reviewing your question, it appears that there is a significant amount of repetition in your HTML code, such as having multiple instances of the same class name like rubyData scattered throughout. You can streamline this using CSS3 and the direct descendant selector (>).

.rubyTable > tr:nth-of-type(1n+3) > td:first-of-type {
    /* equivalent to .rubyTrack CSS properties */
}
.rubyTable > tr:nth-of-type(1n+3) > td:not(:first-of-type) {
    /* equivalent to .rubyData CSS properties */
}

The first rule targets the nth-of-type direct descendant within the .rubyTable class starting from the third occurrence. It then styles the first td element accordingly.

The second rule styles all subsequent td elements (excluding the first one) following the defined pattern. This approach eliminates the need for repetitive class assignments in your HTML markup.

By restructuring your code as demonstrated above, your HTML becomes more concise, organized, and easier to manage.

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

What is the process for converting HTML assets into a SCORM package?

**css styles fonts images media menu1_images menu2_images menu3_images menu4_images** index.html index_custom.js index_actions.js menu1.html menu1_custom.js menu1_actions.js menu2.html menu2_custom.js menu2_actions.js menu3.html menu3_custom.js menu3_actio ...

How can I make a Material UI element fill the entire remaining width of the Grid?

Is there a way to make the child element take up the remaining width of the grid in Material UI? I am trying to have the "User Name" text field fill up the rest of the space in the grid cell but so far I have not been successful. I have even attempted us ...

an online platform design

I am currently working on building a webpage, but I am facing some issues with the layout. Specifically, I am unable to make the div automatically adjust its size (particularly the height). Here is the code that demonstrates the problem: <!DOCTYPE html ...

What strategies work well for guiding individuals to different destinations based on their user roles, such as administrators and regular users?

In my CMS environment, I am facing a challenge with redirecting users who are not administrators or do not own the document they're trying to edit. The following code snippet is what I have implemented, but it seems that my administrators cannot acces ...

Why Does my Overlay Keep Bouncing when Using JQuery to slideDown / slideUp Text on Image?

Can anyone help me replicate the text overlay effect that is seen when hovering over an image on The Guardian website? I have almost got it working, but whenever my mouse moves over the trail-text area, it starts bouncing (slideDown slideUp) repeatedly. I ...

Tips for incorporating a local image using file:// in JavaFX WebKit

I am facing an issue with displaying a picture in a JavaFX webview using local HTML. Despite writing the code, I am unable to show the image. The URL of the image is "file:/D:/a.jpg". //Main WebViewBrowser.java public class WebViewBrowser extends Appli ...

What is the process for customizing the image size of a WordPress RSS feed in pixels?

Is there a way to customize image sizes beyond the 'thumbnail', 'medium', and 'large' options? I tried setting width and height in the <img> tag within an RSS file, but it didn't work. Could it be a nested quotation ...

What is the process for integrating three.js code manually into an iframe?

I recently posted a question on Stack Overflow inquiring about how to input code into an iframe without using a file or URL. While I was successful with simple cases like <h1>Hello World</h1>, my ultimate goal is to integrate three.js into thes ...

Numerous Levels of Dropdown Menus

I am looking to implement a feature on a web page where users can select multiple vehicles using JQuery. The idea is that selecting the brand in the first dropdown will populate the second dropdown with the models available for that specific brand. The ...

What is the best way to assign a unique ID to every element in this situation?

Here is the given code: var words = ['ac', 'bd', 'bf', 'uy', 'ca']; document.getElementById("wordTable").innerHTML = arr2tbl(2); function arr2tbl(ncols) { return words.reduce((a, c, i) => { ...

"Customize the appearance of your theme by adjusting the background and text colors when making a

Is there a way to dynamically change the style of a webpage based on user selection using CSS in AngularJS? I've searched online, but haven't found a solution that works for me. I have a CSS file with different styles and I want to apply these ...

Currently in motion post file selection

I am currently facing an issue with a button that triggers a file selector pop-up. Below is the code snippet: <button mat-raised-button (click)="inputFile.click()">Choose a file</button> <input #inputFile type="file" [style.display]="' ...

What are the steps for showcasing a personalized HTML tag on a web page

I need to capture user input text and display it correctly. This is what I have attempted: <div> <input type="text" ng-model="post.content" /> </div> <div> <div ng-bind-html="post.content| htmlize"></div> < ...

CSS guidelines for layering shapes and divs

Currently, I am in the process of developing a screenshot application to enhance my understanding of HTML, CSS, and Electron. One of the key features I have implemented is a toggleable overlay consisting of a 0.25 opacity transparent box that covers the en ...

What is the highest possible z-index value that can be applied in

Is there a specific limit to the CSS property z-index? Are there variations in accepted values among different browsers? How do browsers typically handle extreme z-index values? I recall reading about a potential maximum value for z-index somewhere, but I ...

What is the method for tallying CSS page breaks in printed HTML documents?

Currently, for my report generation process using HTML and CSS to manage page breaks dynamically. I've enabled the option for users to print multiple reports at once by combining them into different sections within a single HTML document. This helps p ...

What is the best way to vertically align a pseudo element image using CSS?

After looking for similar questions, none of the answers seem to be working for my specific issue. This is what I'm struggling with: I am attempting to insert and center a decorative bracket image before and after certain content in the following man ...

What are some ways to enhance the specificity of HTML parsing code?

I'm currently facing a challenge where I need to extract hrefs and titles from an HTML page while only focusing on a tags that have both attributes. Some of the a tags do not have a title attribute. How can I modify my code to only display data from t ...

Blazor components experience element interaction while utilizing more than one instance of Blazorise.Bootstrap component

I am facing an issue in my Blazor app where a component with a button and bootstrap collapse works fine when used once on a page, but triggers collapse elements in other instances when used multiple times. This seems to be happening because their IDs are s ...

Is there a way to obtain the coordinates of an SVG element by simply clicking on a specific point?

I'm still learning about SVG and I'm trying to trigger an event that captures the click coordinates when clicking on the SVG. I have a few questions: Since I'm using Angular, I'm unsure if it's possible to keep my function in th ...