Search code examples
htmlcssfrontendsearchbarweb-frontend

Aligning Search bar with other elements in the nav


I previously asked a question about shifting the search bar on the right most side of my navigation bar, following the answers I did successfully shift it to the right, However now the search bar is not in-line with the other elements (Previously it was). Which CSS Property (if any) is preventing it from happening and what could be the solution?

Code :

body
    {
    	margin: 0;
    	background: #222;
    	font-weight: 300;
    	background-image: url('logo.jpeg');
    }
    
    .container
    {
    	width: 80%;
    	margin: 0 auto;
    }
    
    header
    {
    	background: #f3e5ab;
    	position: relative;
    }
    
    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: 70px;
    	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;
    }
    
    ul li:last-child
    {
    position: absolute;
    right: 0;
    top: 0;
    margin: 15px;
    padding: 0;
    
    }
<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>


Solution

  • I cannot say I am on the same page as far as it goes to the general layout and approach you chose, but here is a quick fix (if I understood correctly your issue)

    https://codepen.io/magom001/pen/mXwovO?editors=1100

    body
    {
        margin: 0;
        background: #222;
        font-weight: 300;
        background-image: url('logo.jpeg');
    }
    
    .container
    {
        width: 80%;
        margin: 0 auto;
    }
    
    .container::after {
      content: '';
      displayt: table;
      clear: both;
    }
    
    header
    {
        background: #f3e5ab;
        position: relative;
    }
    
    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: 70px;
        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;
    }
    
    ul li:last-child
    {
    position: absolute;
    right: 0;
    bottom: 0;
    margin: 15px;
    margin-bottom: 0px;
    padding: 0;
    
    }
    

    Just change top:0 to bottom: 0 and remove margin-bottom on the last-child.