Insert circular shapes of varying colors within the cells of a table

Take a look at the table image here

Is there a way to insert colored circles inside the table data cells?

I attempted with CSS but struggled with positioning.

.circle{
  height: 25px;
  width: 25px;
  background-color: #bbb;
  border-radius: 50%;
  display: inline-block; }

Do I have to create individual classes for each color? Having separate classes for different colors can lead to lengthy CSS code.

What is the best method to achieve this?

table {
    border-collapse: collapse;
    table-layout: fixed;
    width: 100%;
    font-size: 13px;
}

th {
    font-size: 14px;
    font-weight: 400;
    color: #888;
    border-bottom: 2px solid #000;
    padding: 10px 8px;
}

td {
    border-bottom: 1px solid #ccc;
    color: #000;
    padding: 6px 8px;
    text-align: center;
}
<table>
    <thead>
       <tr>
          <th>Column 1</th>
          <th>Column 2</th>
       </tr>
    </thead>
    <tbody>
       <tr>
          <td>Data</td>
          <td>Black</td>
       </tr>
       <tr>
          <td>PEWA</td>
          <td>Yellow</td>
       </tr>
       <tr>
          <td>Data</td>
          <td>Blue</td>
       </tr>
       <tr>
          <td>AFW</td>
          <td>Orange</td>
       </tr>
       <tr>
          <td>YSW</td>
          <td>Red</td>
       </tr>
       <tr>
          <td>GWG</td>
          <td>Green</td>
       </tr>
       <tr>
          <td>BFD</td>
          <td>Blue</td>
       </tr>
       <tr>
          <td>VHY</td>
          <td>Violet</td>
       </tr>
       <tr>
          <td>GWY</td>
          <td>Grey</td>
       </tr>
       <tr>
          <td>WDA</td>
          <td>Yellow</td>
       </tr>
    </tbody>
 </table>

Answer №1

Looking for a solution like this?

Check out this link

table {
    border-collapse: collapse;
    table-layout: fixed;
    width: 100%;
    font-size: 13px;
}

th {
    font-size: 14px;
    font-weight: 400;
    color: #888;
    border-bottom: 2px solid #000;
    padding: 10px 8px;
}

td {
    border-bottom: 1px solid #ccc;
    color: #000;
    padding: 6px 8px;
    text-align: center;
}
.circle {
   height: 25px;
   width: 25px;
   border-radius: 50%;
   display: inline-block;
}

.td-content {
    display: flex;
    display: flex;
    justify-content: center;
    align-items: center;
 }

 .black {
      background-color: #000;
      margin-left: 2px;
 }

Answer №2

To add colors to the text, you can use span elements for each color and wrap them inside a div. Then, style them using display: flex for alignment and inline styles for the color:

table {
  border-collapse: collapse;
  table-layout: fixed;
  width: 100%;
  font-size: 13px;
}

th {
  font-size: 14px;
  font-weight: 400;
  color: #888;
  border-bottom: 2px solid #000;
  padding: 10px 8px;
}

td {
  border-bottom: 1px solid #ccc;
  color: #000;
  padding: 6px 8px;
  text-align: center;
}

.color {
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-width: 55px;
  max-width: fit-content;
  margin: auto;
}

span {
  display: inline-block;
  height: 10px;
  width: 10px;
  border-radius: 50%;
}
<table>
  <thead>
    <tr>
      <th>Head 1</th>
      <th>Head 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Data</td>
      <td>
        <div class="color">
          Black <span style="background-color: black;"></span>
        </div>
      </td>
    </tr>
    ... (additional rows with different colors)
  </tbody>
</table>

If you prefer a cleaner HTML structure, consider using a Sass for loop for setting colors dynamically:

$colors: #000000, #ffff00, #0000ff, #f5a002, #ff0000, #008000, #0202f9, #ee82ee,
    #808080, #fcfc06;

