165

Update again: I am closing this question by selecting the top answer to keep people from adding answers without really understanding the question. In reality there is no way to do it with the build in functionality without using grid or adding extra css. Grids do not work well if you are dealing with help-block elements that need to go beyond a short input for example but they are 'build-in'. If that is an issue I recommend using extra css classes which you can find in the BS3 discussion here. Now that BS4 is out it is possible to use the included sizing styles to manage this so this is not going to be relevant for much longer. Thanks all for good input on this popular SO question.

Update: This question remains open because it is about built-in functionality in BS to manage input width without resorting to grid (sometimes they have to be managed independently). I already use custom classes to manage this so this is not a how-to on basic css. The task is in BS feature discussion list and has yet to be addressed.

Original Question: Anyone figure out a way to manage input width on BS 3? I'm currently using some custom classes to add that functionality but I may have missed some non documented options.

Current docs say to use .col-lg-x but that clearly doesn't work as it can only be applied to the container div which then causes all kinds of layout/float issues.

Here's a fiddle. Weird is that on the fiddle I can't even get the form-group to resize.

http://jsfiddle.net/tX3ae/

<form role="form" class="row">
    <div class="form-group col-lg-1">
        <label for="code">Name</label>
        <input type="text" class="form-control">
    </div>

    <div class="form-group col-lg-1 ">
        <label for="code">Email</label>
        <input type="text" class="form-control input-normal">
    </div>

    <button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
cyberwombat
  • 38,105
  • 35
  • 175
  • 251
  • 5
    Why use bootstrap to manage these widths? It doesn't appear to be particularly good at it, and it introduces complexity. – Eamon Nerbonne Aug 31 '13 at 09:53
  • 1
    Why use bootstrap for this, @Eamon? Responsive design and alignment with the rest of your elements that bootstrap is handling come to mind. And anyway, the *whole point* of the question is how to do it without introducing complexity. – ctb Apr 22 '15 at 17:24
  • @ctb: plain CSS handles these kind of issues just fine; there's no need for the additional complexity of bootstrap. – Eamon Nerbonne Apr 23 '15 at 07:03
  • 1
    so wait, was this question answered? 38 upvotes is a lot. – rook May 04 '15 at 05:11
  • 1
    @rook - the answer is that no, there is no builld-in functionality though it's on their discussion list. However as alternatives go there are a few provided here until/if BS actually adds that. – cyberwombat May 04 '15 at 13:47

17 Answers17

105

What you want to do is certainly achievable.

What you want is to wrap each 'group' in a row, not the whole form with just one row. Here:

<div class="container">
    <h1>My form</h1>
    <p>How to make these input fields small and retain the layout.</p>
    <form role="form">
        <div class="row">
            <div class="form-group col-lg-1">
                <label for="code">Name</label>
                <input type="text" class="form-control" />
            </div>
        </div>

        <div class="row">
            <div class="form-group col-lg-1 ">
                <label for="code">Email</label>
                <input type="text" class="form-control input-normal" />
            </div>
        </div>
        <div class="row">
            <button type="submit" class="btn btn-default">Submit</button>
        </div>
    </form>
</div>

The NEW jsfiddle I made: NEW jsfiddle

Note that in the new fiddle, I've also added 'col-xs-5' so you can see it in smaller screens too - removing them makes no difference. But keep in mind in your original classes, you are only using 'col-lg-1'. That means if the screen width is smaller than the 'lg' media query size, then the default block behaviour is used. Basically by only applying 'col-lg-1', the logic you're employing is:

IF SCREEN WIDTH < 'lg' (1200px by default)

   USE DEFAULT BLOCK BEHAVIOUR (width=100%)

ELSE

   APPLY 'col-lg-1' (~95px)

See Bootstrap 3 grid system for more info. I hope I was clear otherwise let me know and I'd elaborate.

TMB
  • 4,683
  • 4
  • 25
  • 44
