Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shorten the width of input field? Bootstrap v5 or CSS

I can't find a way to reduce the width of the input window. I don't see or find any code that controls the width of the input window. The code below is taken from the Bootstrap v5 documentation, from the form element.

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

<form>
    <div class="input-group mb-3">
        <span class="input-group-text">@</span>
        <input type="email" id="email" class="form-control" placeholder="Your email" aria-label="Email" aria-describedby="basic-addon1" />
        <button type="submit" class="btn btn-primary btn-md">Sign up</button>
    </div>
</form>
like image 708
JoeG Avatar asked Feb 01 '26 02:02

JoeG


2 Answers

The input element in BootStrap v5 is a flex item. To change it's width, just set the input element's flex-grow property to 0 and the flex-basis property to the value you want for the width (e.g. 100px) like this:

flex-grow: 0;
flex-basis: 100px;

Or you can use the flex shorthand property (the second value is for the flex-shrink property) like this:

flex: 0 1 100px;

Check and run the following Code Snippet for a practical example of the above approach:

/* CSS */
.input-group>input.someInput {flex: 0 1 100px;}
<!-- HTML -->

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/><script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>

<form>
     <div class="input-group mb-3">
        <span class="input-group-text">@</span>
        <input type="email" id="email" class="form-control someInput" placeholder="Your email" aria-label="Email" aria-describedby="basic-addon1">
        <button type="submit" class="btn btn-primary btn-md">Sign up</button> 
     </div>          
 </form>
like image 149
AndrewL64 Avatar answered Feb 02 '26 19:02

AndrewL64


It's pretty simple. Add w class like the following examples:

This is the default width of the Bootstrap input element:

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
<form>
   <div class="input-group mb-3">
      <span class="input-group-text">@</span>
      <input type="email" id="email" class="form-control" placeholder="Your email" aria-label="Email" aria-describedby="basic-addon1">
      <button type="submit" class="btn btn-primary btn-md">Sign up</button> 
   </div>
</form>

You will need to reduce it by adding w-25 class like below:

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
<form>
   <div class="input-group mb-3 w-25">
      <span class="input-group-text">@</span>
      <input type="email" id="email" class="form-control" placeholder="Your email" aria-label="Email" aria-describedby="basic-addon1">
      <button type="submit" class="btn btn-primary btn-md">Sign up</button> 
   </div>
</form>

If you want to reduce it even more or make it bigger, change the w class to w-50, w-75, w-100, or w-auto.

This is all called Bootstrap Utilities Sizing.

like image 29
Kevin M. Mansour Avatar answered Feb 02 '26 19:02

Kevin M. Mansour