How can I line up columns in an HTML table?

I am facing a challenge while trying to set up a basic form with a table in the initial stages of my Angular project. I have been struggling to get the columns to align properly.

Despite numerous attempts with various styling options for HTML <table> elements such as <tr>, 'width', 'float', and experimenting with vertical bars, I still can't seem to achieve the desired alignment.

The code I have been working on is heavily inspired by a W3Schools demonstration page available at https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_intro. It is puzzling that despite following the example closely, the columns do not line up and the alternate row colors are also not appearing.

<style>
  table {
    border-collapse: collapse;
    width: 5%;
  }

  td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
    width: 50%;
  }

  tr {
    width: 100%;
  }

  tr:nth-child(even) {
    background-color: #522a2a;
  }
</style>

<form style="margin-left: 2%; margin-right: 2%">
  <h3>Form</h3>
  <table>
    <tr>
      <th>Category</th>
      <th>Input</th>
    </tr>
    <div *ngFor="let key of memberKeys">
      <tr>
        <td>{{key}}</td>
        <td><input type="text" [(ngModel)]="member[key]" [ngModelOptions]="{standalone: true}"/></td>
      </tr>
    </div>
  </table>
  <button type="submit">Submit</button>
</form>

I am perplexed as to why the columns are misaligned and the rows are not alternating background colors. Any advice would be greatly appreciated!

Answer №1

Instead of a tr, there is a div inside the table element:

<table>
    <tr>[...]</tr>
    <div *ngFor="let key of memberKeys">

To fix this issue, try relocating the div so that it is not within the table but still follows the structure of sibling elements.

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 best way to add spacing between the fields within a Bootstrap 4 form row that spans across 2 rows when viewed on small displays?

Trying to create a bootstrap 4 form with fields in one row for larger screens and two rows for smaller screens. The attempt was made using the following code: <div class="form-group row"> <label class="col-sm-2 col-form-label">File:</lab ...

What is the best way to modify the colors of two specific elements using a combination of CSS and JavaScript

I am currently developing a browser-based game and have encountered an issue. Here is the relevant line of my HTML/PHP code: echo '<div id="div'.$n.'" class="d'.$rand.'" onclick="swapit(this.id, this.className)"></di ...

Tips for sending a String[] to my Angular 2 HTML template

In the export class of my component, I have a string array variable declared like this; barChartLabel:string[] = ['2006', '2007', '2008', '2009', '2010', '2011', '2012']; To display t ...

What is the best way to convert an array into a JSON format in Angular 8?

I am attempting to convert my array into a JSON format where it will display the Skill Score for each element in the array. I attempted to accomplish this by using the .map function and then outputting the result as a JSON string, as demonstrated in the c ...

What is the best way to apply a margin to the initial and final markLabel in a MUI Slider component?

I've integrated MUI's Slider component into my React project in the following manner: <Slider id={name} aria-label="Restricted values" defaultValue={defaultValue} getAriaValueText={valuetext} ...

The spinning wheel goes round and round while executing the location.reload() function

Currently, I am performing actions in my JavaScript function and then triggering a page refresh using location.reload(); I'm wondering if there is a straightforward method with jQuery to display a spinning wheel from the moment the page initiates the ...

Toggle textboxes using jQuery depending on the radio button choice

I'm trying to make specific textboxes appear when a particular radio button is clicked on my form, but I want them to remain hidden otherwise. Here's an example of what I've implemented: HTML: Radio buttons: <p>Show textboxes<inpu ...

PHP error "Attempting to access a non-existent variable: mysql_fetch_assoc"

I've encountered the error above when attempting to execute this code. I've experimented with various fixes, including using fetch_array too: $conn = mysql_connect('localhost', '-----', '-----','-----') o ...

Scraping Data: Uncover JSON data embedded in HTML code

I need help extracting what appears to be JSON data embedded within an HTML script. The script is presented like this on the webpage: <script> $(document).ready(function(){ var terms = new Verba.Compare.Collections.Terms([{"id":"6436","name ...

Arrange the contents within a div with Bootstrap 4 for proper alignment

Just stepping into the world of front end development and my question might come off as quite basic. Currently, I am delving into ReactJS. https://i.sstatic.net/o9tDo.png My current challenge is related to the following code snippet: <div className=" ...

Django has the ability to display images from static files, however, it is not capable of

I am facing an issue with my Django project where my static files (css and images) load properly when referenced in an html tag, but when I try to set a background image using CSS (background: url('images/mypic.png') or url('../images/mypic. ...

Tips for setting up a listener for when the month changes in an ion-datetime object

When the user selects a different month, I need to update the highlightedDates by calling a query to retrieve all the dates of that specific month. This currently works if the user manually chooses a month and year using the top button, but not when they c ...

Issue with pre-selected checkboxes: JavaScript

A function was created to automatically check the first value of a checkbox when a page is loaded: function defaultCheck(){ document.checkBoxForm.list[0].checked = true; var val=document.checkBoxForm.list[0].value; showtotal[0] = docum ...

Hover Effects on Facebook Tabs

I am currently facing an issue with implementing CSS-sprite for image-based rollovers within a Facebook Tab page. Strangely, the background images are getting removed by Facebook only when inside a Tab page and not in the main application itself. It seems ...

Issue with Internet Explorer 7: Floating elements are incorrectly positioned below their intended placement

Upon visiting using IE7, you may notice that the two middle columns are being pushed down to the bottom of the page. I have attempted to resolve this issue by applying display:inline to both columns without success. Any assistance on this matter would be ...

Unconventional tendencies of Select component in Vue.js

I have a basic Vue component with two separate sections. In each section, users can make selections from dropdown menus that update the variables value1 and value2. The issue arises when transitioning from "stepOne" to "stepTwo", as unexpectedly the value ...

Developing an array in Angular on an Android device is proving to be a sluggish

I'm facing an issue with my simple array that collects rows from a database and uses a distance column as a key. let output = {}; for (let dataRow of sqllite.rows) { output[dataRow.distance] = dataRow; } When testing in Chrome browser on my PC, ...

Step-by-step guide to retrieving the value of a duplicate input field with identical IDs in Angular4

This is the form I am working on: <button (click)="M_add()">Add</button> <tbody id="_tbody"> </tbody> Whenever the add button is clicked, it triggers the creation of an input field. let tbody = document.getElementById("_tbody"); ...

How can I use CSS to conceal the controls for an HTML5 video?

Seeking advice on how to hide and show controls on an HTML5 video section. I currently have a setup where clicking on the video div opens a modal that plays the video in a larger size. The code for opening the modal and playing the video is working fine. ...

Tips for keeping my radio button selected even after clicking the submission button?

I am just starting out with coding and need some help with a questionnaire that requires selecting two choices. I can select an option and see the correct response, but the choice does not stay selected after hitting submit. Can anyone advise how to make ...