11

I'm trying to create a navigation bar which contains an input field. I would like the input field to occupy all available space in the navigation bar.

Following this answer, I successfully created a layout similar to what I want, which contains an "addon" icon to the left of the input field (see code here).

Input field with an addon icon

But: I don't want the icon near the input field.

The following code controls the input field:

<form class="navbar-form">
  <div class="form-group" style="display:inline;">
    <div class="input-group">
      <span class="input-group-addon">
        <span class="glyphicon glyphicon-search"></span>
      </span>
      <input type="text" class="form-control">
    </div>
  </div>
</form>

The problem is that removing <span class="input-group-addon">...</span> to get rid of the icon, makes the input field contract to a smaller size and loose the rounded edges (which is less important. see code here).

Smaller field with sharp edges

Of course it doesn't make sense to wrap a single input field in an "input-group". But removing the "input-group" wrapper causes the input field to expand and break into a new line (see code here).

Input field breaks into a new line

After looking at Bootstrap.css I tried to create a new css class which mimics the relevant code of the input-group class. This brings back the input field inline in the navbar, but still does not make it expand to occupy all available space (see code here).

Input field with input-group-mimic

My question is: How do I get the layout I want without the icon?
Bonus: Why are the "input-group" and the icon making the input field expand?

Required browser compatibility: Chrome, Firefox, IE8+

Community
  • 1
  • 1
EyalAr
  • 3,160
  • 1
  • 22
  • 30

3 Answers3

10

Well, most folks didn't used have to design with tables, but in 99 when I began, tables created the layouts and we used spacer .gifs and all kinds of crap to design with. When you have a display:table on the container and a display:table-cell on the contents inside it, since the .form-control is empty there has to be something else to force the width of the element to take up the space, or it's going to act like an empty cell. So you have to have an empty span with some padding on it to force it.

http://jsbin.com/aXOZEXi/1 -- Bootstrap 3.0 - 3.1

http://jsbin.com/aXOZEXi/2/edit -- Bootstrap 3.2

.test {
  display:table;
}

.test > .form-control {
  display: table-cell;
  margin-left:-3px;
}

.test > span {
  display: table-cell;
  width:1%;
  padding:0 3px;
}

HTML

<form class="navbar-form">
  <div class="form-group" style="display:inline;">
    <div class="test">
      <span><!--shim--></span>
      <input type="text" class="form-control">
    </div>
  </div>
</form>
Community
  • 1
  • 1
Christina
  • 34,296
  • 17
  • 83
  • 119
3

Replace with this html: (made changes to glyphicon glyphicon-search and background-color, border of input-group-addon) Made the round edges too :)

<div class="container">

<nav class="navbar navbar-default" role="navigation">
    <div class="container">

    <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapsible">
            <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="#">Some Brand</a>
    </div>

    <div class="navbar-collapse collapse" id="navbar-collapsible">

        <ul class="nav navbar-nav navbar-left">
            <li><a href="#">Link 1</a></li>
            <li><a href="#">Link 2</a></li>
        </ul>

        <div class="navbar-form navbar-right btn-group">
            <button type="button" class="btn btn-default">Button 1</button>
            <button type="button" class="btn btn-default">Button 2</button>
            <button type="button" class="btn btn-default">Button 3</button>
        </div>

        <form class="navbar-form">
            <div class="form-group" style="display:inline;">
                <div class="input-group">
                  <span class="input-group-addon" style="background-color:transparent; border:none"><span class=""></span></span>
                  <input type="text" class="form-control" style="border-bottom-left-radius: 4px;border-top-left-radius: 4px;">
                </div>
            </div>
        </form>

    </div>

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

Worked really hard to get to what you require. hope you appreciate :)

Shourya Sharma
  • 547
  • 4
  • 23
  • 1
    Thanks for the effort. Manually making the unwanted parts invisible, is indeed an option; but for me it's more of a hack. I am looking for a more valid and elegant solution. Also note that your solution creates uneven spaces to the sides of the input field, which again can manually be fixed. But, again, I do not wish to resort to such methods. – EyalAr Jan 03 '14 at 01:20
0

This will get you pretty much where you want to be

<div class="form-group" style="display:inline;">
    <div class="input-group col-lg-6 col-md-5 col-sm-3 center-block col-xs-12">
        <input class="form-control" type="text" placeholder="Search">
    </div>
</div>

It will not fill exactly all the space but it will fill most of it and will center the search box so it does not look out of place. Added more dynamic column sizing extra small screen uses a dropdown menu so I re-sized the box to fit the screen. to build a fully responsize page as you are describing you should probably nest everything inside a grid because col-?-? is not a fixed width it is a percentage width based on it's container.

engineersmnky
  • 25,495
  • 2
  • 36
  • 52
  • Thank you for the suggestion, but it does not work: http://bootply.com/103533 (The input field is not centered, and when resizing the screen it breaks into a new line). – EyalAr Jan 02 '14 at 17:42
  • Additionally, I would like the layout to be responsive, and to be able to accomodate a varying number of links and buttons; thus setting a predefined column size is not a good solution in this case. – EyalAr Jan 02 '14 at 17:45
  • Updated my answer but a fully responsive layout should be built on the grid system. column-size is not a fixed width but based on the size of the viewport and it's parent container. – engineersmnky Jan 02 '14 at 18:03