近日小颖在根据公司之前的C# 代码来重构该项目,项目中有一个页面中有如下图所示的样式:
绘制一个白色顶部有缺口的边框,并且中间有文字
一开始不知道可以用 fieldset 和 legend这俩标签来实现,就想着用 CSS 绘制白色顶部缺口样式,最终用 border + clip-path 实现了,下面我们一起来看看吧 基础代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS绘制多边形</title>
<style type="text/css">
html,
body {
width: 100%;
height: 100%;
background-color: black;
}
* {
padding: 0;
margin: 0;
}
</style>
</head>
<body></body>
</html>
方法一:使用 CSS
CSS
.has-bd {
margin: 30px 0 0 30px;
width: 300px;
height: 200px;
display: inline-block;
border: 1px solid white;
clip-path: polygon(0 0, 20px 0, 20px 20px, 66px 20px, 66px 0, 100% 0, 100% 100%, 0 100%);
}
span {
font-size: 20px;
color: #fff;
position: absolute;
left: 63px;
top: 20px;
}
HTML
<body>
<span>标题</span>
<div class="has-bd">
</div>
</body>
方法二:使用HTML5中的fieldset 和 legend标签 + border
CSS
.field-box {
width: 300px;
height: 200px;
margin-top: 10px;
margin-left: 20px;
border: 1px solid white;
border-radius: 3px;
padding-left: 25px;
padding-bottom: 15px;
}
.top-title {
font-size: 20px;
margin-left: 20px;
padding: 0 10px;
color: #fff;
}
HTML
<body>
<fieldset class="field-box">
<legend class="top-title">标题</legend>
</fieldset>
</body>