Nse Nse - 6 months ago 83
HTML Question

Bootstrap search field with icon in navigation header not appearing correctly

I'm attempting to add a search box with a header to my Bootstrap 3 template. The template that I'm using was purchased from the Bootstrap themes page and is the Light UI Dashboard theme.

I found this answer on Stack Overflow but it's not appearing correctly (it appears incorrectly in both Chrome and Edge).

How to add a search box with icon to the navbar in Bootstrap 3?

I modified my code to include the search box in my header however the glyphicon doesn't display and the search box is out of alignment. Here is a screen shot.

enter image description here

My code is as follows:



@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');

<form runat="server">
<!-- Fixed navbar -->
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Crown Ent.</a>
</div>
<div id="navbar" class="navbar-collapse collapse">

<ul class="nav navbar-nav navbar-left">
<li role="presentation" id="dashboard"><a href="./">Dashboard</a></li>
<li role="presentation" id="workorders"><a href="workorders.aspx">Work Orders</a></li>
<li role="presentation" id="invoices"><a href="invoices.aspx">Invoices</a></li>
<li role="presentation" id="accounts"><a href="accounts.aspx">Accounts</a></li>
<li role="presentation" id="people"><a href="people.aspx">People</a></li>
</ul>

<ul class="nav navbar-nav navbar-right">
<li><a runat="server" href="~/admin">Settings</a></li>
<li><a runat="server" href="~/admin">Help</a></li>
</ul>

<form class="navbar-form" style="padding-top:5px;">
<div class="form-group" style="display:inline;margin-top:5px;">
<div class="input-group" style="display:table;">
<span class="input-group-addon"><i class="glyphicon glyphicon-search"></i></span>
<input class="form-control" name="search" placeholder="Search Here" id="crown-search" autocomplete="off" autofocus="autofocus" type="text">
</div>
</div>
</form>

</div>
</div>
</nav>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>





I can't see anything wrong with the code. Adjusting the CSS margin in an attempt to fix the positioning is also ineffective.

Answer

UPD. I removed the first line <form runat="server"> and used the code

  <form class="navbar-form" role="search">
    <div class="form-group">
      <div class="input-group">

instead of

  <form class="navbar-form" style="padding-top:5px;">
    <div class="form-group" style="display:inline;margin-top:5px;">
      <div class="input-group" style="display:table;">

And I've added some CSS to make the form wider. Please check the result.

@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');

#navbar .navbar-form {
  overflow: hidden;
}
#navbar .form-group,
#navbar .input-group {
  width: 100%;
}
#navbar .input-group-addon {
  width: 39px;
}
<!-- Fixed navbar -->
<nav class="navbar navbar-inverse navbar-fixed-top">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="/">Crown Ent.</a>
    </div>
    <div id="navbar" class="navbar-collapse collapse">

      <ul class="nav navbar-nav navbar-left">
        <li role="presentation" id="dashboard"><a href="./">Dashboard</a></li>
        <li role="presentation" id="workorders"><a href="workorders.aspx">Work Orders</a></li>
        <li role="presentation" id="invoices"><a href="invoices.aspx">Invoices</a></li>
        <li role="presentation" id="accounts"><a href="accounts.aspx">Accounts</a></li>
        <li role="presentation" id="people"><a href="people.aspx">People</a></li>
      </ul>

      <ul class="nav navbar-nav navbar-right">
        <li><a runat="server" href="~/admin">Settings</a></li>
        <li><a runat="server" href="~/admin">Help</a></li>
      </ul>

      <form class="navbar-form" role="search">
        <div class="form-group">
          <div class="input-group">
            <span class="input-group-addon"><i class="glyphicon glyphicon-search"></i></span>
            <input class="form-control" name="search" placeholder="Search Here" id="crown-search" autocomplete="off" autofocus="autofocus" type="text">
          </div>
        </div>
      </form>

    </div>
  </div>
</nav>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>