css when designing a table, ensuring alignment can be challenging

How can I align these two tables perfectly?

<html>
    <head> 
        <style>
            #index_table, #index_table_header{
                text-align:left;
                margin:20px;
                margin: 0 auto;
                table-layout: fixed;
            }
            #index_table, #index_table_header{
                width:800px;
            }
            #index_table{
                display: block;
                height: 390px;
                overflow-y: scroll;
            }
            #index_table_header td{
                padding:10px 8px;
                width: 100px;
                border: 1px solid black;
            }
            #index_table td{
                padding:10px 8px;
                width: 100px;
                border: 1px solid black;
            }


            #index_table td:nth-child(1), #index_table_header td:nth-child(1){
                width: 80px;
                }
            #index_table  td:nth-child(3), #index_table_header  td:nth-child(3){
                width: 70px;
                }

            #index_table  td:nth-child(5), #index_table_header  td:nth-child(5){
                width: 200px;
                }  
            #index_table  td:nth-child(2), #index_table_header  td:nth-child(2) {
                width: 250px;
                }


        </style>       
    </head>
    <body>
            <table id="index_table_header">
                    <thead>
                        <tr>
                            <td>ID</th>
                            <td>Item</td>
                            <td>Amount</td>
                            <td>Added</td>
                            <td>Nutritional Value ID</td>
                            <td>Actions</td>
                        </tr>
                    </thead>
            </table>
            <table id="index_table">
                <tbody>
                    <tr>
                        <td>395</td>
                        <td>chicken liver</td>
                        <td>0.37</td>
                        <td>2019-10-14</td>
                        <td>67</td>
                        <td>
                            <a href="/delete/395">Delete</a>
                            <br>
                            <a href="/update/395">Update</a>
                        </td>
                    </tr>

    </body>
</table>
    </html>

a few things to think about

  1. I need the tables and table cells (columns) to be perfectly aligned
  2. The second table has numerous rows and I want it to be scrollable without the header disappearing from view, which is why I kept them separate.

Answer №1

It appears that the first table you mentioned actually serves as the header for the second table. In this scenario, you can distinguish between the header and data by using specific styling.

#index_table {
  text-align: left;
  margin: 20px;
  margin: 0 auto;
}

#index_table {
  width: 800px;
}

#index_table thead {
  display: block;
}

#index_table tbody {
  display: block;
  height: 390px;
  overflow-y: scroll;
}

#index_table th {
  padding: 10px 8px;
  border: 1px solid black;
  background-color: gray;
}

#index_table td {
  padding: 10px 8px;
  border: 1px solid black;
}

#index_table td:nth-child(1),th:nth-child(1) {
  width: 80px;
}

#index_table td:nth-child(2),th:nth-child(2) {
  width: 70px;
}

#index_table td:nth-child(3),th:nth-child(3) {
  width: 200px;
}

#index_table td:nth-child(4),th:nth-child(4) {
  width: 250px;
}

#index_table td:nth-child(5),th:nth-child(5) {
  width: 250px;
}
<html>

<body style="height: 100px">
  <table id="index_table">
    <thead>
      <tr>
        <th>ID</th>
        <th>Item</th>
        <th>Amount</th>
        <th>Added</th>
        <th>Nutritional Value ID</th>
        <th>Actions</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>395</td>
        <td>chicken liver</td>
        <td>0.37</td>
        <td>2019-10-14</td>
        <td>67</td>
        <td>
          <a href="/delete/395">Delete</a>
          <br>
          <a href="/update/395">Update</a>
        </td>
      </tr>
      <continue to replicate sample data structure here>
   </tbody>
  </table>
</body>

</html>

Answer №2

Feel free to utilize the following code snippet:

<!doctype html>
<html lang="en>

<head>
  <meta charset="utf-8>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous>
  <title>Hello, world!</title>
  <style type="text/css">
    body {
      margin: 0;
    }
    
    table {
      border-collapse: collapse;
      border-width: 0;
      display: block;
      overflow: auto;
    }
    
    td,
    th {
      border: 1px solid;
      padding: .5em 2em;
    }
    
    th {
      background-color: #dddddd;
    }
  </style>
</head>

<body>
  <div class="container">
    <table>
      <thead>
        <tr>
          <th>ID</th>
          <th>Item</th>
          <th>Amount</th>
          <th>Added</th>
          <th>Nutritional Value ID</th>
          <th>Actions</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>395</td>
          <td>chicken liver</td>
          <td>0.37</td>
          <td>2019-10-14</td>
          <td>67</td>
          <td>
            <a href="/delete/395">Delete</a>
            <br>
            <a href="/update/395">Update</a>
          </td>
        </tr>
        <tr>
          <td>395</td>
          <td>chicken liver</td>
          <td>0.37</td>
          <td>2019-10-14</td>
          <td>67</td>
          <td>
            <a href="/delete/395">Delete</a>
            <br>
            <a href="/update/395">Update</a>
          </td>
        </tr>
        <tr>
          <td>395</td>
          <td>chicken liver</td>
          <td>0.37</td>
          <td>2019-10-14</td>
          <td>67</td>
          <td>
            <a href="/delete/395">Delete</a>
            <br>
            <a href="/update/395">Update</a>
          </td>
        </tr>
        <tr>
          <td>395>/td>
          <td>chicken liver</td>
          <td>0.37</td>
          <td>2019-10-14</td>
          <td>67</td>
          <td>
            <a href="/delete/395">Delete</a>
            <br>
            <a href="/update/395">Update</a>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous></script>
