HTML Table Advanced Features And Accessibility - MDN Web Docs
Có thể bạn quan tâm
- Guides
- HTML
- HTML tables
- HTML table advanced features and accessibility
- English (US)
- Remember language
- Deutsch
- Español
- Français
- 日本語
- 한국어
- Русский
- 中文 (简体)
- Adding a caption to your table with <caption>
- Adding structure with <thead>, <tbody>, and <tfoot>
- Nesting Tables
- Tables for visually impaired users
- Summary
- Complete beginners start here!
- Getting started with the web
- Getting started with the web
- Installing basic software
- What will your website look like?
- Dealing with files
- HTML basics
- CSS basics
- JavaScript basics
- Publishing your website
- How the web works
- HTML — Structuring the web
- Introduction to HTML
- Introduction to HTML
- Getting started with HTML
- What's in the head? Metadata in HTML
- HTML text fundamentals
- Creating hyperlinks
- Advanced text formatting
- Document and website structure
- Debugging HTML
- Marking up a letter
- Structuring a page of content
- Multimedia and embedding
- Multimedia and embedding
- Images in HTML
- Video and audio content
- From object to iframe — other embedding technologies
- Adding vector graphics to the web
- Responsive images
- Mozilla splash page
- HTML tables
- HTML tables
- HTML table basics
- HTML table advanced features and accessibility
- Structuring planet data
- CSS — Styling the web
- CSS first steps
- CSS first steps
- What is CSS?
- Getting started with CSS
- How CSS is structured
- How CSS works
- Styling a biography page
- CSS building blocks
- CSS building blocks
- CSS selectors
- Type, class, and ID selectors
- Attribute selectors
- Pseudo-classes and pseudo-elements
- Combinators
- Cascade, specificity, and inheritance
- Cascade layers
- The box model
- Backgrounds and borders
- Handling different text directions
- Overflowing content
- CSS values and units
- Sizing items in CSS
- Images, media, and form elements
- Styling tables
- Advanced styling effects
- Debugging CSS
- Organizing your CSS
- Fundamental CSS comprehension
- Creating fancy letterheaded paper
- A cool-looking box
- Styling text
- CSS styling text
- Fundamental text and font styling
- Styling lists
- Styling links
- Web fonts
- Typesetting a community school homepage
- CSS layout
- CSS layout
- Introduction to CSS layout
- Normal Flow
- Flexbox
- Grids
- Floats
- Positioning
- Multiple-column layout
- Responsive design
- Beginner's guide to media queries
- Legacy layout methods
- Supporting older browsers
- Fundamental layout comprehension
- JavaScript — Dynamic client-side scripting
- JavaScript first steps
- JavaScript first steps
- What is JavaScript?
- A first splash into JavaScript
- What went wrong? Troubleshooting JavaScript
- Storing the information you need — Variables
- Basic math in JavaScript — numbers and operators
- Handling text — strings in JavaScript
- Useful string methods
- Arrays
- Silly story generator
- JavaScript building blocks
- JavaScript building blocks
- Making decisions in your code — conditionals
- Looping code
- Functions — reusable blocks of code
- Build your own function
- Function return values
- Introduction to events
- Event bubbling
- Image gallery
- Introducing JavaScript objects
- Introducing JavaScript objects
- JavaScript object basics
- Object prototypes
- Object-oriented programming
- Classes in JavaScript
- Working with JSON
- Object building practice
- Adding features to our bouncing balls demo
- Asynchronous JavaScript
- Asynchronous JavaScript
- Introducing asynchronous JavaScript
- How to use promises
- How to implement a promise-based API
- Introducing workers
- Sequencing animations
- Client-side web APIs
- Client-side web APIs
- Introduction to web APIs
- Manipulating documents
- Fetching data from the server
- Third-party APIs
- Drawing graphics
- Video and Audio APIs
- Client-side storage
- Web forms — Working with user data
- Web form building blocks
- Web form building blocks
- Your first form
- How to structure a web form
- Basic native form controls
- The HTML5 input types
- Other form controls
- Styling web forms
- Advanced form styling
- UI pseudo-classes
- Client-side form validation
- Sending form data
- Advanced web form techniques
- How to build custom form controls
- Sending forms through JavaScript
- CSS property compatibility table for form controls
- HTML forms in legacy browsers
- Accessibility — Make the web usable by everyone
- Accessibility guides
- Accessibility
- What is accessibility?
- HTML: A good basis for accessibility
- CSS and JavaScript accessibility best practices
- WAI-ARIA basics
- Accessible multimedia
- Mobile accessibility
- Assessment: Accessibility troubleshooting
- Performance — Making websites fast and responsive
- Performance guides
- Web performance
- The "why" of web performance
- What is web performance?
- Perceived performance
- Measuring performance
- Multimedia: Images
- Multimedia: video
- JavaScript performance optimization
- HTML performance optimization
- CSS performance optimization
- The business case for web performance
- MathML — Writing mathematics with MathML
- MathML first steps
- MathML first steps
- Getting started with MathML
- MathML Text Containers
- MathML fractions and roots
- MathML scripted elements
- MathML tables
- Three famous mathematical formulas
- Games — Developing games for the web
- Guides and tutorials
- Introduction to game development for the Web
- Techniques for game development
- Tutorials
- Publishing games
- Tools and testing
- Client-side web development tools
- Understanding client-side web development tools
- Client-side tooling overview
- Command line crash course
- Package management basics
- Introducing a complete toolchain
- Deploying our app
- Introduction to client-side frameworks
- Introduction to client-side frameworks
- Framework main features
- React
- Getting started with React
- Beginning our React todo list
- Componentizing our React app
- React interactivity: Events and state
- React interactivity: Editing, filtering, conditional rendering
- Accessibility in React
- React resources
- Ember
- Getting started with Ember
- Ember app structure and componentization
- Ember interactivity: Events, classes and state
- Ember Interactivity: Footer functionality, conditional rendering
- Routing in Ember
- Ember resources and troubleshooting
- Vue
- Getting started with Vue
- Creating our first Vue component
- Rendering a list of Vue components
- Adding a new todo form: Vue events, methods, and models
- Styling Vue components with CSS
- Using Vue computed properties
- Vue conditional rendering: editing existing todos
- Vue refs and lifecycle methods for focus management
- Vue resources
- Svelte
- Getting started with Svelte
- Starting our Svelte to-do list app
- Dynamic behavior in Svelte: working with variables and props
- Componentizing our Svelte app
- Advanced Svelte: Reactivity, lifecycle, accessibility
- Working with Svelte stores
- TypeScript support in Svelte
- Deployment and next steps
- Angular
- Getting started with Angular
- Beginning our Angular todo list app
- Styling our Angular app
- Creating an item component
- Filtering our to-do items
- Building Angular applications and further resources
- Git and GitHub
- Git and GitHub
- Cross browser testing
- Cross browser testing
- Introduction to cross-browser testing
- Strategies for carrying out testing
- Handling common HTML and CSS problems
- Handling common JavaScript problems
- Handling common accessibility problems
- Implementing feature detection
- Introduction to automated testing
- Setting up your own test automation environment
- Server-side website programming
- First steps
- Server-side website programming first steps
- Introduction to the server side
- Client-Server Overview
- Server-side web frameworks
- Website security
- Django web framework (Python)
- Django Web Framework (Python)
- Django introduction
- Setting up a Django development environment
- Django Tutorial: The Local Library website
- Django Tutorial Part 2: Creating a skeleton website
- Django Tutorial Part 3: Using models
- Django Tutorial Part 4: Django admin site
- Django Tutorial Part 5: Creating our home page
- Django Tutorial Part 6: Generic list and detail views
- Django Tutorial Part 7: Sessions framework
- Django Tutorial Part 8: User authentication and permissions
- Django Tutorial Part 9: Working with forms
- Django Tutorial Part 10: Testing a Django web application
- Django Tutorial Part 11: Deploying Django to production
- Django web application security
- Assessment: DIY Django mini blog
- Express Web Framework (Node.js/JavaScript)
- Express web framework (Node.js/JavaScript)
- Express/Node introduction
- Setting up a Node development environment
- Express Tutorial: The Local Library website
- Express Tutorial Part 2: Creating a skeleton website
- Express Tutorial Part 3: Using a Database (with Mongoose)
- Express Tutorial Part 4: Routes and controllers
- Express Tutorial Part 5: Displaying library data
- Express Tutorial Part 6: Working with forms
- Express Tutorial Part 7: Deploying to production
- Further resources
- Common questions
- Common questions
- Use HTML to solve common problems
- Use CSS to solve common problems
- Solve common problems in your JavaScript code
- Web mechanics
- Tools and setup
- Design and accessibility
- Adding a caption to your table with <caption>
- Adding structure with <thead>, <tbody>, and <tfoot>
- Nesting Tables
- Tables for visually impaired users
- Summary
- Previous
- Overview: HTML tables
- Next
In the second article in this module, we look at some more advanced features of HTML tables — such as captions/summaries and grouping your rows into table head, body and footer sections — as well as looking at the accessibility of tables for visually impaired users.
Prerequisites: | The basics of HTML (see Introduction to HTML). |
---|---|
Objective: | To learn about more advanced HTML table features, and the accessibility of tables. |
Adding a caption to your table with <caption>
You can give your table a caption by putting it inside a <caption> element and nesting that inside the <table> element. You should put it just below the opening <table> tag.
html<table> <caption> Dinosaurs in the Jurassic period </caption> … </table>As you can infer from the brief example above, the caption is meant to contain a description of the table contents. This is useful for all readers wishing to get a quick idea of whether the table is useful to them as they scan the page, but particularly for blind users. Rather than have a screen reader read out the contents of many cells just to find out what the table is about, the user can rely on a caption and then decide whether or not to read the table in greater detail.
A caption is placed directly beneath the <table> tag.
Note: The summary attribute can also be used on the <table> element to provide a description — this is also read out by screen readers. We'd recommend using the <caption> element instead, however, as summary is deprecated and can't be read by sighted users (it doesn't appear on the page).
Active learning: Adding a caption
Let's try this out, revisiting an example we first met in the previous article.
- Open up your language teacher's school timetable from the end of HTML Table Basics, or make a local copy of our timetable-fixed.html file.
- Add a suitable caption for the table.
- Save your code and open it in a browser to see what it looks like.
Note: You can find our version on GitHub — see timetable-caption.html (see it live also).
Adding structure with <thead>, <tbody>, and <tfoot>
As your tables get a bit more complex in structure, it is useful to give them more structural definition. One clear way to do this is by using <thead>, <tbody>, and <tfoot>, which allow you to mark up a header, body, and footer section for the table.
These elements don't make the table any more accessible to screen reader users, and don't result in any visual enhancement on their own. They are however very useful for styling and layout — acting as useful hooks for adding CSS to your table. To give you some interesting examples, in the case of a long table you could make the table header and footer repeat on every printed page, and you could make the table body display on a single page and have the contents available by scrolling up and down.
To use them, they should be included in the following order:
- The <thead> element must wrap the part of the table that is the header — this is usually the first row containing the column headings, but this is not necessarily always the case. If you are using <col>/<colgroup> elements, the table header should come just below those.
- The <tbody> element needs to wrap the main part of the table content that isn't the table header or footer.
- The <tfoot> element needs to wrap the part of the table that is the footer — this might be a final row with items in the previous rows summed, for example.
Note: <tbody> is always included in every table, implicitly if you don't specify it in your code. To check this, open up one of your previous examples that doesn't include <tbody> and look at the HTML code in your browser developer tools — you will see that the browser has added this tag for you. You might wonder why you ought to bother including it at all — you should, because it gives you more control over your table structure and styling.
Active learning: Adding table structure
Let's put these new elements into action.
-
First of all, make a local copy of spending-record.html and minimal-table.css in a new folder.
-
Try putting the obvious headers row inside a <thead> element, the "SUM" row inside a <tfoot> element, and the rest of the content inside a <tbody> element.
-
Next, add a colspan attribute to make the "SUM" cell span across the first four columns, so the actual number appears at the bottom of the "Cost" column.
-
Let's add some simple extra styling to the table, to give you an idea of how useful these elements are for applying CSS. Inside the head of your HTML document, you'll see an empty <style> element. Inside this element, add the following lines of CSS code:
csstbody { font-size: 95%; font-style: italic; } tfoot { font-weight: bold; } -
Save and refresh, and have a look at the result. If the <tbody> and <tfoot> elements weren't in place, you'd have to write much more complicated selectors/rules to apply the same styling.
Note: We don't expect you to fully understand the CSS right now. You'll learn more about this when you go through our CSS modules (Introduction to CSS is a good place to start; we also have an article specifically on styling tables).
Your finished table should look something like the following:
Note: You can also find it on GitHub as spending-record-finished.html.
Nesting Tables
It is possible to nest a table inside another one, as long as you include the complete structure, including the <table> element. This is generally not really advised, as it makes the markup more confusing and less accessible to screen reader users, and in many cases you might as well just insert extra cells/rows/columns into the existing table. It is however sometimes necessary, for example if you want to import content easily from other sources.
The following markup shows a simple nested table:
html<table id="table1"> <tr> <th>title1</th> <th>title2</th> <th>title3</th> </tr> <tr> <td id="nested"> <table id="table2"> <tr> <td>cell1</td> <td>cell2</td> <td>cell3</td> </tr> </table> </td> <td>cell2</td> <td>cell3</td> </tr> <tr> <td>cell4</td> <td>cell5</td> <td>cell6</td> </tr> </table>The output of which looks something like this:
table { border-collapse: collapse; } td, th { border: 1px solid black; padding: 10px 20px; }Tables for visually impaired users
Let's recap briefly on how we use data tables. A table can be a handy tool, for giving us quick access to data and allowing us to look up different values. For example, it takes only a short glance at the table below to find out how many rings were sold in Gent during August 2016. To understand its information we make visual associations between the data in this table and its column and/or row headers.
Clothes | Accessories | |||||
---|---|---|---|---|---|---|
Trousers | Skirts | Dresses | Bracelets | Rings | ||
Belgium | Antwerp | 56 | 22 | 43 | 72 | 23 |
Gent | 46 | 18 | 50 | 61 | 15 | |
Brussels | 51 | 27 | 38 | 69 | 28 | |
The Netherlands | Amsterdam | 89 | 34 | 69 | 85 | 38 |
Utrecht | 80 | 12 | 43 | 36 | 19 |
But what if you cannot make those visual associations? How then can you read a table like the above? Visually impaired people often use a screen reader that reads out information on web pages to them. This is no problem when you're reading plain text but interpreting a table can be quite a challenge for a blind person. Nevertheless, with the proper markup we can replace visual associations by programmatic ones.
Note: There are around 253 Million people living with Visual Impairment according to WHO data in 2017.
This section of the article provides further techniques for making tables as accessible as possible.
Using column and row headers
Screen readers will identify all headers and use them to make programmatic associations between those headers and the cells they relate to. The combination of column and row headers will identify and interpret the data in each cell so that screen reader users can interpret the table similarly to how a sighted user does.
We already covered headers in our previous article — see Adding headers with <th> elements.
The scope attribute
A new topic for this article is the scope attribute, which can be added to the <th> element to tell screen readers exactly what cells the header is a header for — is it a header for the row it is in, or the column, for example? Looking back to our spending record example from earlier on, you could unambiguously define the column headers as column headers like this:
html<thead> <tr> <th scope="col">Purchase</th> <th scope="col">Location</th> <th scope="col">Date</th> <th scope="col">Evaluation</th> <th scope="col">Cost (€)</th> </tr> </thead>And each row could have a header defined like this (if we added row headers as well as column headers):
html<tr> <th scope="row">Haircut</th> <td>Hairdresser</td> <td>12/09</td> <td>Great idea</td> <td>30</td> </tr>Screen readers will recognize markup structured like this, and allow their users to read out the entire column or row at once, for example.
scope has two more possible values — colgroup and rowgroup. These are used for headings that sit over the top of multiple columns or rows. If you look back at the "Items Sold August 2016" table at the start of this section of the article, you'll see that the "Clothes" cell sits above the "Trousers", "Skirts", and "Dresses" cells. All of these cells should be marked up as headers (<th>), but "Clothes" is a heading that sits over the top and defines the other three subheadings. "Clothes" therefore should get an attribute of scope="colgroup", whereas the others would get an attribute of scope="col":
html<thead> <tr> <th colspan="3" scope="colgroup">Clothes</th> </tr> <tr> <th scope="col">Trousers</th> <th scope="col">Skirts</th> <th scope="col">Dresses</th> </tr> </thead>The same applies to headers for multiple grouped rows. Take another look at the "Items Sold August 2016" table, this time focusing on the rows with the "Amsterdam" and "Utrecht" headers (<th>). You'll notice that the "The Netherlands" header, also marked up as a <th> element, spans both rows, being the heading for the other two subheadings. Therefore, scope="rowgroup" should be specified on this header cell to help screen readers create the correct associations:
html<tr> <th rowspan="2" scope="rowgroup">The Netherlands</th> <th scope="row">Amsterdam</th> <td>89</td> <td>34</td> <td>69</td> </tr> <tr> <th scope="row">Utrecht</th> <td>80</td> <td>12</td> <td>43</td> </tr>The id and headers attributes
An alternative to using the scope attribute is to use id and headers attributes to create associations between headers and cells.
The headers attribute takes a list of unordered, space-separated strings, each corresponding to the unique id of the <th> elements that provide headings for either a data cell (<td> element) or another header cell (<th> element).
This gives your HTML table an explicit definition of the position of each cell in the table, defined by the header(s) for each column and row it is part of, kind of like a spreadsheet. For it to work well, the table really needs both column and row headers.
Returning to our "Items Sold August 2016" example, we can use the id and headers attributes as follows:
- Add a unique id to each <th> element in the table.
- Add a headers attribute to each <th> element that acts as a subheading, i.e., has a header element above it. The value is the id of the heading that sits over the top and defines the subheadings, which is "clothes" for the column headers and "belgium" for the row header in our example.
- Add a headers attribute to each <td> element and add the ids of the associated <th> element(s) in form of a space-separated list. You can proceed as you would in a spreadsheet: Find the data cell and search for the corresponding headings for the row and column. The order of the specified ids doesn't matter, but you should be consistent to keep it organized.
Note: This method creates very precise associations between headers and data cells but it uses a lot more markup and does not leave any room for errors. The scope approach is usually sufficient for most tables.
Active learning: playing with scope and headers
- For this final exercise, we'd like you to first make local copies of items-sold.html and minimal-table.css, in a new directory.
- Now try adding in the appropriate scope attributes to make this table more accessible.
- Finally, try making another copy of the starter files, and this time make the table more accessible by creating precise and explicit associations using id and headers attributes.
Note: You can check your work against our finished examples — see items-sold-scope.html (also see this live) and items-sold-headers.html (see this live too).
Summary
There are a few other things you could learn about tables in HTML, but this is all you need to know for now. Next, you can test yourself with our HTML tables assessment. Have fun!
If you are already learning CSS and have done well on the assessment, you can move on and learn about styling HTML tables — see Styling tables.
If you want to get started with learning CSS, check out the CSS Learning Area!
- Previous
- Overview: HTML tables
- Next
Help improve MDN
Was this page helpful to you?YesNoLearn how to contribute.This page was last modified on Oct 14, 2024 by MDN contributors.
View this page on GitHub • Report a problem with this contentTừ khóa » Html Table Td Scope Col
-
HTML |
Scope Attribute - GeeksforGeeks HTML Th Scope Attribute - W3Schools
HTML Scope Attribute - W3Schools
HTML Td Scope Attribute
H63: Using The Scope Attribute To Associate Header Cells And ... - W3C
Scope Of Headers | Tables | WAI Web Accessibility Tutorials - W3C
: The Table Header Element - HTML - MDN Web Docs - Mozilla Web Accessibility - University Of Hawaii System
HTML Th Scope Attribute - Dofactory
HTML - Table Headers & Scope
9.3. In Simple Tables Use The Scope Attribute To Associate Data Cells ...
Scope Attribute Should Be Used Correctly On Tables Axe Rules
HTML
Col Attribute - Stack Overflow Tables - Bootstrap
Copyright © 2022 | Thiết Kế Truyền Hình Cáp Sông Thu