shadowf
  • 1,176
  • 1
  • 12
  • 15
  • yes that is what I put in my comment to @Skelly - there is an open issue on this and they will address it once things settled down. Adding row us not what I am looking for due to the extra markup that is totally unnecessary if they made the proper classes - which is what I did. I created .input1-12 with percentage width and I just apply that to the input - works great – cyberwombat Nov 02 '13 at 20:24
  • 7
    I haven't read much regarding this post but... "Use Bootstrap's predefined grid classes to align labels and groups of form controls in a horizontal layout by adding .form-horizontal to the form. Doing so changes .form-groups to behave as grid rows, so no need for .row." – dtc Dec 09 '13 at 19:36
  • @dtc That doesn't help much for input widths though? – Jacques Aug 21 '14 at 06:12
  • @shadowf And what if I want to make an input shorter than its label? Do I have to put an input in yet _another_ pair of divs? – PowerGamer Jul 06 '15 at 12:44
  • @PowerGamer I'm not sure I fully understand the question - do you mean to make the input element shorter? To do so, you only need to change the containing row's class - e.g. change col-xs-5 to col-xs-1. Because the contained label has no width specified on it but the contained input does (through the form-control class applied to it), then the label's size doesn't change but the input will become shorter. Does this answer your question? – shadowf Jul 07 '15 at 17:26
  • @shadowf Nope, I meant when the label text is long and I want an input to be shorter than label text without label text wrapping to next line, see http://jsfiddle.net/sf80qu6g/. Now to make an input ~1/3 of its current width you'll have to put an input into more divs. – PowerGamer Jul 07 '15 at 19:22
  • It works for me. But now I have an issue. The sizing is applicable for the label and the text box. How can I make the sizing apply only for the Text box and not the label. Modified fiddle here http://jsfiddle.net/jarajesh/kytsf0vz/ – Ananda Mar 27 '16 at 14:46
  • @Mr.X if I understand your question correctly (after all, it's Sunday morning...!), what you want is to limit the width of the textbox but not the label? If so, then you need to utilise the nesting feature of BS. Essentially, put "rows" inside each column and then your desired columns inside each row. It's probably easier if you see the code... Check out this: http://jsfiddle.net/1k1qoaer/ Please let me know if this answers your question. Cheers. – shadowf Mar 27 '16 at 15:57
  • 1
    thanks that works for me. By the way its Sunday 10 pm for me :-) as I am on the other side of the globe. – Ananda Mar 27 '16 at 16:20
  • i have checked your jsfiddle and saw there you use two class col-xs-5 & col-lg-1.....why? – Monojit Sarkar Jan 31 '18 at 11:10
  • @MonojitSarkar, this is the basis of how BS3's grid system works - you specify different classes so that BS will adjust according to the screen size and the classes you specify (see http://getbootstrap.com/docs/3.3/examples/grid/) In essence, it is what I describe in the pseudo code in my answer above: if there is only the col-lg-1 class on the element, when the screen size smaller than 1200px, col-lg-1 is ignored - and if a smaller class is not defined, the default behaviour of the element (being a div that'd be acting as a block) will kick in. Does this make sense? See the link to BS3 docs. – shadowf Jan 31 '18 at 15:55
72

In Bootstrap 3

You can simply create a custom style:

.form-control-inline {
    min-width: 0;
    width: auto;
    display: inline;
}

Then add it to form controls like so:

<div class="controls">
    <select id="expirymonth" class="form-control form-control-inline">
        <option value="01">01 - January</option>
        <option value="02">02 - February</option>
        <option value="03">03 - March</option>
        <option value="12">12 - December</option>
    </select>
    <select id="expiryyear" class="form-control form-control-inline">
        <option value="2014">2014</option>
        <option value="2015">2015</option>
        <option value="2016">2016</option>
    </select>
</div>

This way you don't have to put extra markup for layout in your HTML.

George Filippakos
  • 16,359
  • 15
  • 81
  • 92
  • 17
    This wins by far on the answers. It's clean, reusable, and just works. In fact, it should be put in bootstrap by default as this is a very common annoyance. Very rarely do you want pure block forms. – baweaver Sep 24 '14 at 18:08
  • I tried this solution but it didn't work for me. I wanted to limit the width of my field so I used 'width:200px' in the custom style, but that did not change the width. Only when I set 'max-width:200px' did I get the correct result. – paulo62 Nov 18 '14 at 13:38
  • When I use this the width will not override the form-control class auto width. – johnny Jun 08 '15 at 20:18
  • 1
    Agreed with comments here - I actually tried this before looking here, it doesn't seem to overwrite the defaults... unsure why some custom styles do and some don't. – Wil Jul 14 '15 at 15:12
  • 1
    Here's a fiddle demonstrating what this looks like: http://jsfiddle.net/yd1ukk10/ – brandones Sep 28 '15 at 18:58
  • please add to your answer a JS fiddle provided by @brandones (or create your own). Its very helpful to see the result when dealing with bootstrap markup – Zanshin13 Sep 08 '16 at 15:33
  • This is a nice solution, only I had to add `!important` to width & display. – Arghya C Feb 22 '17 at 10:41
  • Thank you, George, this works on Bootstrap 4, using various class libraries. – Two Feb 19 '21 at 12:14
