Html table header with colspan

HTML Table Headers

HTML tables can have headers for each column or row, or for many columns/rows.

EMIL TOBIAS LINUS
8:00
9:00
10:00
11:00
12:00
13:00
MON TUE WED THU FRI
8:00
9:00
10:00
11:00
12:00
DECEMBER

HTML Table Headers

Table headers are defined with th elements. Each th element represents a table cell.

Example

Vertical Table Headers

To use the first column as table headers, define the first cell in each row as a element:

Example

Align Table Headers

By default, table headers are bold and centered:

Firstname Lastname Age
Jill Smith 50
Eve Jackson 94

To left-align the table headers, use the CSS text-align property:

Читайте также:  Пример

Example

Header for Multiple Columns

You can have a header that spans over two or more columns.

Name Age
Jill Smith 50
Eve Jackson 94

To do this, use the colspan attribute on the element:

Example

You will learn more about colspan and rowspan in the Table colspan & rowspan chapter.

Table Caption

You can add a caption that serves as a heading for the entire table.

Monthly savings

Month Savings
January $100
February $50

To add a caption to a table, use the tag:

Example

HTML Exercises

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

HTML colspan Attribute

An HTML table with a header cell that spans two columns:

Definition and Usage

The colspan attribute defines the number of columns a header cell should span.

Browser Support

Note: Only Firefox supports colspan=»0″ , which has a special meaning (look below in the «Attribute Values» table).

Syntax

Attribute Values

Value Description
number Sets the number of columns a header cell should span. Note: colspan=»0″ tells the browser to span the cell to the last column of the column group (colgroup)

❮ HTML

tag

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

CSS Colspan: How To Create Multiple Columns in Your HTML Tables

Position Is Everything

How to use css colspan

CSS colspan is an attribute that allows you to dictate the number of columns a given table cell can occupy. If you are stuck about creating numerous columns, then look no further! Read this article, and we will show you how CSS colspan works along with teaching you how to create complex table structures.