</body>

</html>

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

Arrow with missing outline

https://i.sstatic.net/HUyIV.png Can anyone help me create a left-pointing arrow on a white background? I've tried using the following CSS, but it only gives me a single color border. .white-box{ padding: 20px; background: #fff; } .wh ...

Update the form field with today's date in a JHipster application

In our current project in JHipster, we are facing a challenge with setting the default value of a form field as the current date. JHipster offers a moment format for dates, which is essentially an ngbdatepicker. However, when attempting to change the inpu ...

Obtain JSON data from a web address using PHP

This question is quite common. I am looking to learn how to retrieve JSON data from a URL using PHP. Find the corresponding .php file below: <html> <head> <?php // $url = "https://localhost:8666/web1/popupData/dataWeekly.php"; ...

Calculate the quantity of rows within a specific div container

Looking at the image provided, there are multiple divs inside a main div. I am trying to figure out how many rows the main div contains. For instance, in this particular scenario, there are 5 rows. It is important to mention that the inner div is floated ...

Using Regular Expressions (or not) in Javascript/Jquery to append <span> around vowels

Seeking guidance as a beginner in the coding world. Currently, my code generates numbers from 1 to 99 and displays text next to the number based on certain conditions. For instance, numbers divisible by 3 are labeled 'Java', those divisible by 5 ...

What is the best way to ensure that an image perfectly fits within a div container?

My goal is to make my image occupy half of the div's width and 100% of the height. However, I am facing an issue with a lingering white bar at the bottom. Can you offer some guidance on how to resolve this? Take a look at the site preview .contain ...

Tips for adjusting the size of nav-pills when collapsing with bootstrap 5

I'm attempting to utilize Bootstrap to create active classes that display a nav-pill/container around the active section of my webpage. It works well on larger screens, but on smaller screens with the hamburger menu active, the nav-pill stretches acro ...

Locate a specific cell in a table while concealing the entire row

I currently have a table set up like this: <table> <tr> <td>Title</td> <td> Description<br> <input type="text" id="search" placeholder="Search description text"> ...

Updating an Image Using JavaScript

Having trouble changing an image using JavaScript. Followed instructions to use document.getElementbyId("image").src = "image2.jpg", but it's not working. Script is placed at the bottom after the image tag in the HTML. HTML code: ...

Could you provide guidance on how to toggle the visibility of a div using the click event of a button or link located on a different HTML page in Angular?

Is there a way to change the visibility of a div on a different page when a button or link is clicked? For example, I have a button on the "main.html" page that, when clicked, should display a hidden div on the "header.html" page. How can I achieve this? ...

Tips for making an interactive slider using JQuery

I need to create dynamic sliders within tabs that can change content dynamically. My development platform is ASP.NET MVC 5 and I'm using JSON data format. What's the best way to implement this dynamic slider feature for each tab? <div class ...

Creating a proper nth-child CSS for multi-level HTML elements involves understanding the structure of the elements

My current project involves a screen where widgets can be dynamically inserted into the DOM. I am looking to adjust the z-index based on the number of times the class decrement-z-index appears. This is the CSS code I have written in an attempt to achieve ...

Unable to stop default left alignment of layouts using Tailwind and Phoenix

Every page I create (whether liveview or not) seems to be aligned to the left by default, creating a large empty space on the right side of the page. I have checked the root.html.heex file for any embedded styles that might be causing this issue, but have ...

Change the JavaFX ToggleButton's color and revert back to its original color

Can you tell me what the default background and border color is for a toggle button? Here’s an example of code that changes the background color of a toggle button: i.toggle11.setOnAction(e->{ if(i.toggle11.isSelected()){ i.toggle ...

Utilize the HTML Tidy executable in combination with PHP, as opposed to compiling it

I have created a PHP application specifically for shared hosting environments. Dealing with the inconsistency of hosts providing HTML Tidy can be challenging for me. Is there a way to integrate the tidy executable directly into my PHP application and proce ...

What causes the preview pane in an embedded Stackblitz project to not show up?

I've been attempting to integrate Stackblitz projects into my Angular application. The project editor pane displays correctly, but the preview pane is not showing the preview. Instead, it's showing an error message that reads: Error occurred: Err ...

Animating the background position of a progress bar using CSS3 and Webkit

I am seeking a functional method to animate the background-position property of an HTML5 progress element in webkit/blink. HTML: <progress max="100" value="0"></progress> CSS: progress[value]::-webkit-progress-bar { -webkit-animation: mov ...

ul/div element not displaying background color

I am attempting to showcase navigation items horizontally within a blue colored ribbon. Despite my efforts, the background-color property is not being applied to the ul element. I even tried enclosing it in a div element with a blue background, but still n ...

Guide on uploading multiple images using AJAX and jQuery

Here is the code snippet I am currently working on, which allows users to upload one or multiple files and delete them. All the file data is stored in the variable form_data. Unfortunately, I have not been successful in getting the file upload functionali ...

What is the process for incorporating a custom data attribute into the serialization parameters in Gridster.js?

I am currently utilizing Gridster.js in conjunction with JQuery for the purpose of creating a widget container. My goal is to utilize the serialized_params property of Gridster.js to store additional attributes (such as owner and company) of the added widg ...