2016-10-01 110 views
0

我试过使用float来使导航栏水平排列,搜索栏也将放置在导航栏旁边。
如何移动导航栏旁边的搜索栏

但结果是导航栏不再放置文本对齐中心。

body { 
 
    background-color: #C0C0C0; 
 
    margin: 0px; 
 
} 
 
#title { 
 
    text-align: center; 
 
    margin: 0; 
 
    font-size: 40px; 
 
    text-decoration: underline; 
 
} 
 
#wrapper { 
 
    margin: 3% 5%; 
 
    background-color: #FFF5EE; 
 
    min-width: 300px; 
 
    position: relative; 
 
} 
 
#navbar { 
 
    text-align: center; 
 
    font-size: 30px; 
 
    padding: 20px; 
 
    display: block; 
 
    background-color: #4B0082; 
 
} 
 
.nav li { 
 
    list-style-type: none; 
 
    display: inline-block; 
 
    padding: 30px; 
 
    background-color: red; 
 
} 
 
.nav li a { 
 
    color: white; 
 
}
<DOCTYPE html> 
 
    <html lang="en"> 
 

 
    <head> 
 
    <link rel="stylesheet" type="text/css" href="test1.css"> 
 
    <title>KDU Music</title> 
 
    <meta charset="utf-8"> 
 
    </head> 
 

 
    <body> 
 
    <div id="title"> 
 
     <p> 
 
     <h1>Welcome to KDU MUSIC CENTER</h1> 
 
     </p> 
 
    </div> 
 
    <div id="wrapper"> 
 
     <div id="navbar"> 
 
     <ul class="nav"> 
 
      <li><a href="index.html">Home</a> 
 
      </li> 
 
      <li><a href="findoutmore.php">Find out more</a> 
 
      </li> 
 
      <li><a href="offer.html">Offer</a> 
 
      </li> 
 
      <li><a href="credit.html">Credit</a> 
 
      </li> 
 
      <li><a href="#">Admin</a> 
 
      </li> 
 
      <li><a href="wireframe.html">WireFrame</a> 
 
      </li> 
 
     </ul> 
 
     <form class="formright"> 
 
      <input type="text" placeholder="Search"> 
 
      <button type="submit">Search</button> 
 
     </form> 
 
     </div> 
 
     <div id="content"> 
 
     <p>asdasdas</p> 
 
     </div> 
 
    </div> 
 
    </body> 
 

 
    </html>

我如何可以将搜索栏恰到好处的导航栏旁边?

enter image description here

回答

1

通过添加的APSolute位置,你将迫使元素总是在右上角:

.formright { 
    position: absolute; 
    right: 50px; 
    top: 80px 
} 

在这里,你可以看到它在行动: http://codepen.io/1GR3/pen/WGkwzo

对于较小的屏幕尺寸,您需要创建媒体查询,以另一种方式重新排列它。

请注意这是一个快速修复!要做到这一点,使用某种网格系统并将元素放入列中。在左侧,您可以使用空列或偏移量。

0

http://codepen.io/Navedkhan012/pen/wzAGRo

body { 
 
    background-color: #C0C0C0; 
 
    margin: 0px; 
 
} 
 
#title { 
 
    text-align: center; 
 
    margin: 0; 
 
    font-size: 40px; 
 
    text-decoration: underline; 
 
} 
 
#wrapper { 
 
    margin: 3% 5%; 
 
    background-color: #FFF5EE; 
 
    min-width: 300px; 
 
    position: relative; 
 
} 
 
#navbar { 
 
    text-align: center; 
 
    font-size: 30px; 
 
    padding: 20px; 
 
    display: block; 
 
    background-color: #4B0082; 
 
} 
 
.nav li { 
 
    list-style-type: none; 
 
    display: inline-block; 
 
    padding: 10px; 
 
    background-color: red; 
 
} 
 
.nav li a { 
 
    color: white; 
 
}
<div id="title"> 
 
     <p> 
 
     <h1>Welcome to KDU MUSIC CENTER</h1> 
 
     </p> 
 
    </div> 
 
    <div id="wrapper"> 
 
     <div id="navbar"> 
 
     <ul class="nav"> 
 
      <li><a href="index.html">Home</a> 
 
      </li> 
 
      <li><a href="findoutmore.php">Find out more</a> 
 
      </li> 
 
      <li><a href="offer.html">Offer</a> 
 
      </li> 
 
      <li><a href="credit.html">Credit</a> 
 
      </li> 
 
      <li><a href="#">Admin</a> 
 
      </li> 
 
      <li><a href="wireframe.html">WireFrame</a> 
 
      </li> 
 
      
 
      <li> 
 
     <form class="formright"> 
 
      <input type="text" placeholder="Search"> 
 
      <button type="submit">Search</button> 
 
     </form></li> 
 
     </ul> 
 
     
 
     </div> 
 
     <div id="content"> 
 
     <p>asdasdas</p> 
 
     </div> 
 
    </div> 
 
    </body>