Ensure the width of an HTML table by using only the <td witdth> tag

How can I set a fixed width for each column in my HTML table without using the width property in CSS?

The current code is not displaying the table properly, with it only rendering at 100% width of the screen. Here's a snippet of the relevant code:

#example_table {
  border-collapse: collapse;
  table-layout: fixed;
}

.zui-wrapper {
  position: relative;
}

.zui-scroller {
  overflow-x: scroll;
  overflow-y: visible;
  width: 100%;
}
<div class="zui-wrapper">
  <div class="zui-scroller">
    <table id="example_table">
      <thead>
        <tr>
          <th style="width: 75px;">Col1</th>
          <th style="width: 50px;">Col2</th>
          <th style="width: 200px;">Col3</th>
          <th style="width: 70px;">Col4</th>
          <th style="width: 70px;">Col5</th>
          <th style="width: 70px;">Col6</th>
          <th style="width: 70px;">Col7</th>
          <th style="width: 100px;">Col8</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Ex1</td>
          <td>Ex2</td>
          <td>Ex3</td>
          <td>Ex4</td>
          <td>Ex5</td>
          <td>Ex6</td>
          <td>Ex7</td>
          <td>Ex8</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

I have attempted to set the width manually for each row, but I do not want to use the width property under #example_table CSS as some rows are dynamically hidden through JavaScript.

Various combinations of overflow properties in CSS classes and IDs have also been tested, but I am still encountering issues. Any assistance on this matter would be highly appreciated!

Answer №1

To specify the width of columns, you can utilize the <colgroup> tag. Take a look at this example:

.tg {
  border-collapse: collapse;
  border-spacing: 0;
}

.tg td {
  border-style: solid;
  border-width: 1px;
}

.tg th {
  padding: 10px 5px;
  border-style: solid;
  border-width: 1px;
}
<table class="tg" style="undefined;table-layout: fixed; width: 700px">
  <colgroup>
    <col style="width: 75px">
    <col style="width: 50px">
    <col style="width: 200px">
    <col style="width: 70px">
    <col style="width: 70px">
    <col style="width: 70px">
    <col style="width: 70px">
    <col style="width: 100px">
  </colgroup>
  <tr>
    <th>Column1</th>
    <th>Column2</th>
    <th>Column3</th>
    <th>Column4</th>
    <th>Column5</th>
    <th>Column6</th>
    <th>Column7</th>
    <th>Column8</th>
  </tr>
  <tr>
    <td>Example1</td>
    <td>Example2</td>
    <td>Example3</td>
    <td>Example4</td>
    <td>Example5</td>
    <td>Example6</td>
    <td>Example7</td>
    <td>Example8</td>
  </tr>
</table>

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 line up a number and text using CSS?

