css 实现 table斜线表头

本文介绍了一种使用HTML和CSS实现特殊形状的通话资费表的方法,通过巧妙设置border属性来模拟对角线效果,并展示了不同地区通话费用的具体收费标准。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

border-left  border-right border-top border-bottom 能实现特殊形状
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>资费标准</title>
<style type="text/css">
table {
	border-collapse: collapse;
	border: 1px #e3e3e3 solid;
	width: 96%;
	margin: 0 auto;
	margin-top: 30px;
	margin-bottom:50px;
}

th, td {
	border: 1px solid #e3e3e3;
	text-align: center;
	font-size: 35px;
	line-height: 100px;
	background: #C6C7C6;
}

th {
	background: #199fff;
	color: white;
} /*模拟对角线*/
.out {
	border-top: 5em #199fff solid; /*上边框宽度等于表格第一行行高*/
	border-left: 200px #ff8838 solid; /*左边框宽度等于表格第一行第一格宽度*/
	position: relative; /*让里面的两个子容器绝对定位*/
	color:white;
}

b {
	font-style: normal;
	display: block;
	position: absolute;
	top: -5.3em;
	left: -160px;
	width: 200px;
}

em {
	font-style: normal;
	display: block;
	position: absolute;
	top: -78px;
	left: -230px;
	width: 200px;
}

.assessDetail td {
	background-color: #FFF;
}
</style>
</head>
<body style="background-color: #f0f0f6;color:#404040;">
	<table>
		<tr>
			<th style="width:200px;">
				<div class="out" style="font-size:27px">
					<b>消费类型</b> <em>通话类型</em>
				</div>
			</th>
			<th>通话时长</th>
			<th>虚拟币</th>
		</tr>

		<tr  class="assessDetail">   
			  <td>国内直拨</td>   
			  <td>1 时长/分钟</td>   
			  <td>0.05/分钟</td>   
		  </tr>   
		  <tr  class="assessDetail">   
			  <td>国内回拨</td>   
			  <td>2 时长/分钟</td>   
			  <td>0.1/分钟</td>   
		  </tr> 
		  <tr  class="assessDetail">   
			  <td>洛杉矶</td>   
			  <td>1 时长/分钟</td>   
			  <td>0.2/分钟</td>   
		  </tr> 
		  <tr  class="assessDetail">   
			  <td >温哥华</td>   
			  <td>1 时长/分钟</td>   
			  <td>0.5/分钟</td>   
		  </tr> 
		  <tr  class="assessDetail">   
			  <td >香港</td>   
			  <td>1 时长/分钟</td>   
			  <td>0.3/分钟</td>   
		  </tr> 
		<tr>
		</tr>
	</table>
</body>
</html>

### HTML CSS 表格表头 斜线效果 实现方法 要实现表格表头带有斜线的效果,可以通过两种主要方式来完成:一种是通过伪元素 `::before` 或 `::after` 来绘制斜线;另一种则是利用 SVG 图像作为背景。 #### 方法一:使用伪元素 `.TableTwice` 这种方法基于第一个引用的内容[^1]。以下是完整的代码示例: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>斜线表头</title> <style type="text/css"> .TableTwice { position: relative; background: #fcfcfc; display: inline-block; } .TableTwice::before { content: ''; position: absolute; width: 1px; height: 216px; background-color: rgb(192, 192, 192); display: block; transform: rotate(-80deg); transform-origin: top; overflow: hidden; } .TableTwice span:first-child { padding-left: 100px; } .TableTwice span:last-child { padding-right: 100px; } </style> </head> <body> <table border="1" cellpadding="10"> <thead> <tr> <th class="TableTwice"><span>类别</span><span>名称</span></th> <th>数据列1</th> <th>数据列2</th> </tr> </thead> <tbody> <tr> <td>行1 列1</td> <td>行1 列2</td> <td>行1 列3</td> </tr> </tbody> </table> </body> </html> ``` 上述代码中,`.TableTwice::before` 使用了一个绝对定位的伪元素,并将其旋转 `-80deg` 形成一条斜线。两个 `<span>` 元素分别用于放置表头的不同部分文字。 --- #### 方法二:使用 SVG 背景图像 第二种方法来自第二个引用[^2]。此方法更灵活,允许自定义线条的颜色和位置。下面是具体实现: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>SVG 斜线表头</title> <style type="text/css"> .diagonal-header { background: #fff url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiPjxsaW5lIHgxPSIwIiB5MT0iMCIgeDI9IjEwMCUiIHkyPSIxMDAlIiBzdHJva2U9IndoaXRlIiBzdHJva2Utd2lkdGg9IjEiLz48L3N2Zz4=) no-repeat 100% center; text-align: left; padding-left: 20px; } </style> </head> <body> <table border="1" cellpadding="10"> <thead> <tr> <th class="diagonal-header">类别 / 名称</th> <th>数据列1</th> <th>数据列2</th> </tr> </thead> <tbody> <tr> <td>行1 列1</td> <td>行1 列2</td> <td>行1 列3</td> </tr> </tbody> </table> </body> </html> ``` 在此代码片段中,`background` 属性设置了一张 Base64 编码的 SVG 图片作为背景,该图片包含一条从左上到右下的白色直线。如果需要更改颜色或角度,则可以调整原始 SVG 的属性并重新编码为 Base64 格式[^2]。 --- #### 总结 以上提供了两种不同的解决方案,分别是基于伪元素的方法以及基于 SVG 背景图的方式。前者适合简单的场景,而后者则更加灵活且易于维护。 相关问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值