Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Q307】如何实现表格单双行条纹样式 #309

Open
shfshanyue opened this issue Jun 2, 2020 · 4 comments
Open

【Q307】如何实现表格单双行条纹样式 #309

shfshanyue opened this issue Jun 2, 2020 · 4 comments
Labels

Comments

@shfshanyue
Copy link
Owner

No description provided.

@shfshanyue shfshanyue added the css label Jun 2, 2020
@shfshanyue
Copy link
Owner Author

shfshanyue commented Jun 2, 2020

通过 css3 中伪类 :nth-child 来实现。其中 :nth-child(an+b) 匹配下标 { an + b; n = 0, 1, 2, ...} 且结果为整数的子元素

  • nth-child(2n)/nth-child(even): 双行样式
  • nth-child(2n+1)/nth-child(odd): 单行样式

其中 tr 在表格中代表行,实现表格中单双行样式就很简单了:

tr:nth-child(2n) {
  background-color: red;
}


tr:nth-child(2n+1) {
  background-color: blue;
}

同理:

  1. 如何匹配最前三个子元素: :nth-child(-n+3)
  2. 如何匹配最后三个子元素: :nth-last-child(-n+3)

@nanwangjkl
Copy link

不是有odd和even吗

tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}

@songcee
Copy link

songcee commented Oct 2, 2022 via email

@loveminxo
Copy link

loveminxo commented Oct 2, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants