Home >Web Front-end >CSS Tutorial >How to implement table style in css
How to implement table style in css: first create an HTML sample file; then set the "colspan, rowspan" attributes of the td tag; finally implement the table style by setting "background-color" and other styles.
The operating environment of this article: Windows7 system, HTML5&&CSS3 version, Dell G3 computer.
How to implement table style in css?
html, css to implement a beautiful table
##Final effect: Use html and css to create a beautiful and generous table, and it is very simple and easy to use.
Foreword:
Websites are usually divided into two parts: the front desk and the back deskFront desk: User-oriented
- Backend: Administrator-oriented (low requirements for interface, high requirements for functionality)
Now, the era of h5 is coming, we One point needs to be made clear. HTML is only responsible for the skeleton of the website. HTML tag elements are semantic (for search engines to see); and the style of the website is responsible for the style of the website.
1: Explanation of table tags
2: Pure HTML effectIt is indeed ugly, but looking back, after adding css, the ugly duckling becomes a white swan
html :
The code is long and boring, so I won’t show it all (the collapsed content of the tbody tag is 10 tr tags, and each tr tag has 5 td tags inside)
Across rows:
css:
table{ width: 100%; border-collapse: collapse;}table caption{ font-size: 2em; font-weight: bold; margin: 1em 0;}th,td{ border: 1px solid #999; text-align: center; padding: 20px 0;}table thead tr{ background-color: #008c8c; color: #fff;}table tbody tr:nth-child(odd){ background-color: #eee;}table tbody tr:hover{ background-color: #ccc;}table tbody tr td:first-child{ color: #f40;}table tfoot tr td{ text-align: right; padding-right: 20px;}If you are interested, you can continue to read: 4: Several interesting knowledge points in the above CSS
border-collapse
border-collapse is an attribute of the table tag and has two values:
seperate
collapse
:nth-child() is a pseudo-class
The pseudo-class is a type of selector
table tbody tr:nth-child(odd)Meaning:
must be a tr element and must be the odd-numbered sub-element under the table tbody
正是用这个伪类,我实现了表格中表格主体内的奇数行和偶数行的背景颜色不同
()内的参数:
odd
或者2n+1
:第奇数个even
或者2n
:第偶数个6n
:第6、12、18、24、… 、6n个5
:第5个:first-child()
:first-child()是伪类
table tbody tr td:first-child
意思:选中table tbody tr下,第一个子元素并且必须是td元素
利用这个伪类,我实现了将表格主体的第一列全部单元的背景颜色改了
:hover
:hover是伪类
table tbody tr:hover
意思:选中鼠标悬停的table tbody下tr标签
即我通过这个伪类,实现了我鼠标悬停在表格主体的某个地方时,整行变色
最终效果:利用html、css制作一个美观、大方的表格,而且很简单,容易上手。
前言:
不过,表格还是有用的:
网站通常分为前台、后台两部分
- 前台:面向用户
- 后台:面向管理员(对界面要求不高,对功能性要求高)
如今,h5的时代正在来临,我们需要明确一个观点,HTML只负责网站的骨架,html标签元素是有语义化的(给搜索引擎看的);而网站的样式是靠css来负责的
一:表格标签讲解
二:纯HTML效果
确实丑,但是往后面看,加了css后,丑小鸭就变白天鹅了
html:
代码又长又无趣,我就不把它全部显示出来了(tbody标签折叠的内容就是10个tr标签,每个tr标签内部有5个td标签)
<td colspan="'5'"></td>
,如上,合并一行中的5列单元格<td rowspan="'2'"></td>
,合并一列中的2行单元格三:用CSS修改表格样式
css:
table{ width: 100%; border-collapse: collapse;}table caption{ font-size: 2em; font-weight: bold; margin: 1em 0;}th,td{ border: 1px solid #999; text-align: center; padding: 20px 0;}table thead tr{ background-color: #008c8c; color: #fff;}table tbody tr:nth-child(odd){ background-color: #eee;}table tbody tr:hover{ background-color: #ccc;}table tbody tr td:first-child{ color: #f40;}table tfoot tr td{ text-align: right; padding-right: 20px;}
有兴趣的可以继续看看:
四:上述CSS中几个有意思的知识点
border-collapse
border-collapse是table标签的一个属性,有两个取值:
seperate
边框之间分离collapse
两两相临边框合并:nth-child()
:nth-child()是伪类
伪类是选择器的一种
table tbody tr:nth-child(odd)
意思:必须是tr元素,必须是table tbody下的第奇数个子元素
正是用这个伪类,我实现了表格中表格主体内的奇数行和偶数行的背景颜色不同
()内的参数:
odd
或者2n+1
:第奇数个even
或者2n
:第偶数个6n
:第6、12、18、24、… 、6n个5
:第5个:first-child()
:first-child()是伪类
table tbody tr td:first-child
意思:选中table tbody tr下,第一个子元素并且必须是td元素
利用这个伪类,我实现了将表格主体的第一列全部单元的背景颜色改了
:hover
:hover是伪类
table tbody tr:hover
意思:选中鼠标悬停的table tbody下tr标签
即我通过这个伪类,实现了我鼠标悬停在表格主体的某个地方时,整行变色
【推荐学习:css视频教程】
The above is the detailed content of How to implement table style in css. For more information, please follow other related articles on the PHP Chinese website!