Home > Web Front-end > CSS Tutorial > How to implement table style in css

How to implement table style in css

藏色散人
Release: 2023-01-07 11:45:38
Original
18833 people have browsed it

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.

How to implement table style in css

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.

How to implement table style in css


Foreword:

    Before the advent of CSS, web pages usually used tables Layout;
  • Nowadays, times have changed, and tables are no longer suitable for web page layout,
  • Because the rendering speed of tables is too slow
  • The browser will not display the table until it has finished rendering
However, tables are still useful:

    For developers, tables are generally used in the background management system
  1. For
  2. beginners For scholars, forms are particularly easy to use and produce exquisite effects
Websites are usually divided into two parts: the front desk and the back desk

    Front 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

How to implement table style in css

    ##Table
  • There is this under the table tag Four sub-tags: caption, thead, tbody, tfoot (can be written or not)
  • Table title (caption)
  • Header (thead)
  • There are sub-elements tr
  • Table body (tbody)
  • There are sub-elements tr
  • Table foot (tfoot)
  • There are sub-elements tr
    The table is composed of rows and rows of elements,
  • Table row (tr)
  • There are sub-elements th and td
    The table is row, The column is divided into multiple cells,
  • Title cell (th), cell (td)
  • By convention, th appears in thead

2: Pure HTML effectIt is indeed ugly, but looking back, after adding css, the ugly duckling becomes a white swan


How to implement table style in csshtml :
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)


How to implement table style in css

Cell merging of tables
    Two attributes of td tag: colspan, rowspan

  • Cross-column:
  • , as above, merge 5 columns of cells in one rowAcross rows:
  • , merge 2 rows of cells in one column
##3: Use CSS to modify the table style

How to implement table style in csscss:

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;}
Copy after login
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
    Separate borders
  1. collapse
  2. Merge adjacent borders
:nth-child()

:nth-child() is a pseudo-class

The pseudo-class is a type of selector

table tbody tr:nth-child(odd)
Copy after login

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

意思:选中table tbody tr下,第一个子元素并且必须是td元素

利用这个伪类,我实现了将表格主体的第一列全部单元的背景颜色改了

:hover

:hover是伪类

table tbody tr:hover
Copy after login

意思:选中鼠标悬停的table tbody下tr标签

即我通过这个伪类,实现了我鼠标悬停在表格主体的某个地方时,整行变色
How to implement table style in css

最终效果:利用html、css制作一个美观、大方的表格,而且很简单,容易上手。

How to implement table style in css


前言:

  • 在css出现之前,网页通常使用表格布局;
  • 如今,时代变了,表格不再适用于网页布局,
  • 因为表格的渲染速度过慢
    浏览器要将表格渲染完,才会显示

不过,表格还是有用的:

  1. 对于开发者来说,一般在后台管理系统中使用到表格
  2. 对于初学者来说,表格特别容易上手,并且制作出精美的效果

网站通常分为前台、后台两部分

  • 前台:面向用户
  • 后台:面向管理员(对界面要求不高,对功能性要求高)

如今,h5的时代正在来临,我们需要明确一个观点,HTML只负责网站的骨架,html标签元素是有语义化的(给搜索引擎看的);而网站的样式是靠css来负责的

一:表格标签讲解

How to implement table style in css

  • 表格(table)
    table标签下有这么四个子标签:caption、thead、tbody、tfoot(可写可不写)
  • 表格标题(caption)
  • 表头(thead)
    有子元素 tr
  • 表格主体(tbody)
    有子元素 tr
  • 表尾(tfoot)
    有子元素 tr
  • 表格是一行一行元素组成的,表格行(tr)
    有子元素 th 和 td
  • 表格被行、列划分为多个单元,标题单元格(th)、单元格(td)
    习惯上,th 出现在thead内

二:纯HTML效果

确实丑,但是往后面看,加了css后,丑小鸭就变白天鹅了
How to implement table style in css
html:

代码又长又无趣,我就不把它全部显示出来了(tbody标签折叠的内容就是10个tr标签,每个tr标签内部有5个td标签)
How to implement table style in css

  • 表格的单元格合并
    td标签的两个属性:colspan、rowspan
  • 跨列:<td colspan="'5'"></td>,如上,合并一行中的5列单元格
  • 跨行:<td rowspan="'2'"></td>,合并一列中的2行单元格

三:用CSS修改表格样式

How to implement table style in 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;}
Copy after login

有兴趣的可以继续看看:

四:上述CSS中几个有意思的知识点

border-collapse

border-collapse是table标签的一个属性,有两个取值:

  1. seperate 边框之间分离
  2. collapse 两两相临边框合并

:nth-child()

:nth-child()是伪类

伪类是选择器的一种

table tbody tr:nth-child(odd)
Copy after login

意思:必须是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
Copy after login

意思:选中table tbody tr下,第一个子元素并且必须是td元素

利用这个伪类,我实现了将表格主体的第一列全部单元的背景颜色改了

:hover

:hover是伪类

table tbody tr:hover
Copy after login

意思:选中鼠标悬停的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!

Related labels:
css
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