Create a dynamic table with varying colored cells using HTML and CSS

I currently have a table setup where a td element can be styled with a single background color:

https://i.sstatic.net/DoP4f.png

This is achieved using the following code:

<td colspan="1" style="background: cyan; position:relative;" ><span class="CellComment" class="smallCell">Version 1.0</td>

However, in specific cases, I wanted to divide the td into multiple colors. I attempted this by adding several div elements inside the td, each with its own background color (each div should take up equal space within the td).

It needs to be dynamic so that the same code can handle both the one-color and multi-color scenarios. If there's only one color, the corresponding div should fill the entire td.

The code I used for this approach is:

<td colspan="1"><div style="background-color:cyan; position:absolute;"></div><span class="CellComment" class="smallCell">Version 1.0</td>

(the span represents a popup that appears when hovering over the text)

Despite my efforts, the outcome was not as expected:

https://i.sstatic.net/jyss5.png

Even when using just one div, it only occupies half of the available space within the td.

Answer №1

Want to add multiple colors inside a single div (or td) without any hassle? Simply use

background: repeating-linear-gradient(90deg, #DF0000, #DF0000 50px, #000000 50px, #000000 100px)
.

#normal{
 background-color: black;
}
#split {
  background: repeating-linear-gradient(90deg, #DF0000, #DF0000 50px, #000000 50px, #000000 100px)
}
#split3{
background: repeating-linear-gradient(90deg,#DF0000, #DF0000 33px,#4286f4 33px, #4286f4 66px,#000000 66px, #000000 100px)
}
div{
  width: 100px;
  height: 100px;
  margin: 10px;
  float: left;
}
<div id="normal">
</div>
<div id="split">
</div>
<div id="split3">
</div>

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

Tips for altering value by selecting radio buttons

Currently, I am in the process of developing a website using Adobe Dreamweaver with php. The site includes three buttons that serve as payment method options and act as continue buttons. My goal is to have the selected radio button (I agree button) add a s ...

What is the process of securing a directory with a password using Node.js and Express?

I am in the process of creating a basic web server using Node.js and Express. My aim is to host various static files, one of which includes a Unity WebGL game. In order for the game to function properly, numerous assets need to be loaded. I am interested ...

Whenever I include an onClick event to a div element, the entire webpage fails to display

Currently taking on the task of developing a seat booking website. I am encountering an issue with adding an event listener to a particular div element, which should trigger a function to store the value of the div. However, upon implementing the onClick e ...

Error encountered when attempting to utilize a variable within an EJS template document

I seem to be encountering some difficulties while attempting to get an EJS template file to recognize a variable that holds the rows from an SQLite3 table query in a related .js file. Whenever I try to access that route by launching the server, I receive a ...

What is the correct method for handling images in HTML and CSS?

Hey there! Just playing around on jsfiddle and wanted to share this Destiny-inspired creation: http://jsfiddle.net/3mfnckuv/3/ If you're familiar with Destiny, you'll see the inspiration haha.. But here are a few questions for you: 1) Any ide ...

How can I identify when a browser window is maximized using JavaScript or CSS?

I am currently working on a dashboard designed for static display on large monitors or TVs for clients. My main goal is to implement CSS styling, specifically a snap-scroll feature, but only when the display is in 'fullscreen' or 'maximized& ...

Issues with Thunderbird not displaying copied HTML emails

Hello there amazing people at Stackoverflow. I need some assistance with HTML formatting. I am currently working on a bootstrap modal that is being dynamically modified through jQuery using the append() function. Check out the code snippet below: <div ...

Troubleshooting a JQuery accordion malfunction within a table

I am populating the data dynamically. However, the Accordion feature is not functioning correctly. JSFiddle link http://jsfiddle.net/aff4vL5g/360/ Please note: I am unable to modify the HTML structure. Current table Desired output The first accordio ...

Issues with displaying search form div using jQuery AJAX

Currently, I am in the process of developing a search form that includes a dropdown menu. The functionality involves selecting an item from the dropdown menu triggering an AJAX call to another PHP file. This call returns tags which resize both the dropdown ...

Trouble Aligning HTML Button to the Right

I'm currently working with this HTML code. My goal is to have the "Get Geotag" button aligned to the right side, with the text positioned anywhere to the left or above the button. However, I'm struggling to properly align the buttons and maintain ...

Is there a way to shift this structure to the right without relying on the float property?

Here's the HTML and CSS I have - .font-size-add-class { background-color: #2f2f2f; color: #f9f7f1; } .font-sizes { text-align: right; display: table; margin-top: 15px; cursor: pointer; } .font-sizes > .small { font-size: 12px; } .font ...

CSS3: What is causing this issue in FireFox?

http://jsfiddle.net/tNg2B/ It's clear that there is a noticeable difference when viewing this in Chrome compared to Firefox. The ":before" and ":after" pseudo classes seem to be malfunctioning in Firefox. Is there a way to resolve this issue? Edit: ...

Meteor: utilizing the background image for the body

I'm working on a Meteor project that has various Routes set up. Within one of my css files, I currently have the following styling for the body: body { font-family: 'Roboto', sans-serif; font-size: 17px; font-weight: 400; c ...

Tips for updating a div element while maintaining its style and select2 plugin functionality with jQuery

Within the HTML code, I am attempting to refresh the following div: <select name="test[]" id="test" multiple required class="select2"> @foreach($tests as $s) ...

What is the best way to create spacing between images in a CSS image slider?

UPDATE: I've made significant progress by modifying the code provided in the link below. I am very close to achieving my desired result, but there are parts of it that still puzzle me. The transition now has spacing on both sides and the hidden space ...

Issue with jQuery: Changing colors does not work on Chrome and Safarirowsers

I'm puzzled as to why the code snippet below only works in Firefox, but not in Chrome and Safari. Can you shed some light on this issue? if ($(this).css("color") == "Fuchsia"){ $(this).css("color","#000000"); } Here is the link in question: If you ...

What is the best way to handle images overflowing a div

I am currently working on creating a carousel using React, where the content images are aligned horizontally. If I have 3 image tags within a div with a width of 1200px, how can I effectively control these images to ensure they fit within the container div ...

Reduce multiple paragraphs in PHP to a more concise form

Is there a way to trim paragraph content to 100 characters, especially when dealing with multiple paragraphs? For example, consider the following HTML snippet: <div class="div-1"> <div class="section-1"> <p>"Lorem ipsum ...

swapping between two lines within a div

i'm developing a new music app and I am in need of the following chord progression: Am F G Am Ab Gdim A# Hello these are just placeholder lyrics for demonstration purposes, hey hey hello which should look li ...

Show a table when a button is clicked using Javascript

Undertaking a project called: Tennis Club Management involving javascript, HTML, CSS, and bootstrap. The project includes a Login Page (index.html) and a Manage Players Page (managePlayers.html). Within the managePlayers.html, there are two buttons - Add P ...