Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexible width input field in Bootstrap 3 Navbar

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+

like image 552
EyalAr Avatar asked Jan 02 '14 17:01

EyalAr


People also ask

How to reduce size of input field in bootstrap?

Set the heights of input elements using classes like . input-lg and . input-sm . Set the widths of elements using grid column classes like .

What is navbar toggler?

Navbar Toggle LabelBootstrap Navbar component is designed for mobile first approach. Therefore, the navbar renders as collapsed on mobile devices. It can be toggled by a hamburger button.

How do I change the width of a bootstrap form?

For default size . form-control is used, for smaller size we can use . form-control class along with . form-control-sm and for larger size we can use .


2 Answers

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>
like image 178
Christina Avatar answered Oct 20 '22 08:10

Christina


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 :)

like image 3
Shourya Sharma Avatar answered Oct 20 '22 08:10

Shourya Sharma