2016-10-01 112 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>