html - CSS3 搜索框响应,搜索按钮分解

标签 html css responsive-design search-box

我想为我的网站设计一个响应式搜索框,所以当我找到这个 codepen fiddle - http://codepen.io/nikhil/pen/GuAho ,我摆弄它并最终得到了这个版本 - http://codepen.io/anon/pen/Qygrpe

<!-- language: lang-css -->

    /* CSS Search */

        .cf:before, .cf:after{
          content:"";
          display:table;
        }

        .cf:after{
          clear:both;
        }

        .cf{
          zoom:1;
        }

        /*-------------------------------------*/   

        .content-wrapper {
            width: 180px;
            padding: 0px;
            position:relative;
            -moz-border-radius: 4px;
            -webkit-border-radius: 4px;
            border-radius: 4px;

        }

    #search {
      position:relative;
      font-size:13px;font-family:"Dosis";
      float:right;
      margin-top:7px;
      right:5px;
    }
    #search-form {
      background-color:#314d5b;
      height:32px;
      line-height:32px;
      border:0 none;
      font-family:"Dosis";
      font-size:13px;
      font-weight:200;
      color:#b5c5ca;
      width:130px;
      -moz-border-radius: 4px 0 0 4px;
            -webkit-border-radius: 4px 0 0 4px;
            border-radius: 4px 0 0 4px;      
      box-shadow:inset 0 0 1px 0 rgba(0,0,0,0.2);
      padding:0 10px;
      transition:all 0.5s ease 0s;
    }
    #search-form .search-button,#search-form .search-button:hover{
      transition:all 0.5s ease 0s;
      height:21px;
      width:21px;
      line-height:30px;
      background-color:#ed0b77;
      border:0 none;
      float:right;
      cursor:pointer;
      outline:none;
    }
    #search-form:hover{
      background-color:#f0f0f0;
    }
    #search-form:focus{
      background-color:#eee;
      text-shadow:none !important;
      outline:none;
      color:#459bb6;
    }
    #box {display:none}
    .search-text {
      font-size:13px;
      font-family:"Dosis";
      float:right;
      cursor:pointer;
    }
    .search-text:hover {
      color:#459bb6
    }


        .content-wrapper button {
                overflow: visible;
            position: relative;
            float: right;
            border: 0;
            padding: 0;
            cursor: pointer;
            height: 32px;
            width: 30px;
            background: #416577;
            -moz-border-radius: 0 3px 3px 0;
            -webkit-border-radius: 0 3px 3px 0;
            border-radius: 0 3px 3px 0;      
            text-shadow: 0 -1px 0 rgba(0, 0 ,0, .3);
        }   

        .content-wrapper button:hover{      
            background: #e2614c;
        }   

        .content-wrapper button:active,
        .content-wrapper button:focus{   
            background: #c42f2f;    
        }

        .content-wrapper button:before {
            content: '';
            position: absolute;
            border-width: 8px 8px 8px 0;
            border-style: solid solid solid none;
            border-color: transparent #416577 transparent;
            top: 8px;
            left: -5px;
        }

        .content-wrapper button:hover:before{
            border-right-color: #e2614c;
        }

        .content-wrapper button:focus:before{
            border-right-color: #c42f2f;
        }    

        .content-wrapper button::-moz-focus-inner {
            border: 0;
            padding: 0;
        }


<!-- language: lang-html -->

    <div id='search'>
        <div class='content-wrapper cf'>
          <form action='/search' id='searchform' method='get'>
    <input id='search-form' name='q' onblur='if(this.value==&apos;&apos;)this.value=this.defaultValue;' onfocus='if(this.value==this.defaultValue)this.value=&apos;&apos;' type='text' value='Looking for something?'/>
          <button class='search-button' title='Search' type='submit'><img alt='search button' src='http://4.bp.blogspot.com/-e8Tp2vSgy2c/VpS0vc8zi7I/AAAAAAAAAN0/6rDw0GIVaSg/s1600/mysearch2.png' title='Search'/>
    </button>
    </form>
    </div>
    </div>

我将搜索框的 HTML 代码及其 CSS 添加到我的博客模板中,在屏幕宽度超过 600 像素之前它都能很好地显示。 看看

enter image description here

但是一旦宽度变为 600 像素或更小,我的搜索框就会分崩离析并且不会 float 在右侧,即使有足够的空间也是如此。我什至删除了菜单的几个部分,只留下“BLOGGER”和“MUSIC”,但搜索框仍然脱离了 div。

enter image description here

请指出我哪里出错了,我确定我在 CSS 的某个地方走错了方向。提前致谢。

最佳答案

尝试添加这个解决方案:

.content-wrapper {
    width: auto;
}
#search {
    position: absolute; // it was relative
}
div#menutop {
    position: relative;
}

关于html - CSS3 搜索框响应,搜索按钮分解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34741592/

相关文章:

javascript - 在 Windows Phone 上禁用双击缩放

javascript - CSS 和 JS - 单击背景时删除模式

css - 使用表格方法垂直对齐?

css - 媒体查询不适用于 ie 和手机

html、正文和 div 设置为 100% 宽度,但它仍然没有占用全部 100%

javascript - 导航栏下方的全屏 map

html - 尝试垂直线

css - Spotify 预览 API CSS 资源

css - 响应式主页(平板电脑): one single column

css - 如何使用Css随着屏幕宽度的减小而增加div的宽度?