The positioning of my search bar icon is causing me some confusion

I am currently working on a Navigation Bar and I need some assistance with positioning the search Icon within the search bar. Is it recommended to use absolute positioning in a relative container for this purpose?

The form I have includes search-ct and an input field with the image placed next to it.

.navbar {
  width: 100%;
  height: 80px;
  background-color: red;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-evenly;
  align-items: center;
}

.navbar img {
  width: 100px;
  height: 70px;
}

.search-ct {
  width: 20%;
  box-sizing: border-box;
  align-items: center;
  display: flex;
  flex-wrap: no-wrap;
}

.search-icon {
  position: absolute;
  width: 25px!important;
  height: 30px!important;
  display: flex;
}

.search-input {
  height: 40px;
  border: 2px solid transparent;
  border-radius: 70px;
  text-indent: 10px;
}
<ul class='navbar'>


  <img src='../assets/images/favicon.png' class='img-1' />

  <form class='search-ct'>
    <input class='search-input' placeholder='Search'>
    <img class='search-icon' src='./assets/images/search.png'>

  </form>

  <div class='container-nav-buttons' style='display:none;'>
    <button><a>HOME</a></button>
    <button><a>SHOP</a></button>
    <button><a>APPS</a></button>
    <button><a>CONTACT</a></button>
  </div>


  <svg class='svg-nav' version="1.1" viewBox="0.0 0.0 960.0 720.0" fill="none" stroke="none" stroke-linecap="square" stroke-miterlimit="10" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg">...moreSVGcodehere...</svg>

  <img src='../assets/images/account.png' class='acct-img'>
</ul>

I would appreciate some guidance on how to achieve this layout effectively.

Answer №1

View the code on w3schools

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
* {box-sizing: border-box;}

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  overflow: hidden;
  background-color: #e9e9e9;
}

