I am new with this, especially with Bootstrap. I have this code:
<div class="row">
<div class="col-lg-3">
<div class="input-group">
<input type="text" class="form-control">
<span class="input-group-btn">
<button class="btn btn-default" type="button">Go!</button>
</span>
</div><!-- /input-group -->
</div><!-- /.col-lg-6 -->
</div><!-- /.row -->
I need to put this input field and button in the center of the page:
This didn't work: "margin-left: auto; margin-right:auto;"
Does anyone have any ideas?
This question is related to
html
css
twitter-bootstrap
twitter-bootstrap-3
The best way for centering your element it is using .center-block
helper class. But must your bootstrap version not less than 3.1.1
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet" />_x000D_
<div class="row">_x000D_
<div class="col-lg-3 center-block">_x000D_
<div class="input-group">_x000D_
<input type="text" class="form-control">_x000D_
<span class="input-group-btn">_x000D_
<button class="btn btn-default" type="button">Go!</button>_x000D_
</span>_x000D_
</div>_x000D_
<!-- /input-group -->_x000D_
</div>_x000D_
<!-- /.col-lg-6 -->_x000D_
</div>_x000D_
<!-- /.row -->
_x000D_
Ok, this is best solution for me. Bootstrap includes mobile-first fluid grid system that appropriately scales up to 12 columns as the device or viewport size increases. So this worked perfectly on every browser and device:
<div class="row">
<div class="col-lg-4"></div>
<div class="col-lg-4">
<div class="input-group">
<input type="text" class="form-control" />
<span class="input-group-btn">
<button class="btn btn-default" type="button">Go!</button>
</span>
</div><!-- /input-group -->
</div><!-- /.col-lg-4 -->
<div class="col-lg-4"></div>
</div><!-- /.row -->
It means 4 + 4 + 4 =12... so second div will be in the middle that way.
Well in my case the following work fine
<div class="card-body p-2">
<div class="d-flex flex-row justify-content-center">
<div style="margin: auto;">
<input type="text" autocomplete="off"
style="max-width:150px!important; "
class="form-control form-control-sm font-weight-bold align-self-center w-25" id="dtTechState">
</div>
</div>
</div>
Using offsets is not desired for responsive websites.
Use a flexbox with justify content center:
<div class="row d-flex justify-content-center">
<input></input>
</div>
For me this solution worked well to center an input field in a TD:
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet" />_x000D_
<td style="background:#B3AEB5;">_x000D_
<div class="form-group text-center">_x000D_
<div class="input-group" style="margin:auto;">_x000D_
<input type="month" name="p2" value="test">_x000D_
</div>_x000D_
</div>_x000D_
</td>
_x000D_
Try to use this code:
.col-lg-3 {
width: 100%;
}
.input-group {
width: 200px; // for exemple
margin: 0 auto;
}
if it didn't work use !important
For bootstrap 4 use offset-4, see below.
<div class="mx-auto">
<form class="mx-auto" action="/campgrounds" method="POST">
<div class="form-group row">
<div class="col-sm-4 offset-4">
<input class="form-control" type="text" name="name" placeholder="name">
</div>
</div>
<div class="form-group row">
<div class="col-sm-4 offset-4">
<input class="form-control" type="text" name="picture" placeholder="image url">
</div>
</div>
<div class="form-group row">
<div class="col-sm-4 offset-4">
<input class="form-control" type="text" name="description" placeholder="description">
</div>
</div>
<button class="btn btn-primary" type="submit">Submit!</button>
<a class="btn btn-secondary" href="/campgrounds">Go Back</a>
</form>
</div>
Source: Stackoverflow.com