实现效果如下:
利用flex实现:
这个需求的难点在于中间title如何保证在整个头部中保持居中的位置。
思路
按照人类思考的常识,如果是三栏布局的话,两侧靠两边,中间的是肯定可以居中的,所以,可以增加一个伪元素,宽度设定为跟左边的元素一样,就能够实现这样的布局。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.header {
display: flex;
justify-content: space-between;
height: 100px;
}
.left {
background-color: aqua;
width: 60px;
}
.title {
background-color: burlywood;
}
.header::after {
content: "";
width: 60px;
display: block;
visibility: hidden;
}
</style>
</head>
<body>
<div class="header">
<div class="left">左边</div>
<div class="title">中间标题</div>
</div>
</body>
</html>