.topnav a {
  float: left;
  display: block;
  color: black;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.topnav a.active {
  background-color: #2196F3;
  color: white;
}

.topnav .search-container {
  float: right;
}

.topnav input[type=text] {
  padding: 6px;
  margin-top: 8px;
  font-size: 17px;
  border: none;
}

.topnav .search-container button {
  float: right;
  padding: 6px 10px;
  margin-top: 8px;
  margin-right: 16px;
  background: #ddd;
  font-size: 17px;
  border: none;
  cursor: pointer;
}

.topnav .search-container button:hover {
  background: #ccc;
}

@media screen and (max-width: 600px) {
  .topnav .search-container {
    float: none;
  }
  .topnav a, .topnav input[type=text], .topnav .search-container button {
    float: none;
    display: block;
    text-align: left;
    width: 100%;
    margin: 0;
    padding: 14px;
  }
  .topnav input[type=text] {
    border: 1px solid #ccc;  
  }
}
</style>
</head>
<body>

<div class="topnav">
  <a class="active" href="#home">Home</a>
  <a href="#about">About</a>
  <a href="#contact">Contact</a>
  <div class="search-container">
    <form action="/action_page.php">
      <input type="text" placeholder="Search.." name="search">
      <button type="submit"><i class="fa fa-search"></i></button>
    </form>
  </div>
</div>

<div style="padding-left:16px">
  <h2>Responsive Search Bar</h2>
  <p>Navigation bar with a search box and a submit button inside of it.</p>
  <p>Resize the browser window to see the responsive effect.</p>
</div>

</body>
</html>

Answer №2

Change the position from absolute to relative

.navbar {
  width: 100%;
  height: 80px;
  background-color: red;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-evenly;
  align-items: center;
}

.navbar img {
  width: 100px;
  height: 70px;
}

.search-ct {
  width: 20%;
  box-sizing: border-box;
  align-items: center;
  display: flex;
  flex-wrap: no-wrap;
}

.search-icon {
  position: relative;
  width: 25px!important;
  height: 30px!important;
  display: flex;
}

.search-input {
  height: 40px;
  border: 2px solid transparent;
  border-radius: 70px;
  text-indent: 10px;
}
<ul class='navbar'>


  <img src='../assets/images/favicon.png' class='img-1' />

  <form class='search-ct'>
    <input class='search-input' placeholder='Search'>
    <img class='search-icon' src='./assets/images/search.png'>

  </form>

  <div class='container-nav-buttons' style='display:none;'>
    <button><a>HOME</a></button>
    <button><a>SHOP</a></button>
    <button><a>APPS</a></button>
    <button><a>CONTACT</a></button>
  </div>


  <svg class='svg-nav' version="1.1" viewBox="0.0 0.0 960.0 720.0" fill="none" stroke="none" stroke-linecap="square" stroke-miterlimit="10" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg"><clipPath id="p.0"><path d="m0 0l960.0 0l0 720.0l-960.0 0l0 -720.0z" clip-rule="nonzero"/></clipPath><g clip-path="url(#p.0)"><path fill="#000000" fill-opacity="0.0" d="m0 0l960.0 0l0 720.0l-960.0 0z" fill-rule="evenodd"/><path fill="#000000" d="m269.50192 203.12325l-14.907623 309.4995l0 0c-1.9529419 40.545288 -17.996399 72.73224 -35.83406 71.89154c-17.837677 -0.84069824 -30.714783 -34.390625 -28.761856 -74.93591l14.907623 -309.4995l0 0c1.9529419 -40.545273 17.996399 -72.73219 35.83406 -71.89151c-17.837662 0.84069824 -30.714798 34.390656 28.761856 74.93591z" fill-rule="evenodd"/><path fill="#000000" d="m241.3195 203.3388l591.2756 0l0 61.480316l-591.2756 0z" fill-rule="evenodd"/><path fill="#000000" d="m241.3195 449.26282l532.8819 0l0 46.740143l-532.8819 0z" fill-rule="evenodd"/><path fill="#000000" d="m248.14636 333.82135l553.3858 0l0 33.19684l-553.3858 0z" fill-rule="evenodd"/><path fill="#000000" d="m845.83777 255.70149l-37.220947 198.45943l0 0c-4.8760986 25.998718 -19.406982 45.131836 -32.45569 42.735016c-13.048706 -2.39682 -19.67395 -25.415985 -14.797852 -51.414703l37.220947 -198.45943l0 0c4.8760986 -25.998718 19.406982 -45.131836 32.45569 -42.735c-13.048706 2.39682 -19.67395 25.41597 14.797852 51.41469z" fill-rule="evenodd"/><path fill="#000000" d="m427.79663 248.37027l-16.566925 205.85828l-25.606323 -2.0472412l16.566925 -205.85828z" fill-rule="evenodd"/><path fill="#000000" d="m611.26135 257.02603l-16.566895 205.85825l-25.606323 -2.0472412l16.566956 -205.85826z" fill-rule="evenodd"/><path fill="#000000" d="m221.75337 541.68036l482.86615 0l0 33.1969l-482.86615 0z" fill-rule="evenodd"/><path fill="#000000" d="m114.00525 126.666664l127.30708 0l0 56.566933l-127.30708 0z" fill-rule="evenodd"/><path fill="#000000" d="m216.47675 630.2187l0 0c0 -30.562622 26.136688 -55.338562 58.37796 -55.338562l0 0c15.482788 0 30.331451 5.8303223 41.27942 16.208313c10.947998 10.377991 17.09851 24.453552 17.09851 39.13025l0 0c0 30.562683 -26.136688 55.338623 -58.37793 55.338623l0 0c-32.241272 0 -58.37796 -24.77594 -58.37796 -55.338623z" fill-rule="evenodd"/><path fill="#000000" d="m569.0679 630.2187l0 0c0 -30.562622 26.136658 -55.338562 58.37793 -55.338562l0 0c15.482788 0 30.331482 5.8303223 41.27948 16.208313c10.947937 10.377991 17.09851 24.453552 17.09851 39.13025l0 0c0 30.562683 -26.136719 55.338623 -58.37799 55.338623l0 0c-32.241272 0 -58.37793 -24.77594 -58.37793 -55.338623z" fill-rule="evenodd"/></g></svg>

  <img src='../assets/images/account.png' class='acct-img'>
</ul>

Answer №3

When it comes to styling your search icon with CSS, consider switching its positioning from absolute to relative.

To learn more about the differences between using style="position:absolute" and style="position:relative", check out this helpful resource: Difference between style = "position:absolute" and style = "position:relative"

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Refreshing the form fields and storing the information using AngularJS

I have successfully implemented a basic form using AngularJS. The issue I am facing is that even after the user enters their details and submits the form, the values remain in the input fields. My goal is to store the details of multiple fields in the con ...

Emulate the Dynamics CRM Website

One of my clients is in need of a personalized page integrated into Microsoft Dynamics CRM. The task at hand may seem simple, as it involves embedding an iFrame. But here's the catch - my client is very particular and expects the embedded page to sea ...

Incorporating React's dynamic DIV layout algorithm to generate a nested box view design

My goal is to showcase a data model representation on a webpage, depicting objects, attributes, and child objects in a parent-child hierarchy. I had the idea of developing a versatile React component that can store a single data object while also allowing ...

PHP - the elusive task of extracting a value from an existing input

I'm attempting to utilize the POST method to retrieve values from a form. I decided to move this section to a test page: <form name="form" action="" method="post"> <input type="text" name="subject" id="subject" value="enter something" /> ...

bespoke HTML elements and properties

I'm currently facing some difficulties and I am unsure of how challenging it can be. I have tried following various tutorials, including those on SO and YouTube, but they all provide different approaches leaving me stuck. My goal is to create a custo ...

Using JavaScript, create a set of buttons within a div element and implement

$(document).ready(function() { $('#b1').click(function() { $('#uch').toggle("slow"); }); $('#b2').click(function() { $('#uch2').toggle("slow"); }) }) Although I'm not a program ...

Switching background colors (green/red) in an HTML void when a specific key is pressed - What's the trick?

Is there a solution available for changing the background color in HTML when a specific button is pressed, like the letter A? I want the color to switch from red to green or green to red when button A is pressed, and I would like it to stay that way even i ...

Inconsistencies in Bootstrap hover sizes

I am encountering an issue with the hover size in my 'content' class. The hover sizes are different due to discrepancies in paragraph line spacing across cards. Is there a solution to ensure all hovers appear equal in size on each card? For refer ...

Implementing the IF statement in jQuery

I've been trying to implement an overflow check when hovering over a div. The issue arises because I'm using jQuery for the hover function and then simple JavaScript for the overflow check in the next step. It seems that directly using an if-then ...

What methods are available for employing conditions in HTML attributes?

I need to implement a functionality in Angular where an image is loaded in a new window when the user clicks a button. The popup window will contain HTML content. Here's the Angular code snippet: <button class="btn btn-primary" type=&quo ...

Reduce the length of selection choices in the dropdown menu of WebForms

In my WebForms project, I have created a drop-down list by binding a list of data with 'Title' and 'Id' to it. Ddltitlelist.DataSource = submissionTitleList; Ddltitlelist.DataTextField = "Submission_Title"; Ddltitlelist.DataValueField ...

How Bootstrap Navigation is negatively impacting the width of the website

I am experiencing a problem with site width changes in the navigation. I have checked margins and padding, but couldn't find a solution to fix the issue. It seems like the row element is causing the problem, but I'm not sure why. Please assist me ...

Safari experiencing issues running Svg animations

We are currently developing a web application using React with SVG animations. While our SVG animations work perfectly on desktop and Android browsers, they do not animate on iOS devices (specifically tested on Safari on iPhone 5s, 8, and MacBook Pro). ...

Tips for monitoring data changes and removing a class name from the DOM using the `$watch` method

In my particular case, I have a title that is currently active. When a user clicks on another subtitle, I switch the data between them. When a subtitle is clicked, I add a class name update. Once the update occurs, I want to monitor this in the active dir ...

Mobile users are experiencing trouble viewing a non-responsive website on their devices in a responsive

I'm currently facing a perplexing problem that I've never encountered before. I'm developing an application with Rails and the Foundation by Zurb responsive framework. Strangely, the website is not responsive on mobile devices, even though i ...

Converting an array to an HTML form and then executing a submit function

I am feeling a bit overwhelmed because my knowledge of PHP is quite basic, but I am trying to learn as I go. Within a custom Wordpress plugin, there is a PHP function that looks like this: $pool->get_leagues( true ); This function returns an array co ...

Is there a way to retrieve just the parent's value?

<div class="island biz-owner-reply clearfix"> <div class="biz-owner-reply-header arrange arrange--6"> <div class="arrange_unit biz-owner-reply-photo"> <div class="photo-box pb-30s"> <a hre ...

Avoid jQuery ContextMenu Submenu Items from Expanding in Size on Mobile Devices

Recently, I started using the jQuery contextMenu which can be found at . The issue I encountered involves a menu with a submenu. Whenever I try to access the submenu items on mobile Safari or Chrome, the size of the menu items suddenly doubles and gets cu ...

Issue with relative input causing Jquery click event to not fire

Jquery $(document).on("click","[type=text]",function(event) { alert('test'); }); CSS .noWorking:focus{ z-index:100; position:relative; outline:none; box-shadow:0 0 0 1000px rgba(0,0,0,.2); } To ensure the shadow appears on top of all oth ...

A hobby project involving the creation of a webmail app using a combination of PHP

As a beginner in web development, I am eager to tackle a hobby project that will help me learn more about PHP and other web-related technologies. I have been considering creating a simple webmail client, but I'm not sure where to begin. I anticipate ...