搜索框 - div中input自适应

当须要开发如上这样一个搜索框时。html

由于是移动端开发,手机屏幕宽度的不肯定性,因此不能使用平时的定宽开发。web

分析如上图:app

    1.该搜索框由两部分组成:input搜索框+取消按钮;
webapp

    2.搜索框距离左右两边的距离是固定的,即图中1和2相等;布局

    3.取消按钮的大小是固定的,以及距离input距离是固定的;
优化

    4.input随着屏幕宽度的变化自适应变化。
url

代码:spa

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>搜索框 - div中input自适应</title>
<style>
*{
    margin:0;
    padding:0;
}
.search{
    position: relative;
    height: 32px;
    border-bottom:1px solid #d3d3d3;
    overflow:hidden;
    background: #fff;
    text-align: center;
    overflow:hidden;
    padding:4px 20px;
}
.search_bar{
    height: 32px;
    margin-right:92px;
}
.search_bar>input{
    width:100%;
    display:inline-block;  
    *display:inline;  
    border:1px solid #ccc;
    border-radius:5px;
    font-size:14px;
    height:30px;
    padding:0 10px 0 32px;
    background: #f0f0f0 url(/images/webapp/icon.png) no-repeat;
    background-size:32px 32px;
}
.search_btn>span{
    position:absolute;
    top:4px;
    right:20px;
    z-index:9;
    line-height:32px;
    color:green;
}
</style>
</head>
<body>
<!--设置总体布局-->
<div>
    <!--input包裹层,利用块元素的特性(width:100%)使其能够彻底填充父级div,使用margin-right在右边留出固定宽度-->
    <div>
        <!--使input完整填充input包裹层,同时设置其padding值,用以存放搜索小图标+页面优化-->
        <input type="text" placeholder="搜索"/>
     </div>
     <!--btn包裹层,无关紧要,使用position将其放置在input右边固定位置,利用z-index使得图层不会被覆盖-->
     <div>
         <span>取消</span>
      </div>
    </div>
</body>
</html>
相关文章
相关标签/搜索