HTML Layout Guide: How to use pseudo-class selectors for table style control
Introduction:
HTML tables are one of the commonly used elements in web design. for displaying data and information. However, by default, the style of a table can be relatively simple and uninteresting. To make the table more attractive, we can use CSS to control the style of the table. This article will introduce in detail how to use CSS pseudo-class selectors to control table styles, including specific code examples.
:hover
(select when the mouse is hovering),:active
(select when activated) and:visited
(visited link selection) etc. We can use pseudo-class selectors to control the style of table elements in specific states.姓名 | 年龄 | 性别 |
---|---|---|
张三 | 25 | 男 |
李四 | 30 | 女 |
王五 | 35 | 男 |
Now, we will use pseudo-class selectors to control the background color and text color of the table rows on mouseover. This can be achieved using the following CSS code:
table { border-collapse: collapse; width: 100%; } th, td { text-align: left; padding: 8px; } tr:hover { background-color: #f2f2f2; color: #000; }
In this example, we set the width of the entire table to 100% and merge the borders using theborder-collapse
property. Theth
andtd
elements are set to left-aligned and have some padding. Most importantly, we use the pseudo-class selector:hover
to select table rows and change the background color and text color on mouse hover.
:hover
, there are other commonly used pseudo-class selectors that can be used to control the style of the table. Here are some examples::first-child
Select the first child element
tr:first-child { font-weight: bold; }
:last-child
Select the last child element
tr:last-child { background-color: #f2f2f2; }
:nth-child
Select the child element at a specific position, which can be set using the parameter n Interval
tr:nth-child(2n) { background-color: #f2f2f2; }
table { border-collapse: collapse; width: 100%; } th, td { text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #f2f2f2; } tr:hover { background-color: #ddd; color: #000; } th { background-color: #4CAF50; color: white; }
In this example, we usetr:nth-child(even)
to select even rows and set the background color for them.:hover
Pseudo-class selector is used to set the background color and text color when the mouse is hovering. Theth
element uses other style attributes to set the background color and text color.
Conclusion:
By using CSS pseudo-class selectors, we can easily control and customize the style of HTML tables. Whether it's via mouseover, or via a child element at a specific location, we can use pseudo-class selectors to add detail and beauty. I hope this article can provide you with a guide so that you can better use pseudo-class selectors to control table styles in HTML layout.
Reference:
The above is the detailed content of HTML layout guide: How to use pseudo-class selectors for table style control. For more information, please follow other related articles on the PHP Chinese website!