前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >【Bootstrap】005-全局样式:表格

【Bootstrap】005-全局样式:表格

作者头像
訾博ZiBo
发布2025-01-06 15:47:05
发布2025-01-06 15:47:05
8500
代码可运行
举报
运行总次数:0
代码可运行

一、基本实例

1、说明

为任意 <table> 标签添加 .table 类可以为其赋予基本的样式 — 少量的内补(padding)和水平方向的分隔线。这种方式看起来很多余!?但是我们觉得,表格元素使用的很广泛,如果我们为其赋予默认样式可能会影响例如日历和日期选择之类的插件,所以我们选择将此样式独立出来;

2、演示

代码演示:
代码语言:javascript
代码运行次数:0
复制
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
		<title></title>
		<!-- 引入bootstrap -->
		<link rel="stylesheet" href="../css/bootstrap.min.css" />
	</head>
	<body>
		<table class="table">
		  <caption>Optional table caption.</caption>
		  <thead>
			<tr>
			  <th>#</th>
			  <th>First Name</th>
			  <th>Last Name</th>
			  <th>Username</th>
			</tr>
		  </thead>
		  <tbody>
			<tr>
			  <th scope="row">1</th>
			  <td>Mark</td>
			  <td>Otto</td>
			  <td>@mdo</td>
			</tr>
			<tr>
			  <th scope="row">2</th>
			  <td>Jacob</td>
			  <td>Thornton</td>
			  <td>@fat</td>
			</tr>
			<tr>
			  <th scope="row">3</th>
			  <td>Larry</td>
			  <td>the Bird</td>
			  <td>@twitter</td>
			</tr>
		  </tbody>
		</table>
	</body>
</html>
运行结果:

二、条纹状表格

1、说明

通过 .table-striped 类可以给 <tbody> 之内的每一行增加斑马条纹样式;

跨浏览器兼容性:

条纹状表格是依赖 :nth-child CSS 选择器实现的,而这一功能不被 Internet Explorer 8 支持;

2、演示

代码演示:
代码语言:javascript
代码运行次数:0
复制
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
		<title></title>
		<!-- 引入bootstrap -->
		<link rel="stylesheet" href="../css/bootstrap.min.css" />
	</head>
	<body>
		<table class="table table-striped">
		  <caption>Optional table caption.</caption>
		  <thead>
			<tr>
			  <th>#</th>
			  <th>First Name</th>
			  <th>Last Name</th>
			  <th>Username</th>
			</tr>
		  </thead>
		  <tbody>
			<tr>
			  <th scope="row">1</th>
			  <td>Mark</td>
			  <td>Otto</td>
			  <td>@mdo</td>
			</tr>
			<tr>
			  <th scope="row">2</th>
			  <td>Jacob</td>
			  <td>Thornton</td>
			  <td>@fat</td>
			</tr>
			<tr>
			  <th scope="row">3</th>
			  <td>Larry</td>
			  <td>the Bird</td>
			  <td>@twitter</td>
			</tr>
		  </tbody>
		</table>
	</body>
</html>
运行结果:

三、带边框的表格

1、说明

添加 .table-bordered 类为表格和其中的每个单元格增加边框;

2、演示

代码演示:
代码语言:javascript
代码运行次数:0
复制
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
		<title></title>
		<!-- 引入bootstrap -->
		<link rel="stylesheet" href="../css/bootstrap.min.css" />
	</head>
	<body>
		<table class="table table-bordered">
		  <caption>Optional table caption.</caption>
		  <thead>
			<tr>
			  <th>#</th>
			  <th>First Name</th>
			  <th>Last Name</th>
			  <th>Username</th>
			</tr>
		  </thead>
		  <tbody>
			<tr>
			  <th scope="row">1</th>
			  <td>Mark</td>
			  <td>Otto</td>
			  <td>@mdo</td>
			</tr>
			<tr>
			  <th scope="row">2</th>
			  <td>Jacob</td>
			  <td>Thornton</td>
			  <td>@fat</td>
			</tr>
			<tr>
			  <th scope="row">3</th>
			  <td>Larry</td>
			  <td>the Bird</td>
			  <td>@twitter</td>
			</tr>
		  </tbody>
		</table>
	</body>
</html>
运行结果:

四、鼠标悬停

1、说明

通过添加 .table-hover 类可以让 <tbody> 中的每一行对鼠标悬停状态作出响应;

2、演示