@each $color in $colors {
    $i: index($colors, $color);

    tr:nth-of-type(#{$i}) span {
        background-color: $color;
    }
}

Answer №3

If you're looking to streamline your code, there are two approaches you can consider:

  1. Directly set the background-color attribute in HTML, which may result in longer HTML but avoids interfering with the CSS file.
  2. Set the background-color attribute using a specific class (e.g., .bg-green for setting the color to green) in the CSS file for flexible application across various elements in the HTML.

Another tip is to use the tag with either block or inline-block positioning to easily control text alignment. For example, changing

<td>Black<span></span></td>
to
<td><span>Black</span><span></span></td>
, and then applying vertical-align: middle to both span elements for vertical centering.

For a detailed demonstration, refer to the snippet below where the first two rows of the table utilize the first method while the remaining rows employ the second method:

table {
    border-collapse: collapse;
    table-layout: fixed;
    width: 100%;
    font-size: 13px;
}

th {
    font-size: 14px;
    font-weight: 400;
    color: #888;
    border-bottom: 2px solid #000;
    padding: 10px 8px;
}

td {
    border-bottom: 1px solid #ccc;
    color: #000;
    padding: 6px 8px;
    text-align: center;
}

/* custom */
.custom_table span{
    display: inline-block;
    vertical-align: middle; /* this attribute will middle align all the "span" tag at same level */
}
.custom_table .circle {
    margin-left: 5px; /* spacing the circle and the text */
    height: 15px;
    width: 15px;
    border-radius: 50%;
    display: inline-block;
}
.bg-blue {
    background-color: blue;
}

.bg-orrange {
    background-color: orange;
}
<table class="custom_table">
    <thead>
       <tr>
          <th>Head 1</th>
          <th>Head 2</th>
       </tr>
    </thead>
    <tbody>
       <tr>
          <td>Data</td>
          <td><span>Black</span><span class="circle" style="background:black;"></span></td>
       </tr>
       <tr>
          <td>PEWA</td>
          <td><span>Yellow</span><span class="circle" style="background:yellow;"></span></td>
       </tr>
       <tr>
          <td>Data</td>
          <td><span>Blue</span><span class="circle bg-blue"></span></td>
       </tr>
       <tr>
          <td>AFW</td>
          <td><span>Orange</span><span class="circle bg-orange"></span></td>
       </tr>
    </tbody>
 </table>

Hope this explanation proves helpful!

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 get rid of a tooltip from a disabled button

I am facing an issue with my button that is disabled using ng-disabled. The problem is that the button still shows a tooltip even when it is disabled. I need to find a way to remove the tooltip when the button is disabled. Check out my Button ...

In WordPress, you can add a logo with accompanying text positioned at the bottom of the page

Can someone help me create a header with a logo and text positioned at the bottom next to it? I want the text to be aligned with the bottom of the image. Any suggestions on how to achieve this? Here is the logo image: https://i.sstatic.net/W2S0U.jpg And ...

When the parent element is already set to justify its content with flexbox, you can ensure equal heights for CSS classes by utilizing

Within my design, I have several rows with 3 columns each. Every column contains a main content section labeled SavedSearchBox-content (with various permutations that affect its height) and a footer labeled SavedSearchBox-footer. To structure this layout, ...

What is the strategy to load a div exclusively when triggered by a click event, instead of loading it

Can anyone assist me with a problem I am facing on my scripting page? I am currently working on a website that showcases properties. I would like to know how to prevent a specific div from loading when the page initially loads, and instead have its content ...

Steps for rendering Emoji in React Application

I'm looking to integrate emojis into my webpage within a React chat app. The idea is to provide users with a list of emojis to choose from. How can I use codes like '1F683' to display emojis on the page, particularly with support for Chrome? ...

Create a design where two columns can be scrolled independently and extend to fit the height of the viewport