Creating a basic gauge meter with min value 0 and max value 2. The current UI progress is as follows: .sc-gauge { width:200px; height:200px; margin:200px auto; } .sc-background { position:relative; height:100px; margin-bottom:10px; background-color:g ...

Choosing a request date that falls within a specified range of dates in PHP Laravel

In my database, I currently store two dates: depart_date and return_date. When a user is filling out a form on the view blade, they need to select an accident_date that falls between depart_date and return_date. Therefore, before submitting the form, it ne ...

What is the best way to display a header element in front of an article element?

I'm struggling with making my header element sticky and appear in front of my article. Modifying the z-index hasn't given me the desired result so far. Is the z-index ineffective when dealing with floating elements? Or is there a workaround to m ...

CSS3 Animation: Facing issue with forwards fill behavior in Safari when using position and display properties

After creating a CSS3 animation to fade out an element by adjusting its opacity from 1 to 0 and changing the position to absolute and display to none in the last frames, I encountered an issue. In Safari, only the opacity is maintained while the position a ...

Is it possible to have an input field that is read-only without altering the mouse icon

echo '<input type="text" class="form-control" value="' . $server->address . ":" . $server->connection_port . '">'; Can readonly mode be activated for this input without altering its CSS or the mouse icon when hovering over ...

Creating a unique layout with CSS: layered divs

I'm struggling with a stubborn layout issue that I just can't seem to figure out... - Left column - Fixed, Static (always in view) - Right column - Fluid/liquid/scrollable --- Header - fixed --- left/main fluid/liquid --- Right/sidebar - fixed ...

Implementing CSS to Style Images in JavaFX FXML

After creating the FXML structure below, I attempted to define a border in CSS for the Image by using code in the customerform.css file: <VBox id="customerFormPane" styleClass="customerForm" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.exampl ...

Is there a way to automatically update the input value when I refresh the page following a click event?

Currently, I have multiple p elements that trigger the redo function upon being clicked. When a p element is clicked, the quest[q] template is loaded onto the .form div. These templates are essentially previously submitted forms that sent values to an obj ...

Having EventListeners set up across a single CSS class returns null when applied to different types of elements simultaneously

I want to create floating labels that resize dynamically when an input is clicked, similar to modern forms. I am using vanilla JS exclusively for this task. Currently, the setup works with the <input> tag. However, it does not work with the <text ...

Tips for populating an HTML input with the value of a link element using JQuery

I have a form input that I want to populate with autocomplete suggestions from my database using Ajax. To style the list, I am using bootstrap 4, and I have opted to use the <a></a> tag instead of the <li></li> tag. This is because ...

Singling out a particular navigation tab

When attempting to link specific table IDs so that the corresponding tab is active when opened (i.e. www.gohome.com/html#profile), I am facing an issue where the active tab remains unchanged. Even after specifically calling out tab IDs, there seems to be n ...

Struggling to float <aside> to the left of <article> for proper alignment?

tldr; I'm attempting to position the aside-section to the left of the article-section (similar to a sidebar/side column), but my "float" property doesn't seem to be working at all. Is there a way to achieve this without modifying the HTML code an ...

How can I display pure HTML in a Pyramid view?

I am completely new to Pyramid (and relatively inexperienced with web frameworks in general). My goal is to reach a point where I can retrieve raw HTML from a view in order to format data fetched from my mongoDB database. The __init__.py in my Pyramid pr ...

Highlighted Navigation Options

When visiting , take note of the top navigation bar. The background color on this page is white, but as you navigate to other pages, the tab changes to a different color. How can you modify this behavior and change the class? Is PHP or jQuery necessary f ...

When the ::after pseudo element is utilized for creating a bottom border, it will display following each individual list item contained within the specified div

I have been using a pseudo-element to create a division line between sections, and it was working perfectly until I added a list of items. Now, the division line is appearing after every <strong> and <li> tag. I have tried various approaches, s ...

Stable placement in browsers using webkit technology

Having trouble with fixed positioning on webkit browsers. Works fine in Firefox, but can't seem to solve the issue. You can see the problem here: When clicking on a project, a page is loaded using jQuery's .load() function. On this page, there ...

jQuery .click() only triggering upon page load

I've been searching all over the place and I just can't seem to find a solution to my specific situation. But here's what I'm dealing with: Instead of using inline HTML onclick, I'm trying to use jQuery click() like this $(docume ...

How can I redirect after the back button is pressed?

I am currently working with a trio of apps and scripts. The first app allows the user to choose a value, which is then passed on to the second script. This script fetches data from a database and generates XML, which is subsequently posted to an Eclipse/J ...

Instructions on separating an array into two table rows "<tr>" with a single foreach loop on the template side

Given the constraints of my working environment, I must divide this array into two separate table rows - one containing half of the data and the other with the remaining half. The array is already sorted in the order that I require: Array ( [product] ...

CSS transform: applying the same CSS to multiple divs results in a variety of different outcomes

I am working on creating multiple parallelograms with the same skew aligned horizontally. While the first one looks perfect, additional divs seem to increase the skew more and more. I even attempted this using images and applying transform: rotate() but en ...