代码演示:
代码语言:javascript
代码运行次数:0
复制
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
		<title></title>
		<!-- 引入bootstrap -->
		<link rel="stylesheet" href="../css/bootstrap.min.css" />
	</head>
	<body>
		<table class="table table-bordered table-hover">
		  <caption>Optional table caption.</caption>
		  <thead>
			<tr>
			  <th>#</th>
			  <th>First Name</th>
			  <th>Last Name</th>
			  <th>Username</th>
			</tr>
		  </thead>
		  <tbody>
			<tr>
			  <th scope="row">1</th>
			  <td>Mark</td>
			  <td>Otto</td>
			  <td>@mdo</td>
			</tr>
			<tr>
			  <th scope="row">2</th>
			  <td>Jacob</td>
			  <td>Thornton</td>
			  <td>@fat</td>
			</tr>
			<tr>
			  <th scope="row">3</th>
			  <td>Larry</td>
			  <td>the Bird</td>
			  <td>@twitter</td>
			</tr>
		  </tbody>
		</table>
	</body>
</html>
运行结果:

五、紧缩表格

1、说明

通过添加 .table-condensed 类可以让表格更加紧凑,单元格中的内补(padding)均会减半;

2、演示

代码演示:
代码语言:javascript
代码运行次数:0
复制
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
		<title></title>
		<!-- 引入bootstrap -->
		<link rel="stylesheet" href="../css/bootstrap.min.css" />
	</head>
	<body>
		<table class="table table-bordered table-condensed">
		  <caption>Optional table caption.</caption>
		  <thead>
			<tr>
			  <th>#</th>
			  <th>First Name</th>
			  <th>Last Name</th>
			  <th>Username</th>
			</tr>
		  </thead>
		  <tbody>
			<tr>
			  <th scope="row">1</th>
			  <td>Mark</td>
			  <td>Otto</td>
			  <td>@mdo</td>
			</tr>
			<tr>
			  <th scope="row">2</th>
			  <td>Jacob</td>
			  <td>Thornton</td>
			  <td>@fat</td>
			</tr>
			<tr>
			  <th scope="row">3</th>
			  <td>Larry</td>
			  <td>the Bird</td>
			  <td>@twitter</td>
			</tr>
		  </tbody>
		</table>
	</body>
</html>
运行结果:

六、状态类

1、说明

通过这些状态类可以为行或单元格设置颜色;

2、演示

代码演示:
代码语言:javascript
代码运行次数:0
复制
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
		<title></title>
		<!-- 引入bootstrap -->
		<link rel="stylesheet" href="../css/bootstrap.min.css" />
	</head>
	<body>
		<table class="table">
		      <thead>
		        <tr>
		          <th>#</th>
		          <th>Column heading</th>
		          <th>Column heading</th>
		          <th>Column heading</th>
		        </tr>
		      </thead>
		      <tbody>
		        <tr class="active">
		          <th scope="row">1</th>
		          <td>Column content</td>
		          <td>Column content</td>
		          <td>Column content</td>
		        </tr>
		        <tr>
		          <th scope="row">2</th>
		          <td>Column content</td>
		          <td>Column content</td>
		          <td>Column content</td>
		        </tr>
		        <tr class="success">
		          <th scope="row">3</th>
		          <td>Column content</td>
		          <td>Column content</td>
		          <td>Column content</td>
		        </tr>
		        <tr>
		          <th scope="row">4</th>
		          <td>Column content</td>
		          <td>Column content</td>
		          <td>Column content</td>
		        </tr>
		        <tr class="info">
		          <th scope="row">5</th>
		          <td>Column content</td>
		          <td>Column content</td>
		          <td>Column content</td>
		        </tr>
		        <tr>
		          <th scope="row">6</th>
		          <td>Column content</td>
		          <td>Column content</td>
		          <td>Column content</td>
		        </tr>
		        <tr class="warning">
		          <th scope="row">7</th>
		          <td>Column content</td>
		          <td>Column content</td>
		          <td>Column content</td>
		        </tr>
		        <tr>
		          <th scope="row">8</th>
		          <td>Column content</td>
		          <td>Column content</td>
		          <td>Column content</td>
		        </tr>
		        <tr class="danger">
		          <th scope="row">9</th>
		          <td>Column content</td>
		          <td>Column content</td>
		          <td>Column content</td>
		        </tr>
		      </tbody>
		    </table>
		<table class="table">
			<!-- On rows -->
			<!-- <tr class="active">活跃</tr>
			<tr class="success">成功</tr>
			<tr class="warning">警告</tr>
			<tr class="danger">危险</tr>
			<tr class="info">信息</tr> -->
			
			<!-- On cells (`td` or `th`) -->
			<tr>
			  <td class="active">活跃</td>
			  <td class="success">成功</td>
			  <td class="warning">警告</td>
			  <td class="danger">危险</td>
			  <td class="info">信息</td>
			</tr>
		</table>
	</body>
