1

I can't find a way to make my input field (a search box) and its relative icon (a magnifier lens) to float to the right of the navbar in my website (while leaving my logo on the left of the nav). Any suggestion?

HTML:

 <nav>
        <div class="nav-container">
            <div class="logo">
                <a href="">
                    <h1>MyWebsiteName.com</h1>
                </a>
            </div>
            <form action="javascript:redirect()" autocomplete="off">
                <input type="search" name="search" id="search" class="form-control">
                <ul class=mylist list-group" id="result"></ul>  <!--this line is for JS-->
                <button type="submit">
                   <svg width="50" height="50" viewBox="0 0 77 77" fill="none" 
                           xmlns="http://www.w3.org/2000/svg">
                   </svg> </button>
            </form>

        </div>
</nav>

CSS:


.nav-container {
  display: flex;
  align-items: center;
  padding: 16px 0;
  position: relative;
}
.nav-container form {
  display: flex;
  align-items: center;
  justify-content: center;
}
nav input {
  font-size: 1.20em;
  border: solid 2px #E5E5E5;
  border-radius: 25px;
  background: none;
  color: #F4F4F4; 
  padding: 14px;
  margin: 0 20px 0 0;
  width: 260px;
}
form {
  float: right;
}
Johannes
  • 64,305
  • 18
  • 73
  • 130
leowebdev
  • 23
  • 5

3 Answers3

2

Use margin-left: auto; on the form instead of float: right - it's inside a flex container (.nav-container), where float will not be effective, but margin-left: auto will move it as far right as possible.

.nav-container {
  display: flex;
  align-items: center;
  padding: 16px 0 16px 0;
  position: relative;
}

.nav-container form {
  display: flex;
  align-items: center;
  justify-content: center;
}

nav input {
  font-size: 1.20em;
  border: solid 2px #E5E5E5;
  border-radius: 25px;
  background: none;
  color: #F4F4F4;
  padding: 14px;
  margin: 0 20px 0 0;
  width: 260px;
}

form {
  margin-left: auto;
}
<nav>
  <div class="nav-container">
    <div class="logo">
      <a href="">
        <h1>MyWebsiteName.com</h1>
      </a>
    </div>
    <form action="javascript:redirect()" autocomplete="off">
      <input type="search" name="search" id="search" class="form-control">
      <ul class=mylist list-group " id="result "></ul>     <!-- this line is for JS -->
                <button type="submit ">
                   <svg width="50 " height="50 " viewBox="0 0 77 77 " fill="none " 
                           xmlns="http://www.w3.org/2000/svg ">
                   </svg> </button>
            </form>

        </div>
</nav>
Johannes
  • 64,305
  • 18
  • 73
  • 130
1

There are multiple ways to do it.

flexbox

grid

NOTE:

Due to space constraint in the snippet you have to view it in full screen.

  1. I just used position:absolute; this will move your content towards right.
  2. You can have a wrapper around those 2 input and use flex to keep those 2 items towards right.
  3. Don't use float it has become old and it not very much flexible when you have your responsive design.

.nav-container {
  display: flex;
  align-items: center;
  padding: 16px 0 16px 0;
  position: relative;
}

.nav-container form {
  display: flex;
  align-items: center;
  justify-content: center;
}

nav input {
  font-size: 1.20em;
  border: solid 2px #E5E5E5;
  border-radius: 25px;
  background: none;
  color: #F4F4F4;
  padding: 14px;
  margin: 0 20px 0 0;
  width: 260px;
}

.moveRight {
  position: absolute;
  right: 10px;
}
<nav>
  <div class="nav-container">
    <div class="logo">
      <a href="">
        <h1>MyWebsiteName.com</h1>
      </a>
    </div>
    <form action="javascript:redirect()" autocomplete="off" class="moveRight">
      <input type="search" name="search" id="search" class="form-control">
      <ul class="mylist list-group" id="result"></ul>
      <!-- this line is for JS -->
      <button type="submit">
                   <svg width="50" height="50" viewBox="0 0 77 77" fill="none" 
                           xmlns="http://www.w3.org/2000/svg">
                   </svg> </button>
    </form>

  </div>
</nav>
Manjuboyz
  • 6,978
  • 3
  • 21
  • 43
  • Thank you, it works! :) I didn't know that float was considered old, what do you usually prefer for responsive design? – leowebdev May 24 '20 at 17:42
  • 1
    Glad it works! the `position:absolute;` will works for your case and for the responsiveness since it keeps the those 2 controls towards right always. When you consider to do any responsive design especially layout design try using either `flexbox` or `grid` these 2 are really good towards responsiveness design, start exploring those it will give you very good information. – Manjuboyz May 24 '20 at 17:46
  • 1
    I have added both `flexbox` and `grid` layout documentation which may help you further. happy coding. – Manjuboyz May 24 '20 at 17:48
  • Wonderful, I really appreciated :) – leowebdev May 24 '20 at 18:28
0

Missing this

.nav-container {
justify-content: space-between;
}
Scott
  • 1
  • 2