【问题标题】:Responsive 100% width search bar that accounts for the other nav elements占其他导航元素的响应式 100% 宽度搜索栏
【发布时间】:2015-01-29 16:47:03
【问题描述】:

我正在尝试创建一个类似于 youtube 的导航栏,其中有两个 div,每个 div 都浮动到屏幕的相对两侧。在中间,我正在尝试制作一个扩展以填充剩余空间的搜索栏。当用户调整浏览器的大小时,搜索栏的宽度应该收缩以允许响应式布局。我一生都无法弄清楚如何做到这一点。每当我能够让搜索栏做出响应时,它都会导致右侧浮动 div 被推到它的下方,并且不会开始自行折叠,直到它到达屏幕的最边缘。我希望它在碰到右侧浮动 div 时立即开始折叠。

这里是html:

 <header>

    <nav id="page-nav-left" role="navigation"> </nav>

    <form id="searchBar-form">
        <input type="text" id="searchBar" placeholder="Search...">
    </form>

    <nav id="page-nav-right" role="navigation"> </nav>

 </header>

CSS 都在这个小提琴中:http://jsfiddle.net/L9qvpL32/

【问题讨论】:

    标签: html css navigation searchbar


    【解决方案1】:

    也许是这样的? http://jsfiddle.net/L9qvpL32/2/

    我只是更改了元素的顺序并更改了一些宽度:)

    如果您不希望搜索栏改变位置,只需更改为

    #searchBar-form {
        position: relative;
        overflow: hidden;
        min-width: 200px;
    }
    

    【讨论】:

      【解决方案2】:

      这就是你要找的吗? (您需要对其进行一些改进以阻止搜索在较小的屏幕上完全隐藏) http://jsfiddle.net/zxctbynn/

      <header>
          <div class="left"></div>
          <form id="searchBar-form" class="centre">
              <input type="text" id="searchBar" placeholder="Search...">
          </form>  
          <div class="right"></div>
      </header>
      
      header {
          position: fixed;
          top: 0px;
          z-index: 300;
          width: 100%;
          height: 48px;
          background-color: blue;
          display: inline;
      }
      
      .left{
          top: 0;
          background-color: red;
          width: 100px;
          height:100%;
          position: absolute;
      }
      
      .centre{
          top: 0;
          background-color: blue;
          width: auto;
          left: 100px;
          right: 100px;
          height:100%;
          position: absolute;
          padding: 0;
      }
      
      .centre input{
           width: 100%;   
      }
      

      【讨论】:

        【解决方案3】:
            header {
            position: fixed;
                top: 0px;
            z-index: 300;
            width: 100%;
            height: 48px;
            background-color: blue;
            float:left;
        }
        form{
            width:80%;
            float:left;
        }
        
        #page-nav-left {
            display: inline-block;
            height: 100%;
            width: 20%;
            margin: 0 auto;
            padding: 0px;       
            float: left;
            background-color: red;
        }   
        
        #page-nav-right {
            display: inline-block;
            height: 100%;
            width: 20%;
            margin: 0 auto;
            padding: 0px;       
            float: left;
            background-color: red;
        }
        
        #searchBar-form {
            position: relative;
            overflow: hidden;
            width:60%;
        }
        
        #searchBar {
            padding: 4px 15px;
            background-color: white;
            width: 100%;
        }
        
        #searchBar-button {
            float: right;
            padding: 4px 15px;
            border: 0px solid #207cca;
            color: black;
        }
        

        我假设这就是你想要的。您以像素为单位设置导航。始终将宽度设置为 % 以提高响应速度。 http://jsfiddle.net/L9qvpL32/1/

        【讨论】:

          【解决方案4】:

          您应该尝试使用flexbox。使用 flexbox,您可以在同一行中浮动尽可能多的 div,它们也会对父级和彼此做出响应。

          您还可以设置,哪个元素应该填充剩余空间,以及哪些元素的宽度固定为绝对值。

          • display: flex; ==> 把它变成 flexbox 模型
          • flex-grow: 0/1(或其他)==>如果有剩余空间,当前框可以增长
          • flex-shrink: 0/1(或其他)==>如果空间小于元素宽度,则当前框可以缩小

          你可以在这里找到一个很棒的 flexbox 教程: http://css-tricks.com/snippets/css/a-guide-to-flexbox/

          header {
              display: -webkit-flex;
              display: flex;
            
          	position: fixed;
          	top: 0px;
          	z-index: 300;
          	width: 100%;
          	height: 48px;
            
              background-color: blue;
          }
          
          #page-nav-left {
              -webkit-flex-grow: 0;
              flex-grow: 0;
              -webkit-flex-shrink: 0;
              flex-shrink: 0;
            
              width: 100px;
            
              background-color: red;
          }	
          
          #page-nav-right {
              -webkit-flex-grow: 0;
              flex-grow: 0;
              -webkit-flex-shrink: 0;
              flex-shrink: 0;
            
              width: 100px;
              background-color: red;
          }
          
          #searchBar-form {
              -webkit-flex-grow: 1;
              flex-grow: 1;
              -webkit-flex-shrink: 1;
              flex-shrink: 1;
          }
          #searchBar {
            width: 100%;
            max-width: 400px;
          }
            
          
          #searchBar-button {
          	padding: 4px 15px;
          	border: 0px solid #207cca;
          	color: black;
          }
          <header>
                  
          		<nav id="page-nav-left" role="navigation"> </nav>
          
          		<form id="searchBar-form">
                      
          	        <input type="text" id="searchBar" placeholder="Search...">
          	    </form>
                          
          		<nav id="page-nav-right" role="navigation"> </nav>
          
          </header>

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-12-31
            • 1970-01-01
            • 2017-01-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多