The widths specified in the td tags do not take effect on tables with auto width

Check out this fiddle: http://jsfiddle.net/LHsLM/1/.

I'm attempting to set the height and width of an auto-width table using inline CSS, as shown in the fiddle. However, the styles are not taking effect.

Here's the HTML:

<div id="a"></div>

CSS:

div {
  height:300px;
  width:500px;
  overflow:auto;
}

Javascript:

var str = ["<table>"];
for (var i = 0; i < 100; i++) {
  var tr = "<tr>";
  for(var j = 0; j < 100; j++){
    tr += "<td style='width:120px;height:40px'>" + j + "</td>";   
  }
  tr+="</tr>";
  str.push(tr);
}
str.push("</table>");

document.getElementById('a').innerHTML = str.join('');

Answer №1

Transform your td elements by changing width to min-width.

We regret to inform you that this solution is not compatible with IE-8.

However, here is a method that also works for IE8:

function createTable(){
var html = ["<table>"];
for (var i = 0; i < 100; i++) {
    var tr = "<tr>";
    for(var j = 0; j < 100; j++){
        tr += "<td><div style='width:120px;height:40px'>" + j + "</div></td>";   
    }
    tr+="</tr>";
    html.push(tr);
}
html.push("</table>");
document.getElementById('a').innerHTML = html.join('');
}

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

Issue with strange behavior of CSS cursor with images

Is there something blatantly obvious that I'm missing here? My goal is to replace the cursor css property with a png, as shown in this example here I was able to get it working with the 'happy.png' demo, but for some reason it won't wo ...

Angularfire allows for easy and efficient updating of fields

Currently, I am working on creating a basic lateness tracker using AngularFire. So far, I have successfully added staff members to the miniApp and set the initial late minutes to a value of 0. My challenge lies in updating these values. Ideally, when a us ...

I was assigned to calculate and transfer the length of the string within the parentheses (written in javascript)

function formatString(str) { const formatted = str.split(',') .map(subStr => `${subStr}(${subStr.length})`) .join(', '); return formatted; } The expected output was "hello(5), world(5), abra(4), carabfa(7), r ...

Tips for loading nested JSON data into an Angular Material dropdown list

My task involves extracting data from a JSON object and displaying the difficultyLevel. Despite several attempts, I have been unable to achieve the desired outcome. What changes should be made to the HTML file? const ELEMENT_DATA: data = { questions ...

Toggle JavaScript Query Display

Welcome to my HTML test website where I am testing show/hide JavaScript functionality. Upon loading the page, you may notice that everything is hidden until a button is clicked. <html> <head><title>Test</title> <scr ...

How does webpack identify the index.html file as the entry point for loading the bundle.js file?

I've noticed that without specifying a command to load index.html, webpack is automatically loading the page whenever I make changes in a file. Below are the attached files: webpack.config.js and package.json webpack.config.js var config = { entry: ...

Function for verifying presence of empty nested elements (comprising of Arrays, Sets, Strings, and Maps)

I am currently exploring ways to develop a solution using plain JS (without relying on any external libraries) that can determine whether a given input is considered 'empty' or not. Below are the code snippets and test cases I have prepared for ...

Guide on catching network errors using Axios request interceptor?

After encountering issues with our Vue application, I observed in the Sentry logs that the problem may stem from an unreliable network: Error: Network Error Error: Request aborted I wanted to display a warning message to the user but couldn't find a ...

Three.js - Display a model with material that can be visible without the need for a light source

I am currently utilizing Three.js, specifically version 71. My workflow involves creating models in Blender, exporting them as JSON files, and then using THREE.JSONLoader to incorporate these models into my scene as shown below: this.jsonLoader.load(path ...

I am looking to extract a specific value from my JSON file

I need to extract specific values from a JSON file. For example, if the JSON file contains id, name, and lastname, I am interested in extracting the id value. fs.readFile("user.json", function (err, data) { if (err) throw err; console.log(data. ...

Disable the button on page load condition

In the Mysql PHP setup, there is a page displaying a list of students with student_id, name, birthdate, and various buttons on each row. The objective is to make a certain button inactive if the student_id is found in another table called 'saral_tciss ...

Working with decimal numbers in jQuery and JavaScript: a basic guide

Following a button click, I have implemented a function that updates the displayed number: var initial_price = parseFloat($("#current_price").text()); var added_price = initial_price + 1.01; $("span#current_price").text(added_price); This is the ...

Is there a way to conceal the loading screen until the website has completely loaded?

I've been working on my personal portfolio/website and encountered a bug that I can't seem to fix on my own. The issue is with the logo (aa) and text under it showing even after the content page has fully loaded, taking around 3 seconds to hide. ...

Retrieve only the names of keys from a JSON object presented in array form using Node.js

How can I retrieve JSON data key names and store them in an array using nodejs? I attempted the following code, but it returned an object instead of an array. I need the key names in an array format so that I can save them in a variable. const jsondata = ...

Ways to target only the adjacent div

My goal is to target only the next div with the class name indented. Each indented div should have the same id as the <li> element right before it. Currently, I want each div with the class name indented to have the id of the previous <li>. He ...

Performance comparison between Ember and Angular.JS in rendering a large table

I am planning to construct a substantial table containing a plethora of data (approximately 2000 elements <td>). I intend to include functions for calculating values based on model, but without incorporating any bindings. Primarily, my goal is to s ...

The alignment for Bootstrap's NAV element within a display:table-cell element seems to be off when viewed on Firefox浪

Here is an example of HTML markup with Bootstrap library included: <div class="container"> <div class="card"> <ul class="nav list"> <li class="element"> <a href="#">Element</a> </li> ...

Utilizing a dropdown menu in HTML to dynamically alter the URL of a button using JavaScript

I attempted to search for a solution on stackoverflow, but I found the answers confusing. In essence, I am looking to create a list from which I can select a value that will change a link when clicking a button. I believe JavaScript is necessary for this ...

Use the zoom feature on D3 to enhance two graphs in an HTML document

I have been experimenting with d3 libraries lately and came across http://bl.ocks.org/jroetman/9b4c0599a4996edef0ab. I successfully used it to draw a graph based on data from a tsv file and enable zoom in and out functionality, which worked well for me. Ho ...

Unable to navigate to the top of the page after logging in with Angular.js

There is a problem I need help with. After a user successfully logs in and enters the home page, the page automatically scrolls down due to angular.js. I want the page to remain at the top when the user reaches the home page. Can someone assist me in res ...