Keep reading to finetune your CSS knowledge even more!

  • What Is Colspan?
  • How To Use Colspan
    • – Values Accepted by Colspan
    • – Web Browser Support for Colspan
    • – Example 1
    • – Example 2
    • – Note
    • – Example 3
    • – Example of Applying Styles to CSS Colespan
    • – Creating an Invoice Table
    • – Creating a Multicolumn Header
    • – Inputing Employee Details in a Table

    What Is Colspan?

    Colspan is an HTML attribute defined as colspan, and you can use it to control the number of columns of a table cell.

    Simply, when you need to answer the question “how can a single table cell occupy more than one column?” your answer is using colspan!

    How To Use Colspan

    When you have an HTML table, you can attach the colspan attribute to any table cell. After that, you need to give it a value that can determine the number of columns the cell should occupy.

    – Values Accepted by Colspan

    The value of colspan is a non-negative integer greater than one, and its default is one.

    – Web Browser Support for Colspan

    At the time of writing, all modern browsers support colspan.

    Examples of HTML Tables With Colspan

    – Example 1

    In this code snippet, we created an HTML table that has a single HTML header with a colspan value of two:

    When you view it in the browser, the result is as follows:

    • The “First Name” column expands to two columns
    • All corresponding values of “Phone Number” are empty

    – Example 2

    In this case, we applied the same colspan value to the header corresponding data in the table.

    – Note

    A colspan value greater than the number of possible columns in the table will push other cells away. Also, there is a tendency you’ll have table headers with non-corresponding table data, so do keep that in mind/

    – Example 3

    In the next code block, the “First Name” column has colspan=”5″. When you count the number of columns in the table, you’ll arrive at four columns. In this scenario, the web browser will interpret colspan=”5″ as colspan=”4″. Lastly, five is the largest number of possible columns in the table:

    The web browser will ignore colspan=”0″. Firefox and Chrome will treat colspan=”1000″ as the largest number of columns. For Internet Explorer (IE), it will obey this large value, which will lead to a massive overflow in the browser. The fix is to apply a border-collapse.

    table border-collapse: collapse;
    >

    Can I Apply Styles to CSS Colspan?

    Yes, you can style colspan in CSS. All you need is a CSS attribute selector to manipulate the appearance of the CSS table colspan.

    – Example of Applying Styles to CSS Colespan

    In the next code block, the HTML header, “First Name”, has a colspan attribute:

    The next CSS code will style all the “First Name” that has a valid colspan value.

    [colspan] background-color: #ddd;
    >

    What You Can Do With Colspan

    Colspan opens the possibilities to create complex table structures. Just a few of those are:

    – Creating an Invoice Table

    An invoice table shows the result of sales. You can use colspan to replicate one, with little CSS for styling.

    The next HTML code is the code for the invoice table:

    Next, the CSS in the next code block will change the table’s appearance:

    main width: 70%;
    margin: 2em auto;
    >
    table overflow: hidden;
    border: 1px solid #ccc;
    width: 100%;
    border-collapse: collapse;
    padding: 0.2em;
    >
    th, td border: 1px solid #000000;
    padding: 1.2em;
    >
    th text-align: left;
    background-color: #0000000d;
    >
    caption margin-bottom: 2em;
    >

    – Creating a Multicolumn Header

    A multicolumn will span many columns and, as you’ve learned earlier, this is achievable with colspan.

    Moreover, the next code block is the HTML for the multicolumn header:

    The next CSS block will make the table more presentable:

    main width: 70%;
    margin: 2em auto;
    >
    table overflow: hidden;
    border: 1px solid #ccc;
    width: 100%;
    border-collapse: collapse;
    padding: 0.2em;
    >
    th, td border: 1px solid #000000;
    padding: 1.2em;
    >
    th text-align: left;
    background-color: #0000000d;
    >
    caption margin-bottom: 2em;
    >

    – Inputing Employee Details in a Table

    You can create a table showing employee details, so you’ll just need to make some table headers that span over many columns.

    The HTML for the employee details table is next, and you can adjust it to your needs:

    Employee Details

    Hardware Marketing Human Resource Software Others
    Department Embedded Mechanical Indoor Outdoor Onsite Offside PHP Python Accounts Assets Canteen
    H.O.D Martinez Maxwell Simon Johnson Maverick Dos Santos Victor Micheal Sanchez Ruiz John
    Experience (in years) 15 8 4 10 4 3 2 11 13 12 18
    No of Teams 7 7 3 2 2 2 5 5 5 5 5
    Assigned Projects/Task 5 12 20 14 10 8 8 5 7 3 5
    No of employees 34 25 2 5 4 10 182 150 75 82 6

    The next CSS code will change the table’s appearance in the web browser.

    main width: 70%;
    margin: 2em auto;
    >
    table overflow: hidden;
    border: 1px solid #ccc;
    width: 100%;
    border-collapse: collapse;
    padding: 0.2em;
    >
    h1 text-align: center;
    >
    th, td border: 1px solid #000000;
    padding: 1.2em;
    >
    th text-align: left;
    background-color: #0000000d;
    >
    caption margin-bottom: 2em;
    >

    Colspan Not Working: What You Can Do

    When colspan fails to work in your HTML tables, you can check for the following:

    • Ensure colspan has a value, as an empty string will not work.
    • Colspan value is a positive integer, thus negative values are not allowed.
    • Colspan value is not a decimal, so don’t input such a value.

    Difference Between Colspan and Column-span

    Colspan is an HTML attribute that works on HTML table cells. Whereas, CSS column-span is a property used in a CSS multicolumn layout. Moreover, with column-span, you can span an element across many columns.

    For example, in the next code blocks, there is a CSS multicolumn layout created with a CSS column span. The heading spans across all three columns in the layout, which is possible with column-span: 3:

    This heading spans all three columns

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua.

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua.

    .multi-column background-color: #dddddd;
    padding: 2em;
    column-count: 3;
    column-rule: 5px solid #1560bd;
    >
    h1 outline: 2px solid red;
    column-span: all;
    >

    Conclusion

    In this article, you learned a lot about colspan and its values along with its differences compared to CSS column-span. Here is a summary of everything we taught you in this article:

    • Colspan is an HTML attribute that dictates the number of columns for a cell
    • Colspan accepts positive values
    • Colspan ignores negative and decimal values
    • Firefox and Chrome interpret colspan=”1000″ as the table’s greatest number of columns
    • Internet Explorer (IE) will obey colspan=”1000″. This results in an overflow in the browser
    • Border collapsing will let Internet Explorer interpret colspan=”1000″ like Firefox and Chrome
    • Colspan is an attribute, whereas CSS column-span is a property
    • Amongst other applications, you can create an invoice table with colspan

    Css colspan implemented in html tables

    Now that you have all this information about CSS colspan, you can start creating complex tables. You can also definitely make use of our examples and adjust them to what you have envisioned to create!

    Источник

Оцените статью