方法一:使用border
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.div1 {
width: 200px;
height: 50px;
line-height: 50px;
background-color: #ccc;
text-align: center;
position: relative;
/* 方法一:使用 border-left */
border-left: 5px solid red;
}
</style>
</head>
<body>
<div class="div1">左边竖线</div>
</body>
</html>
效果图
方法二:使用伪元素来实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.div1 {
width: 200px;
height: 50px;
line-height: 50px;
background-color: #ccc;
text-align: center;
position: relative;
}
.div1::before {
content: "";
width: 5px;
height: 50px;
position: absolute;
top: 0;
left: 0;
background-color: red;
}
</style>
</head>
<body>
<div class="div1">左边竖线</div>
</body>
</html>
效果图
参考链接:https://blog.csdn.net/llll789789/article/details/98245510