I have successfully created a JS Fiddle that demonstrates exactly what I am looking for: http://jsfiddle.net/j7ay4qd8/5/ This Fiddle features two columns that can be scrolled separately, and different sections of the left column can be easily scrolled to ...

Despite having unique ids, two file input forms are displayed in the same div in the image preview

Running into a minor issue once again. It may not be the most eloquent query, but I'm genuinely stuck and in need of some assistance. I am attempting to showcase image previews of selected files in a file input form. I have a jQuery script that reads ...

Can the text within an li element be accessed using Xpath even with varying attributes?

Instead of directly using: driver.FindElement(By.XPath("//ul[3]/li/ul/li[7]")).Text I am exploring ways to retrieve the text by utilizing Xpath along with different attributes like text(), contains(), and so on. //ul[3]/li/ul/li//[text()='My Data&a ...

Incorporating YouTube videos into Internet Explorer

On my webpage, I have included a YouTube video: <div class="col-md-8"> <iframe id="theframe" width="400" height="325" src="http://www.youtube.com/embed/MYYOUTUBECLIP" frameborder="0" allowfullscreen class=""></iframe> While this works p ...

Using JQuery and Bootstrap, add or remove rows dynamically

For my project, I am creating tests (in English, physics, etc.) using JSTL, Bootstrap, and jQuery. I need a form to create a Test with questions and answers. However, I am facing an issue. When I try to delete a question, the answers that were added are no ...

Edit script: Iterate through the different div elements at regular intervals AND pick a specific div by pressing a button

Check out this interesting discussion about displaying and hiding divs at specific time intervals using jQuery on the page here. The script mentioned is designed to loop through DIVs, showing one after the other while hiding the rest. It's titled "Lo ...

Steps for adding a div after a specified number

To display the following div after getting a value greater than or equal to the specified value and retrieving it through ajax: 1. How can I show the below div with the given value? .mainbox{ width:auto; height:auto; padding:20px; background:#f00; } .i ...

Can an identification be included in a label element?

My inquiry is as follows: <label for="gender" class="error">Choose</label> I am interested in dynamically adding an id attribute to the above line using jQuery or JavaScript, resulting in the following html: <label for="gender" class="err ...

Using AJAX to dynamically update a div's class following a POST request

Upon double clicking a row, I am attempting to trigger the function outlined below. Despite my efforts, I have not been able to update the div class with any changes. function book(id) { $.ajax({ type: "POST", url: "ajax_servlet. ...

I'm having issues getting my fancybox to display properly

HTML: <!--gallery--> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> <script> !window.jQuery && document.write('<script src="jquery-1.4.3. ...

Is it possible to invoke a JavaScript function from within a CSS file?

Currently, I am focusing on developing responsive web design and looking for ways to avoid creating multiple versions of each page based on screen width variations. The struggle lies in managing font sizes effectively. While attempting to style them using ...

Develop a form containing a date input in a special character-free format, along with a validation feature

I am looking to design a form that necessitates users to input a date in the following format: "ddmmyyyy". No slashes, dots, or any other special characters should be included. Only entries matching this specific format will be accepted as valid, any other ...

What is the best way to minify and concatenate multiple CSS files only after converting SCSS to CSS with Gulp?

When attempting to convert my scss files to css files using gulp, I encountered an issue. After writing the scss code, it is easily converted to css. However, I wanted to minify this css and save it in a different folder called 'min'. Within the ...

I need to access a webpage using VBA without relying on Internet Explorer for logging in

I need to access the website by logging in. However, I am facing difficulty in using it. Unfortunately, I am unable to share the username and password due to confidentiality reasons. Below is the code snippet that I have: Sub checkdownload() Dim u ...

Discovering components with identical html attributes in Selenium

I am currently developing a web crawler using Python 3.8. My goal is to convert the table below into a pandas dataframe using Selenium, Pandas, and BeautifulSoup. <table class="no_border_top" width="95%" align="center"> ...