Tips for utilizing the font-family style to span multiple columns

I recently discovered that you can set the background color across table columns using the <colgroup> tag.

table {
     border: 2px solid;
     border-collapse: collapse;
}
td {
     border: 2px solid;
}
<table>
    <colgroup>
        <col span="3" style="background-color:silver">
        <col span="2" style="background-color:green">
        <col span="1" style="background-color:blue">
    </colgroup>
    <tr>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
    </tr>
    <tr>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
    </tr>
</table>

The code above will give silver background to first three columns, green to the next two, and blue to the last column.

My question now is, can I apply other styles, specifically the font-family, to multiple columns in a similar way? If yes, how?

Answer №1

If you're looking to enhance your skills in JavaScript, here's a code snippet I've written:

<table id='myTable'>
    <tr>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
    </tr>
    <tr>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
    </tr>
</table>
<br> 

<button onclick="myCustomFunction()">Try it</button>

<script>
function myCustomFunction() {
let rows=document.getElementById("myTable").rows.length;

    for(let i=0;i<rows;i++)
    {
        // Style cells from column 1 to 3
        for(let j=0;j<4;j++)
        {
            document.getElementById("myTable").rows[i].cells[j].style.fontFamily='verdana';
            document.getElementById("myTable").rows[i].cells[j].style.background='silver';   
        }
        // Style cells from column 4 to 6
        for(let j=3;j<6;j++){
            document.getElementById("myTable").rows[i].cells[j].style.fontFamily='times new roman';
            document.getElementById("myTable").rows[i].cells[j].style.background='green';    
       }
       // Style the last cell
        for(let j=6;j<7;j++){
            document.getElementById("myTable").rows[i].cells[j].style.fontFamily='arial';
            document.getElementById("myTable").rows[i].cells[j].style.background='blue'; 
       }
    } 

}
</script>

Answer №2

Regrettably, the colgroup element does not support font-related properties. You are limited to using style properties such as border, width, visibility, and background. For more information on available options for styles, you can refer to this link.

To apply font styles within your table, you will need to individually specify them for each th or td tag. While this may be tedious, using proper style declarations instead of inline styling can help streamline the process.

.verdana {
  font-family:"verdana";
}
.arial {
  font-family:arial;
}
.times {
  font-family:times new roman;
}
<table>
  <colgroup>
    <col span="3" style="background-color:red">
    <col span="2" style="background-color:green">
    <col span="1" style="background-color:blue">
  </colgroup>
  <tr>
    <th class="verdana">verdana</th>
    <th class="verdana">verdana</th>
    <th class="verdana">verdana</th>
    <th class="arial">arial</th>
    <th class="arial">arial</th>
    <th class="times">times NR</th>
  </tr>
  <tr>
    <th class="verdana">verdana</th>
    <th class="verdana">verdana</th>
    <th class="verdana">verdana</th>
    <th class="arial">arial</th>
    <th class="arial">arial</th>
    <th class="times">times NR</th>
  </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

Bootstrap 5's ScrollSpy functionality seems to be malfunctioning

I attempted to implement the scroll-spy feature of Bootstrap 5 in order to highlight the corresponding navbar item as active while scrolling through different sections of my HTML page. However, I encountered an issue where the navigation-link item would be ...

Utilizing Javascript / jQuery to eliminate specific CSS styles

I am facing an issue with the CSS code for a table positioned at the bottom of the screen. The current code includes a filter specifically for IE 8, but I need to make it compatible with IE 10 as well by removing the filter and adding a background color. ...

Utilize a single JavaScript script to handle numerous HTML forms within the same webpage

I am currently facing an issue with a page that contains multiple forms needing the same JavaScript code. This specific code is designed to add more input fields to the form, which it does successfully. However, the problem lies in the fact that it adds in ...

Customize the CSS styling of third-party components in different pages using NextJS

When working with third-party components, you can include their styles by importing their stylesheet into your component or _app.tsx file. For detailed instructions on how to do this, you can refer to Next.js documentation. Another option is to add the sty ...

