Proqram gizletmek proqrami yükle və bilgisayarınızı qoruyun - Tövsiyə olunan proqramlar
How to Create a Table in HTML
A table is a structured set of data arranged in rows and columns. Tables are useful for displaying and organizing information on web pages. In this article, you will learn how to create, edit, and style tables in HTML.
proqram gizletmek proqrami yükle
What is a table in HTML?
A table in HTML is defined by the <table> element. Inside the <table> element, you can use the following elements to create the structure of the table:
The <tr> element defines a table row.
The <td> element defines a table cell or data.
The <th> element defines a table header or heading.
The <caption> element defines a table caption or title.
You can also use the following elements to group and format the table cells:
The <thead> element defines the header section of the table.
The <tbody> element defines the body section of the table.
The <tfoot> element defines the footer section of the table.
The <colgroup> element defines a group of columns in the table.
The <col> element defines the properties of each column within a <colgroup> element.
Why use tables in HTML?
Tables are helpful for presenting and organizing data on web pages. Some of the benefits of using tables are:
They can make complex data easier to understand and compare.
They can improve the readability and accessibility of web pages.
They can enhance the design and layout of web pages.
When to use tables in HTML?
You should use tables in HTML when you have tabular data, which means data that is related by rows and columns. For example, you can use tables for:
Price lists
Schedules
Statistics
Forms
Comparisons
When not to use tables in HTML?
You should not use tables in HTML for non-tabular data, which means data that is not related by rows and columns. For example, you should not use tables for:
Navigation menus
Images
Text layout
Page structure
Using tables for non-tabular data can cause problems such as:
Poor performance and loading speed
Lack of responsiveness and flexibility
Incompatibility with different browsers and devices
Difficulty with maintenance and updates
Inaccessibility for users with disabilities or assistive technologies
How to make a table in HTML?
Basic syntax of a table in HTML
To make a basic table in HTML, you need to use the following syntax:
<table> <tr> Example of a simple table in HTML
Here is an example of a simple table in HTML that shows the names and scores of some students:
<table> <tr> <td>Alice</td> <td>90</td> </tr> <tr> <td>Bob</td> <td>80</td> </tr> <tr> <td>Charlie</td> <td>85</td> </tr> </table>
This is how the table looks like on the web page:
Alice
90
Bob
80
Charlie
85
How to add table headers, captions, and borders in HTML
To make the table more informative and attractive, you can add some elements to enhance its appearance and functionality. Here are some of the elements you can use:
The <th> element defines a table header or heading. You can use it to label the rows and columns of the table. By default, the text inside the <th> element is bold and centered.
The <caption> element defines a table caption or title. You can use it to give a brief description or summary of the table. By default, the caption is displayed above the table.
The border attribute defines the width of the border around the table and its cells. You can use it to make the table more visible and distinct. The value of the attribute is a number that represents the number of pixels of the border.
Here is an example of a table with headers, caption, and border in HTML:
<table border="1"> <caption>Student Scores</caption> <tr> <th>Name</th> <th>Score</th> </tr> <tr> <td>Alice</td> <td>90</td> </tr> <tr> <td>Bob</td> <td>80</td> </tr> <tr> <td>Charlie</td> <td>85</td> </tr> </table>
This is how the table looks like on the web page:
Student Scores
Name
Score
Alice
90
Bob
80
Charlie
85
How to merge and split cells in HTML
Sometimes, you may want to merge or split cells in a table to create a more complex layout or design. You can use the following attributes to do so:
The colspan attribute defines how many columns a cell should span across. You can use it to merge two or more cells horizontally.
The rowspan attribute defines how many rows a cell should span across. You can use it to merge two or more cells vertically.
<td> element defines a table cell or data. You can use it to split a cell into two or more cells.
Here is an example of a table with merged and split cells in HTML:
<table border="1"> <caption>Student Scores</caption> <tr> <th colspan="2">Name</th> <th rowspan="2">Score</th> </tr> <tr> <th>First Name</th> <th>Last Name</th> </tr> <tr> <td>Alice</td> <td>Smith</td> <td>90</td> </tr> <tr> <td colspan="2">Bob Jones</td> <td>80</td> </tr> <tr> <td rowspan="2">Charlie Brown</td> <td>Brown</td> <td rowspan="2">85</td> </tr> <tr> <td>(no first name)</td> </tr> </table>
This is how the table looks like on the web page:
Student Scores
Name
Score
First Name
Last Name
Alice
Smith
90
Bob Jones
80
Charlie Brown
Brown
85
(no first name)
How to style tables with CSS in HTML
To make the table more appealing and customized, you can use CSS to style the table and its elements. CSS stands for Cascading Style Sheets, and it is a language that describes how HTML elements should look on the web page. You can use CSS to change the color, font, size, alignment, spacing, border, background, and other properties of the table and its elements.
To use CSS, you need to link the HTML file with a CSS file or use the <style> element inside the HTML file. Then, you need to use selectors and declarations to target and style the table and its elements. Selectors are the names or identifiers of the HTML elements that you want to style. Declarations are the rules or instructions that define how the elements should look. Declarations consist of properties and values, separated by a colon and enclosed in curly braces. Properties are the aspects or attributes of the elements that you want to change. Values are the specific settings or choices that you want to apply to the properties.
Here is an example of a table styled with CSS in HTML:
<!DOCTYPE html> <html> <head> <style> /* This is a CSS comment */ /* Select the table element and apply some styles */ table width: 80%; /* Set the width of the table to 80% of the parent element */ margin: auto; /* Center the table horizontally */ border-collapse: collapse; /* Remove the space between the table cells */ /* Select the caption element and apply some styles */ caption font-size: 24px; /* Set the font size of the caption to 24 pixels */ font-weight: bold; /* Make the caption text bold */ color: white; /* Set the caption text color to white */ background-color: green; /* Set the caption background color to green */ padding: 10px; /* Add some space around the caption text */ /* Select the th element and apply some styles */ th font-size: 18px; /* Set the font size of the header text to 18 pixels */ color: white; /* Set the header text color to white */ background-color: blue; /* Set the header background color to blue */ padding: 5px; /* Add some space around the header text */ /* Select the td element and apply some styles */ td font-size: 16px; /* Set the font size of the data text to 16 pixels */ color: black; /* Set the data text color to black */ background-color: lightgray; /* S