25

ASP.net MVC go to Content- Site.css and remove or comment this line:

input,
select,
textarea {
    /*max-width: 280px;*/
}
user2648846
  • 375
  • 3
  • 3
  • If you look at the fiddle, you will see that the OP wants to make the fields smaller, not larger. The bigger problem here is that his site is not referencing the site.css at all. – Bmize729 Feb 22 '16 at 04:21
  • I didn't know that css rule was there and it was driving me crazy that my specified widths seemed to be being ignored. Thank you. – Michael S. Miller May 30 '19 at 17:59
11

I think you need to wrap the inputs inside a col-lg-4, and then inside the form-group and it all gets contained in a form-horizontal..

    <form class="form form-horizontal">
         <div class="form-group">
           <div class="col-md-3">
            <label>Email</label>
            <input type="email" class="form-control" id="email" placeholder="email">
           </div>
         </div>
         ...
     </form>

Demo on Bootply - http://bootply.com/78156

EDIT: From the Bootstrap 3 docs..

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.

So another option is to set a specific width using CSS:

.form-control {
    width:100px;
}

Or, apply the col-sm-* to the `form-group'.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
  • 6
    You have to add row class to form-group - I underwent a discussion with the Bootstrap team and they have added the possibility of input specific width control to their planning list. In the meantime we have to use full grids. If you'll go ahead and add row to the form-group class and remove the horizontal class I'll mark this answer. Here's the working fiddle http://jsfiddle.net/tdUtX/2/ and the planning https://github.com/twbs/bootstrap/issues/9397 – cyberwombat Aug 31 '13 at 16:38
  • 1
    +@Yashua - +1 great example. This also works with the input-group class, which was giving me trouble. – David Robbins Jan 29 '14 at 14:39
10

Current docs say to use .col-xs-x , no lg. Then I try in fiddle and it's seem to work :

http://jsfiddle.net/tX3ae/225/

to keep the layout maybe you can change where you put the class "row" like this :

<div class="container">
  <h1>My form</h1>
  <p>How to make these input fields small and retain the layout.</p>
  <div class="row">
    <form role="form" class="col-xs-3">

      <div class="form-group">
        <label for="name">Name</label>
        <input type="text" class="form-control" id="name" name="name" >
      </div>

      <div class="form-group">
        <label for="email">Email</label>
        <input type="text" class="form-control" id="email" name="email">
      </div>

      <button type="submit" class="btn btn-default">Submit</button>
    </form>
  </div>
</div>

http://jsfiddle.net/tX3ae/226/

Ced
  • 126
  • 1
  • 4
9
<div class="form-group col-lg-4">
    <label for="exampleInputEmail1">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
</div>

Add the class to the form.group to constraint the inputs

BetoCuevas
  • 123
  • 8
  • 8
    That doesn't really work. It causes every subsequent element to float next to this one. You have to add a div with class row on every form group which is ridiculous. – cyberwombat Aug 30 '13 at 19:03
6

If you are using the Master.Site template in Visual Studio 15, the base project has "Site.css" which OVERRIDES the width of form-control fields.

I could not get the width of my text boxes to get any wider than about 300px wide. I tried EVERYTHING and nothing worked. I found that there is a setting in Site.css which was causing the problem.

Get rid of this and you can get control over your field widths.

/* Set widths on the form inputs since otherwise they're 100% wide */
input[type="text"],
input[type="password"],
input[type="email"],
input[type="tel"],
input[type="select"] {
    max-width: 280px;
}
Joe Schmucker
  • 173
  • 1
  • 10
5

I know this is an old thread, but I experienced the same issue with an inline form, and none of the options above solved the issue. So I fixed my inline form like so:-

<form class="form-inline" action="" method="post" accept-charset="UTF-8">
    <div class="row">
        <div class="form-group col-xs-7" style="padding-right: 0;">
            <label class="sr-only" for="term">Search</label>
            <input type="text" class="form-control" style="width: 100% !important;" name="term" id="term" placeholder="Search..." autocomplete="off">
            <span class="help-block">0 results</span>
        </div>
        <div class="form-group col-xs-2">
            <button type="submit" name="search" class="btn btn-success" id="search">Search</button>
        </div>
    </div>
</form>

That was my solution. Bit hacky hack, but did the job for an inline form.

mattauckland
  • 483
  • 1
  • 7
  • 17
4

You can add the style attribute or you can add a definition for the input tag in a css file.

Option 1: adding the style attribute

<input type="text" class="form-control" id="ex1" style="width: 100px;">


Option 2: definition in css

input{
  width: 100px
}

You can change the 100px in auto

I hope I could help.

DW_
  • 104
  • 1
  • 11
3

In Bootstrap 3

All textual < input >, < textarea >, and < select > elements with .form-control are set to width: 100%; by default.

http://getbootstrap.com/css/#forms-example

It seems, in some cases, we have to set manually the max width we want for the inputs.

Anyway, your example works. Just check it with a large screen, so you can see the name and email fields are getting the 2/12 of the with (col-lg-1 + col-lg-1 and you have 12 columns). But if you have a smaller screen (just resize your browser), the inputs will expand until the end of the row.

José Antonio Postigo
  • 2,674
  • 24
  • 17
3

You don't have to give up simple css :)

.short { max-width: 300px; }
<input type="text" class="form-control short" id="...">
Eagle
  • 31
  • 1
2

If you're looking to simply reduce or increase the width of Bootstrap's input elements to your liking, I would use max-width in the CSS.

Here is a very simple example I created:

    <form style="max-width:500px">

    <div class="form-group"> 
    <input type="text" class="form-control" id="name" placeholder="Name">
    </div>

    <div class="form-group">
    <input type="email" class="form-control" id="email" placeholder="Email Address">
    </div>

    <div class="form-group">
    <textarea class="form-control" rows="5" placeholder="Message"></textarea>
    </div>   

    <button type="submit" class="btn btn-primary">Submit</button>
    </form>

I've set the whole form's maximum width to 500px. This way you won't need to use any of Bootstrap's grid system and it will also keep the form responsive.

Trevor
  • 10,903
  • 5
  • 61
  • 84
user3574492
  • 6,225
  • 9
  • 52
  • 105
1

I'm also struggled with the same problem, and this is my solution.

HTML source

<div class="input_width">
    <input type="text" class="form-control input-lg" placeholder="sample">
</div>

Cover input code with another div class

CSS source

.input_width{
   width: 450px;
}

give any width or margin setting on covered div class.

Bootstrap's input width is always default as 100%, so width is follow that covered width.

This is not the best way, but easiest and only solution that I solved the problem.

Hope this helped.

0

Bootstrap 3 I achieved a nice responsive form layout using the following:

<div class="row">
    <div class="form-group  col-sm-4">
        <label for=""> Date</label>
        <input type="date" class="form-control" id="date" name="date" placeholder=" date">
    </div>

    <div class="form-group  col-sm-4">
        <label for="hours">Hours</label>
        <input type="" class="form-control" id="hours" name="hours" placeholder="Total hours">
    </div>
</div>
rrk
  • 15,677
  • 4
  • 29
  • 45
Ravi Ram
  • 24,078
  • 21
  • 82
  • 113
0

I do not know why everyone has seem to overlook the site.css file in the Content folder. Look at line 22 in this file and you will see the settings for input to be controlled. It would appear that your site is not referencing this style sheet.

I added this:

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

to your fiddle and it works just fine.

You should never ever update bootstrap.css or bootstrap.min.css. Doing so will set you up to fail when bootstrap gets updated. That is why the site.css file is included. This is where you can make changes to site that will still give you the responsive design you are looking for.

Here is the fiddle with it working

Bmize729
  • 1,126
  • 8
  • 18
0

Add and define terms for the style="" to the input field, that's the easiest way to go about it: Example:

<form>
    <div class="form-group">
        <label for="email">Email address:</label>
        <input type="email" class="form-control" id="email" style="width:200px;">
    </div>
    <div class="form-group">
        <label for="pwd">Password:</label>
        <input type="password" class="form-control" id="pwd" style="width:200px">
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>
Vaidas
  • 1,494
  • 14
  • 21
0

Bootstrap uses the class 'form-input' for controlling the attributes of 'input fields'. Simply, add your own 'form-input' class with the desired width, border, text size, etc in your css file or head section.

(or else, directly add the size='5' inline code in input attributes in the body section.)

<script async src="//jsfiddle.net/tX3ae/embed/"></script>