Bootstrap搜索字段,导航标题中的图标无法正确显示

时间:2016-06-06 19:36:48

标签: html css twitter-bootstrap twitter-bootstrap-3

我正在尝试将带有标题的搜索框添加到我的Bootstrap 3模板中。我正在使用的模板是从Bootstrap主题页面购买的,是Light UI Dashboard主题。

我在Stack Overflow上找到了这个答案,但它没有正确显示(在Chrome和Edge中都显示不正确)。

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

我修改了我的代码以在我的标题中包含搜索框,但是不显示glyphicon并且搜索框不对齐。这是一个截图。

enter image description here

我的代码如下:

@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>

我看不出代码有什么问题。调整CSS边距以尝试修复定位也是无效的。

3 个答案:

答案 0 :(得分:1)

UPD。我删除了第一行<form runat="server">并使用了代码

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

而不是

  <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;">

我添加了一些CSS来扩大表单。请检查结果。

@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>

答案 1 :(得分:0)

如果您希望它位于另一侧,您只需在输入字段后添加包含搜索图标的范围

所以

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

另外看起来你需要在输入组类中应用margin-top值,随意添加另一个名为search的类或者也可以应用它的东西

这似乎是一个简单的CSS修复

仅供参考我的所有3个浏览器(Chrome,FF和IE)中都会显示字形图标

答案 2 :(得分:0)

为了解决不对齐问题,改变了这个:

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

到此:

<div style="margin-top:5px;">