HTML layout guide: How to use pseudo-class selectors for table style control

王林
Release: 2023-10-18 11:57:11
Original
1283 people have browsed it

HTML layout guide: How to use pseudo-class selectors for table style control

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.

  1. What is a pseudo-class selector?
    In CSS, a pseudo-class selector is a special selector used to select HTML elements. They are used to select elements in a specific state. Common pseudo-class selectors are :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.
  2. Table style control example
    Let us use an example to demonstrate how to use pseudo-class selectors to control the style of a table. We will use the following HTML code to create a simple table:
<table>
  <tr>
    <th>姓名</th>
    <th>年龄</th>
    <th>性别</th>
  </tr>
  <tr>
    <td>张三</td>
    <td>25</td>
    <td>男</td>
  </tr>
  <tr>
    <td>李四</td>
    <td>30</td>
    <td>女</td>
  </tr>
  <tr>
    <td>王五</td>
    <td>35</td>
    <td>男</td>
  </tr>
</table>
Copy after login

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;
}
Copy after login

In this example, we set the width of the entire table to 100% and merge the borders using the border-collapse property. The th and td 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.

  1. Other commonly used pseudo-class selectors
    In addition to :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-childSelect the first child element

    tr:first-child {
    font-weight: bold;
    }
    Copy after login
  • :last-childSelect the last child element

    tr:last-child {
    background-color: #f2f2f2;
    }
    Copy after login
  • :nth-childSelect the child element at a specific position, which can be set using the parameter n Interval

    tr:nth-child(2n) {
    background-color: #f2f2f2;
    }
    Copy after login
  1. Comprehensive example
    Here is a more comprehensive example that uses a combination of pseudo-class selectors and other style attributes to complete the style of the table:
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;
}
Copy after login

In this example, we use tr:nth-child(even) to select even rows and set the background color for them. :hoverPseudo-class selector is used to set the background color and text color when the mouse is hovering. The th 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:

  • MDN Web Docs - Pseudo-classes: https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template