157

I am struggling to create a textbox that fits the entire width of my container area.

<div class="row">
    <div class="col-md-12">
        <form class="form-inline" role="form">           
                <input type="text" class="form-control input-lg" id="search-church" placeholder="Your location (City, State, ZIP)">
                <button type="submit" class="btn btn-lg">Search</button>            
        </form>
    </div>
</div>

When I do the above, the two form elements are in-line, as I expect, but don't take up more than a few columns, at best. Hovering over the col-md-12 div in firebug shows it taking up the expected full width. It's just the text input that doesn't seem to fill. I even tried adding an in-line width value but it didn't change anything. I know this should be simple, just feeling really dumb now.

Here is a fiddle: http://jsfiddle.net/52VtD/4119/embedded/result/

EDIT:

The selected answer is thorough in every way and a wonderful help. It's what I ended up using. However I think my initial issue was actually a problem with the default MVC5 template within Visual Studio 2013. It contained this in Site.css:

input,
select,
textarea {
    max-width: 280px;
}

Obviously that was blocking the text-input from expanding appropriately... Fair warning to future ASP.NET template users...

Killnine
  • 5,728
  • 8
  • 39
  • 66
  • 7
    I just want to second your finding about the problem with the default MVC5 template. I just spend an hour researching this issue and (falsely) cursing at Bootstrap before I found a working sample in jsfiddle that prompted me to start again with a clean HTML page, and it worked fine. Only then did I figure there was an issue with the VS2013 template and came across this post. Goodness knows why the decision for the max-width, but it goes to show you have to be careful when using other people's code. It's also testament to going back to basics when stuck with an issue like this. – Jason Snelders Jun 27 '14 at 00:39
  • 2
    Thanks, also using asp.net template, but I already set max-width of input to none, but it still doesn't take the whole width, only if I set it to 100% manually. But then the button right to it goes down, and I want it to stay in the same line. – Ronen Festinger Dec 31 '14 at 23:30
  • @Killnine The edit in your question solved my headaches. It was in fact the problem, thank you for editing your post ! – Flame_Phoenix Aug 25 '15 at 08:50
  • I removed that bit of CSS from my site.css file and i am still having the same issue. Any ideas? – dave317 Jul 06 '16 at 18:28
  • ughhhhh I've spent too much time for what ended up being the default css in site.css. – Ryan Rodemoyer Dec 05 '16 at 02:54
  • Godamn, I've been trying to figure out what was wrong with this bootstrap for days... Damn Asp.net template... I could kiss you for figuring that out. EDIT: For those that remove those lines, just change it to 100% instead. – CyberClaw Mar 21 '17 at 14:53
  • It was the boilerplate ASP.NET MVC template site.css for me too! – Patrick Borkowicz Mar 22 '17 at 19:11
  • 1
    Dayuuumn! Been struggling with this and turns out it's an MVC issue! How stupid I am for not inspecting the css properties of the textbox! Thanks for pointing this out! – Hajjat Nov 24 '17 at 04:41
  • This was gold. Was struggling to find out what was going wrong – Jishnu Ramesh Sep 06 '18 at 02:30
  • 1
    Thanks for this. Like many before me it seems., I spent a good hour or two trying to figure this out. (Don't forget to F5 to totally refresh after editing site.css - that cost me a few minutes too :-)) – Peter Apr 11 '22 at 15:21

7 Answers7

112

The bootstrap docs says about this:

Requires custom widths Inputs, selects, and textareas are 100% wide by default in Bootstrap. To use the inline form, you'll have to set a width on the form controls used within.

The default width of 100% as all form elements gets when they got the class form-control didn't apply if you use the form-inline class on your form.

You could take a look at the bootstrap.css (or .less, whatever you prefer) where you will find this part:

.form-inline {

  // Kick in the inline
  @media (min-width: @screen-sm-min) {
    // Inline-block all the things for "inline"
    .form-group {
      display: inline-block;
      margin-bottom: 0;
      vertical-align: middle;
    }

    // In navbar-form, allow folks to *not* use `.form-group`
    .form-control {
      display: inline-block;
      width: auto; // Prevent labels from stacking above inputs in `.form-group`
      vertical-align: middle;
    }
    // Input groups need that 100% width though
    .input-group > .form-control {
      width: 100%;
    }

    [...]
  }
}

Maybe you should take a look at input-groups, since I guess they have exactly the markup you want to use (working fiddle here):

<div class="row">
   <div class="col-lg-12">
    <div class="input-group input-group-lg">
      <input type="text" class="form-control input-lg" id="search-church" placeholder="Your location (City, State, ZIP)">
      <span class="input-group-btn">
        <button class="btn btn-default btn-lg" type="submit">Search</button>
      </span>
    </div>
  </div>
</div>
morten.c
  • 3,414
  • 5
  • 40
  • 45
  • 3
    See my edit above. I think the Site.css in the ASP.NET template may have gotten me on this one. However, this is a terrific answer and very helpful for future reference. – Killnine Apr 01 '14 at 04:00
  • 5
    input groups are the way to go on this. Personally, I adjust the input group style to resemble an inline form when I don't want the button immediately beside the input. Working demo with Bootstrap LESS included: http://jsfiddle.net/silb3r/gwxc5c75/ – cfx Oct 04 '14 at 21:23
  • 1
    nice! I know the question is bootstrap-specific, but if anyone else wants to drop it into a bootstrapless (not bootstrap LESS;) site I've extracted the relevant styles here: http://stackoverflow.com/a/27413796/1241736 – henry Dec 11 '14 at 01:17
  • The Bootstrap link in the Fiddle is broken. It's probably pointing to a new version of Bootstrap and the Fiddle is no longer working. – Chloe Jun 12 '18 at 06:37
  • @Chloe You're right, the fiddle was using a bootstrap css file that no longer exists. I just updated the fiddle, it's working again. – morten.c Jun 12 '18 at 14:44
  • Just to save someone all of two seconds Googling, updated input groups link - https://getbootstrap.com/docs/4.4/components/input-group/ – Bondolin Mar 05 '20 at 15:04
37

have a look at something like this:

<form role="form">  
    <div class="row">
      <div class="col-xs-12">
        <div class="input-group input-group-lg">
            <input type="text" class="form-control" />
          <div class="input-group-btn">
            <button type="submit" class="btn">Search</button>
          </div><!-- /btn-group -->
        </div><!-- /input-group -->
      </div><!-- /.col-xs-12 -->
    </div><!-- /.row -->
</form>

http://jsfiddle.net/n6c7v/1/

haxxxton
  • 6,422
  • 3
  • 27
  • 57
  • Your answer helped me realise I was simply missing `class="form-control"` in the input fields. – stevec May 29 '22 at 18:02
11

As stated in a similar question, try removing instances of the input-group class and see if that helps.

refering to bootstrap:

Individual form controls automatically receive some global styling. All textual , , and elements with .form-control are set to width: 100%; by default. Wrap labels and controls in .form-group for optimum spacing.

Community
  • 1
  • 1
kurdtpage
  • 3,142
  • 1
  • 24
  • 24
4

Try something like below to achieve your desired result

input {
    max-width: 100%;
}
Fahim Al Mahmud Ashik
  • 1,083
  • 10
  • 26
4

You can use flex-fill class for input

<div class="row">
<div class="col-md-12">
    <form class="form-inline" role="form">           
            <input type="text" class="form-control input-lg flex-fill" id="search-church" placeholder="Your location (City, State, ZIP)">
            <button type="submit" class="btn btn-lg">Search</button>            
    </form>
</div>
Minh Ho
  • 53
  • 4
1

With Bootstrap >4.1 it's just a case of using the flexbox utility classes. Just have a flexbox container inside your column, and then give all the elements within it the "flex-fill" class. As with inline forms you'll need to set the margins/padding on the elements yourself.

.prop-label {
    margin: .25rem 0 !important;
}

.prop-field {
    margin-left: 1rem;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
 <div class="col-12">
  <div class="d-flex">
   <label class="flex-fill prop-label">Label:</label>
   <input type="text" class="flex-fill form-control prop-field">
  </div>
 </div>
</div>
John M
  • 2,510
  • 6
  • 23
  • 31
1

I know that this question is pretty old, but I stumbled upon it recently, found a solution that I liked better, and figured I'd share it.

Now that Bootstrap 5 is available, there's a new approach that works similarly to using input-groups, but looks more like an ordinary form, without any CSS tweaks:

<div class="row g-3 align-items-center">
    <div class="col-auto">
        <label>Label:</label>
    </div>
    <div class="col">
        <input class="form-control">
    </div>
    <div class="col-auto">
        <button type="button" class="btn btn-primary">Button</button>
    </div>
</div>

The col-auto class makes those columns fit themselves to their contents (the label and the button in this case), and anything with a col class should be evenly distributed to take up the remaining space.

TCF
  • 103
  • 6