Increase division height when mouse hovers over it

I want the height of the div to be 50px by default and change to 300px onmouseover. I have implemented this as follows: <style type="text/css"> #div1{ height:50px; overflow:hidden; } #div1:hover{ height:300px; } </style> <body> <div i ...

Ways to activate the built-in HTML5 form validation without actually submitting the form

I have a form in my application that I plan to submit using AJAX. However, I would like to implement HTML5 for client-side validation. Is it possible to trigger form validation without actually submitting the form? Here is an example of the HTML code: &l ...

Tips for positioning a picklist field dropdown on top of a lightning card in Lightning Web Components

Attempting to resolve an issue with CSS, I have tried adding the following code: .table-responsive, .dataTables_scrollBody { overflow: visible !important; } However, the solution did not work as expected. Interestingly, when applying a dropdown pickli ...

CSS navigation bar (background gradient problem)

I have encountered an issue with my multi-tier navigation menu where tiers below tier 1 are displaying an unexpected block to the left of the options. I suspect this problem is related to the background gradient applied, but I haven't been able to fin ...

Positioning of DIV elements in relation to each other

I'm currently enrolled in Codecademy's HTML & CSS course and I'm finding the topic of positioning a bit perplexing. Here is an example of my HTML file: <!DOCTYPE html> <html> <head> <style> div { pos ...

Displaying or concealing HTML elements using AngularJS while a modal is open

Looking for a way to display a loading spinner icon on my page when a user triggers a button that opens a modal, and then have the spinner disappear once the modal is open and its content has loaded. Currently, I've managed to make the spinner show up ...

Ensuring that images are the perfect size for their parent container

Exploring the functionalities of angular bootstrap carousel, I am eager to showcase various images and enable clicking on them. The challenge I'm facing is that my images are not uniform in size and don't fit perfectly within my bootstrap column ...

Utilizing CSS classes to namespace pseudo-selectors

After attempting to search on Google, I am struggling to find the right keywords. I'm trying to apply a pseudo-selector only to a specific class. In this scenario, if we remove the specified class, the last Hi should appear in blue. However, my expe ...

Is there a function called 'onViewportChange' in media queries that triggers a search in the .css file for applicable styling?

So I'm not entirely sure about all the specifics, but my understanding is that each time a new HTML element is created, it checks through all linked CSS files and inline styles to see if those styles should be applied to that element. This suggests t ...

When hovering over the bootstrap dropdown, the main dropdown remains a clickable link

How can I create a bootstrap dropdown menu that activates on hover, while still allowing the main button to link to a different page when clicked? Below is the HTML code: <div class="body-wrap"> <div class="container"> <nav class="navbar n ...

Is there a way to hide a specific character within a span element as securely as a password input field using

Can I use CSS to mask a span character similar to an input type password? Are there any properties like type="password" for the span or a specific class in Bootstrap such as class="mask" that can achieve this effect? https://i.stack.imgur.com/b8WQ4.png ...

Issue: Keeping the mat-form-field element in a single line

I am facing an issue with incorporating multiple filters for each column in an angular material table. I cannot figure out why the filter input is not moving to a new line under the header. Here is an image for reference -> Angular material table with m ...

Tips for adjusting the width of Material UI TextField within a Table

I am utilizing a Material UI Table. This is how I construct it: tableValues.map((curRow, row) => { tableRows.push( <TableRow key={this.state.key + "_row_" + row}> {curRow.map((cellContent, col) => { let adHocProps ...

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 ...

Having issues with the presentation of full_numbers pagination in IE while using jQuery dataTables

I am currently utilizing the DataTables jQuery plugin (found at ) with the initialization code below: $('#reconcile_table').dataTable( { 'bSort' : true, 'bFilter' : true, 'bSortClasses' : false, &a ...

general declarations take precedence over media queries

One of my usual CSS rules looks something like this: #dayslist > div { height: 51px; } Then there's the media query CSS rule: @media (max-width: 979px){ #dayslist > div { height: 44px; } } However, whenever I resize my bro ...