<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>table</title>
<link rel="stylesheet" href="../icon/iconfont.css">
<style>
* {
margin: 0;
padding: 0;
}
header {
height:40px;
background: red;
box-sizing: border-box;
font-size: 0;
padding: 0 6px;
display: table;
width: 100%;
}
.tr{
display: table-row;
}
.tr>div{
text-align: center;
display: table-cell;
vertical-align: middle;
}
.city-name {
font-size: 14px;
color: #ffffff;
}
.icon-unfold {
font-size: 10px;
}
.search{
padding: 0 10px 0 0;
position: relative;
}
.search input{
height: 30px;
box-sizing: border-box;
border-radius: 20px;
width: 100%;
padding: 0 6px 0 20px;
}
.search i {
position: absolute;
top: 12px;
left: 5px;
font-weight: bold;
}
.person{
color: #ffffff;
}
</style>
</head>
<body>
<header>
<div class="tr">
<div class="city-name">北京 <i class="iconfont icon-unfold"></i></div>
<div class="search">
<i class="iconfont icon-search"></i>
<input type="text" class="search-input" placeholder="请输入关键字">
</div>
<div class="person"><i class="iconfont icon-people"></i></div>
</div>
</header>
</body>
</html>
期望效果如下(已用flex布局实现)
“北京”处宽度自适应 然后右边的icon固定宽度16px,中间的search占用剩余宽度。
用上述table布局实现的效果如下
问题: "北京"和右边的icon被自动分配了一个宽度,并不是像flex布局那样自适应宽度,求原因及解决方法。
table 里的 td 就是尽量均分剩余宽度,不想这样的话用 flex 挺好的