The Programmer News Hubb
Advertisement Banner
  • Home
  • Technical Insights
  • Tricks & Tutorial
  • Contact
No Result
View All Result
  • Home
  • Technical Insights
  • Tricks & Tutorial
  • Contact
No Result
View All Result
Gourmet News Hubb
No Result
View All Result
Home Tricks & Tutorial

Getting Started with HTML Tables — SitePoint

admin by admin
March 28, 2023
in Tricks & Tutorial


HTML tables are intended for displaying tabular data on a web page. They’re great for displaying information in an organized way, and can be styled with CSS to match the look and feel of our website. In this tutorial, we’ll cover the basics of creating HTML tables and adding styles to make them responsive and mobile-friendly.

Creating an HTML Table

To create an HTML table, we need to use the

tag. Inside the

tag, we need to create one or more

tags, which define each row of the table. Inside each

tag, we can create one or more

tag. To add new cells to the table, we can create a new

tag. Here is an example of a table with four rows and three columns:

<table>
  <tr>
    <td>Cell 1td>
    <td>Cell 2td>
    <td>Cell 3td>
  tr>
  <tr>
    <td>Cell 4td>
    <td>Cell 5td>
    <td>Cell 6td>
  tr>
  <tr>
    <td>Cell 7td>
    <td>Cell 8td>
    <td>Cell 9td>
  tr>
  <tr>
    <td>Cell 10td>
    <td>Cell 11td>
    <td>Cell 12td>
  tr>
table>

This will create a table with four rows and three columns.

Styling HTML Tables

HTML tables can be styled using CSS to change their appearance. Some of the most common CSS properties used to style tables include border, padding, and background-color. Here’s an example of how to style a table with a border and a background color:

table {
  border: 1px solid black;
  background-color: #f2f2f2;
}
td {
  padding: 8px;
}

This will create a table with a black border and a light gray background color, with each cell having a padding of eight pixels.

See the Pen
A basic HTML table
by SitePoint (@SitePoint)
on CodePen.

Making Tables Responsive and Mobile-friendly

One of the challenges of using HTML tables is making them responsive and mobile-friendly. One way to accomplish this is to use CSS to adjust the layout of the table based on the size of the screen. One approach is to use the display property to change the layout of the table from a fixed layout to a responsive layout. This can be done using media queries to target specific screen sizes. Here’s an example of how to make a table responsive:

table {
  border: 1px solid black;
  background-color: #f2f2f2;
}

td {
  padding: 8px;
}

@media only screen and (max-width: 600px) {
  table {
    display: block;
  }
  td {
    display: block;
  }
}

This will make the table layout change from a fixed layout to a responsive layout when the screen width is less than 600 pixels.

See the Pen
A responsive HTML table with basic styling
by SitePoint (@SitePoint)
on CodePen.

Adding Captions and Summaries

Another important aspect of using HTML tables is making them accessible to non-visual users. One way to do this is to add captions and summaries to the table. The

tags, which define the cells of the table. Here’s an example of a basic HTML table:

<table>
  <tr>
    <td>Cell 1td>
    <td>Cell 2td>
    <td>Cell 3td>
  tr>
  <tr>
    <td>Cell 4td>
    <td>Cell 5td>
    <td>Cell 6td>
  tr>
table>

This will create a table with two rows and three columns, with each cell displaying its contents.

See the Pen
A basic HTML table
by SitePoint (@SitePoint)
on CodePen.

Adding Rows and Columns

To add new rows to the table, we simply need to create a new

tag inside an existing

tag can be used to add a caption to the table, which describes the content of the table. Here’s an example of how to add a caption to a table:

<table>
  <caption>Sales by Monthcaption>
  <tr>
    <th>Monthth>
    <th>Salesth>
  tr>
  <tr>
    <td>Januarytd>
    <td>$1000td>
  tr>
  <tr>
    <td>Februarytd>
    <td>$1500td>
  tr>
table>

This will add a caption to the table, stating that it displays sales by month. The

tag can be used to provide a summary of the table for screen readers and other assistive technologies. Here’s an example of how to add a summary to a table:

<table summary="Sales by Month">
  <tr>
    <th>Monthth>
    <th>Salesth>
  tr>
  <tr>
    <td>Januarytd>
    <td>$1000td>
  tr>
  <tr>
    <td>Februarytd>
    <td>$1500td>
  tr>
table>

This will provide a summary of the table for screen readers and other assistive technologies, stating that it displays sales by month.

Is Using Tables Bad?

No! Tables are an important part of HTML. They’re are essential for displaying tabular data in a semantic and accessible way. In the early days of the Web, before the advent of CSS, tables offered a means to lay out web page designs, but this wasn’t their intended purose. Thankfully, those days are way behind us (well, mostly, but for some email clients!), and we can now focus on the true — and extremely important — role of HTML tables for displaying data.

Conclusion

HTML tables are a powerful tool for displaying tabular data on a web page. With CSS, tables can be styled to match the look and feel of our website, and made responsive and mobile-friendly for users on different devices. Adding captions and summaries to tables can help improve accessibility for users with disabilities. With these techniques, we can create effective tables that are both visually appealing and functional.





Source link

Previous Post

How to level up your low-code game

Next Post

Custom Software Development Companies: Best Practices

Next Post

Custom Software Development Companies: Best Practices

Recommended

Dedicated Team Model in Software Engineering

5 months ago

SD Times Open-Source of the Week: Stargate

11 months ago

The Chat GPT API is a wakeup call to use these 5 key performance metrics

6 months ago

Weekly News for Designers № 710

3 weeks ago

5 Ways to Select the Best DEI Software for your Workplace

9 months ago

25 Tutorials for Getting the Most Out of DaVinci Resolve

8 months ago

© The Programmer News Hubb All rights reserved.

Use of these names, logos, and brands does not imply endorsement unless specified. By using this site, you agree to the Privacy Policy and Terms & Conditions.

Navigate Site

  • Home
  • Technical Insights
  • Tricks & Tutorial
  • Contact

Newsletter Sign Up.

No Result
View All Result
  • Home
  • Technical Insights
  • Tricks & Tutorial
  • Contact

© 2022 The Programmer News Hubb All rights reserved.