</html>
运行结果:

3、向使用辅助技术的用户传达用意

通过为表格中的一行或一个单元格添加颜色而赋予不同的意义只是提供了一种视觉上的表现,并不能为使用辅助技术 -- 例如屏幕阅读器 -- 浏览网页的用户提供更多信息。因此,请确保通过颜色而赋予的不同意义可以通过内容本身来表达(即在相应行或单元格中的可见的文本内容);或者通过包含额外的方式 -- 例如应用了 .sr-only 类而隐藏的文本 -- 来表达出来;

七、响应式表格

1、说明

将任何 .table 元素包裹在 .table-responsive 元素内,即可创建响应式表格,其会在小屏幕设备上(小于768px)水平滚动。当屏幕大于 768px 宽度时,水平滚动条消失;

垂直方向的内容截断:

响应式表格使用了 overflow-y: hidden 属性,这样就能将超出表格底部和顶部的内容截断。特别是,也可以截断下拉菜单和其他第三方组件;

Firefox 和 fieldset 元素:

Firefox 浏览器对 fieldset 元素设置了一些影响 width 属性的样式,导致响应式表格出现问题。可以使用下面提供的针对 Firefox 的 hack 代码解决,但是以下代码并未集成在 Bootstrap 中:

代码语言:javascript
代码运行次数:0
复制
@-moz-document url-prefix() {
  fieldset { display: table-cell; }
}

更多信息请参考 this Stack Overflow answer.

2、演示

代码演示:
代码语言:javascript
代码运行次数:0
复制
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
		<title></title>
		<!-- 引入bootstrap -->
		<link rel="stylesheet" href="../css/bootstrap.min.css" />
	</head>
	<body>
		<div class="table-responsive">
		      <table class="table">
		        <thead>
		          <tr>
		            <th>#</th>
		            <th>Table heading</th>
		            <th>Table heading</th>
		            <th>Table heading</th>
		            <th>Table heading</th>
		            <th>Table heading</th>
		            <th>Table heading</th>
		          </tr>
		        </thead>
		        <tbody>
		          <tr>
		            <th scope="row">1</th>
		            <td>Table cell</td>
		            <td>Table cell</td>
		            <td>Table cell</td>
		            <td>Table cell</td>
		            <td>Table cell</td>
		            <td>Table cell</td>
		          </tr>
		          <tr>
		            <th scope="row">2</th>
		            <td>Table cell</td>
		            <td>Table cell</td>
		            <td>Table cell</td>
		            <td>Table cell</td>
		            <td>Table cell</td>
		            <td>Table cell</td>
		          </tr>
		          <tr>
		            <th scope="row">3</th>
		            <td>Table cell</td>
		            <td>Table cell</td>
		            <td>Table cell</td>
		            <td>Table cell</td>
		            <td>Table cell</td>
		            <td>Table cell</td>
		          </tr>
		        </tbody>
		      </table>
		    </div>
	</body>
</html>
运行结果:
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-01-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、基本实例
    • 1、说明
    • 2、演示
      • 代码演示:
      • 运行结果:
  • 二、条纹状表格
    • 1、说明
      • 跨浏览器兼容性:
    • 2、演示
      • 代码演示:
      • 运行结果:
  • 三、带边框的表格
    • 1、说明
    • 2、演示
      • 代码演示:
      • 运行结果:
  • 四、鼠标悬停
    • 1、说明
    • 2、演示
      • 代码演示:
      • 运行结果:
  • 五、紧缩表格
    • 1、说明
    • 2、演示
      • 代码演示:
      • 运行结果:
  • 六、状态类
    • 1、说明
    • 2、演示
      • 代码演示:
      • 运行结果:
    • 3、向使用辅助技术的用户传达用意
  • 七、响应式表格
    • 1、说明
      • 垂直方向的内容截断:
      • Firefox 和 fieldset 元素:
    • 2、演示
      • 代码演示:
      • 运行结果:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档