将搜索栏移动到导航栏的右侧

时间:2018-02-13 06:39:43

标签: html css frontend navbar nav

我有以下代码。我想移动导航栏最右侧的搜索栏以及我想要添加搜索图标。我已经使用float:right属性进行搜索但没有用。如何将其向右移?以下是我的HTML和CSS代码

HTML:

    <body>
    <header>
    <div class="container">
        <nav>
            <ul> 
                <li> <a href="#"> Home </a></li>
                <li> <a href="#"> About </a></li>
                <li> <a href="#"> Services </a></li>
                <li> <a href="#"> Products </a></li>
                <li> <a href="#"> Contact Us </a></li>
                <li> <form class="form"> <input type="text" name="Search" placeholder="Search"> </form> </li>
             </ul>
        </nav>
    </div>
    </header>
</body>

以下是我的CSS:

body
{
    margin: 0;
    background: #222;
    font-family: 'Work-Sans',sans-serif;
    font-weight: 300;
}

.container
{
    width: 80%;
    margin: 0 auto;
}

header
{
    background: #f3e5ab;
}

header::after
{
    content: '';
    display: table;
    clear: both;
}

nav
{
    float: left;
}

nav ul
{
    margin: 0;
    padding: 0;
    list-style: none;
}

nav li
{
    display: inline-block;
    margin-left: 50px;
    padding-top: 30px;
    position: relative;
}

nav ul li  a
{
color: #444;
text-decoration: none;
text-transform: uppercase;
font-size: 14px;
}

nav a:hover
{

}


nav a::before
{
    content: '';
    display: block;
    height: 5px;
    width: 0%;
    background-color: #444;

    transition: all ease-in-out 500ms;


}
nav a:hover::before
{
    width: 100%;
}

form .form
{
    float: right;
}

2 个答案:

答案 0 :(得分:1)

你必须定位它。

尝试:

ul li:last-child {
  position: absolute;
  right: 0;
  top: 0;
  margin: 15px;
  padding: 0;
}
  header {
    background: #f3e5ab;
    position: relative
  }
nav li {
    display: inline-block;
    margin-left: 50px;
    padding: 15px 0px;
    position: relative;
}

http://jsfiddle.net/lotusgodkk/GCu2D/2359/

答案 1 :(得分:0)

我希望这就是你想要的。

只需将.search-bar浮动到右侧即可。它将在展开的屏幕右侧。

  

要在其中保留搜索图标,只需将其置于相对位置并在其中添加<span class="search-icon"></span>之类的额外元素即可。让这个位置绝对。

.search-bar{
    position:relative;
}

.search-bar .search-icon{
    position:absolute;
    right: 5px; //adjust this
    top: 2px; //adjust this
}

body
{
    margin: 0;
    background: #222;
    font-family: 'Work-Sans',sans-serif;
    font-weight: 300;
}

.container
{
    width: 80%;
    margin: 0 auto;
}

header
{
    background: #f3e5ab;
}

header::after
{
    content: '';
    display: table;
    clear: both;
}

nav
{
    float: left;
}

nav ul
{
    margin: 0;
    padding: 0;
    list-style: none;
}

nav li
{
    display: inline-block;
    margin-left: 50px;
    padding-top: 30px;
    position: relative;
}

nav ul li  a
{
color: #444;
text-decoration: none;
text-transform: uppercase;
font-size: 14px;
}

nav a:hover
{

}


nav a::before
{
    content: '';
    display: block;
    height: 5px;
    width: 0%;
    background-color: #444;

    transition: all ease-in-out 500ms;


}
nav a:hover::before
{
    width: 100%;
}

form .form
{
    float: right;
}

.search-bar{
    float: right;
}
<body>
    <header>
    <div class="container">
        <nav>
            <ul> 
                <li> <a href="#"> Home </a></li>
                <li> <a href="#"> About </a></li>
                <li> <a href="#"> Services </a></li>
                <li> <a href="#"> Products </a></li>
                <li> <a href="#"> Contact Us </a></li>
                <li class="search-bar"> <form class="form"> <input type="text" name="Search" placeholder="Search"> </form> </li>
             </ul>
        </nav>
    </div>
    </header>
</body>