In bootstrap 3, why isn't button aligning to the bottom of the column as the CSS states? - css

I'm using Bootstrap 3 and I'm trying to align a button to the bottom of a column. The only way I can seem to accomplish this is by assigning an explicit height and using absolute positioning. Since I don't want all columns to behave this way, I'm wondering if there's something built into Bootstrap 3 that would allow me to do this?
.m-t {
margin-top: 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-lg-12">
<div class="panel blank-panel">
<div class="panel-body">
<form>
<div class="form-group">
<div class="col-sm-3 m-t">
<label for="fname">First Name</label>
<input type="text" id="fname" class="form-control">
</div>
<div class="col-sm-3 m-t">
<label for="lname">Last Name</label>
<input type="text" id="lname" class="form-control">
</div>
<div class="col-sm-2 m-t">
<label for="gender">Gender</label>
<select class="form-control">
<option value="" selected>Please Select</option>
<option value="1">Female</option>
<option value="2">Male</option>
<option value="3">Unspecified</option>
</select>
</div>
<div class="col-sm-2 m-t">
<label for="birthDate" class="control-label">Date of Birth</label>
<div class="input-group date" id="birthDateDiv">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
<input type="text" placeholder="mm/dd/yyyy" class="form-control" id="birthDate" name="birthDate" value="">
</div>
</div>
<div class="col-sm-2 m-t" style="display: flex; justify-content: flex-start; align-items: flex-end;">
<button class="btn btn-sm btn-primary" type="submit" id="addPerson">Add Person</button>
</div>
</div>
</form>
</div>
</div>
</div>

Basically need to add the missing label for the spacing. I've added also .form-control to the .btn as to allow for better display in mobile. Also consider hiding the dummy label on mobile using .hidden-sm or similar.
.m-t {
margin-top: 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="col-lg-12">
<div class="panel blank-panel">
<div class="panel-body">
<form>
<div class="form-group">
<div class="col-sm-3 m-t">
<label for="fname">First Name</label>
<input type="text" id="fname" class="form-control">
</div>
<div class="col-sm-3 m-t">
<label for="lname">Last Name</label>
<input type="text" id="lname" class="form-control">
</div>
<div class="col-sm-2 m-t">
<label for="gender">Gender</label>
<select class="form-control">
<option value="" selected>Please Select</option>
<option value="1">Female</option>
<option value="2">Male</option>
<option value="3">Unspecified</option>
</select>
</div>
<div class="col-sm-2 m-t">
<label for="birthDate" class="control-label">Birth</label>
<div class="input-group date" id="birthDateDiv">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
<input type="text" placeholder="mm/dd/yyyy" class="form-control" id="birthDate" name="birthDate" value="">
</div>
</div>
<div class="col-sm-2 m-t" style="">
<label for="dummy" class="control-label"> </label>
<button class="btn btn-primary form-control" type="submit" id="addPerson">Add Person</button>
</div>
</div>
</form>
</div>
</div>
</div>

If you want to align button to bottom, you should apply css settings to form-group. In other words:
<div class="form-group" style="display: flex; align-items: flex-end;">

Bootstrap 3 is deprecated.
Solution: Adapt to Bootstrap 5.
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" rel="stylesheet" />
<div class="col-lg-12">
<div class="panel blank-panel">
<div class="panel-body">
<form>
<div class="form-group d-flex flex-wrap">
<div class="col-md-3 col-8 mt-3 px-2 ms-md-auto mx-auto">
<label for="fname">First Name</label>
<input type="text" id="fname" class="form-control">
</div>
<div class="col-md-3 col-8 mt-3 px-2 ms-md-auto mx-auto">
<label for="lname">Last Name</label>
<input type="text" id="lname" class="form-control">
</div>
<div class="col-md-2 col-8 mt-3 px-2 ms-md-auto mx-auto">
<label for="gender">Gender</label>
<select class="form-select">
<option value="" selected>Please Select</option>
<option value="1">Female</option>
<option value="2">Male</option>
<option value="3">Unspecified</option>
</select>
</div>
<div class="col-md-2 col-8 mt-3 px-2 ms-md-auto mx-auto">
<label for="birthDate" class="control-label">Date of Birth</label>
<div class="input-group date" id="birthDateDiv">
<input id="dateofbirth" class="form-control" type="date" />
</div>
</div>
<div class="col-md-1 col-8 mt-3 px-2 ms-md-auto mx-auto" style="display: flex; justify-content: flex-start; align-items: flex-end;">
<button class="btn btn-sm btn-primary" type="submit" id="addPerson">Add Person</button>
</div>
</div>
</form>
</div>
</div>
</div>

Related

How to make one column have position fixed on horizontal scroll only?

I have one inline form that spans over horizontally and vertically. In this form, I have one
last column that is an add button that I want it to have a fixed position but only when I scroll the form horizontally, not vertically.
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<title>Hello, world!</title>
<style>
.btn-control {
position: fixed;
right: 0;
}
</style>
</head>
<body>
<h1>Inline form</h1>
<div class="container" style="width: 55%; height: 500px;">
<div class="row" style="flex-wrap: nowrap; overflow-x: scroll;">
<form style=" ">
<div class="form-row flex-nowrap">
<div class="col-3">
<label for="">First Name</label>
<input type="text" class="form-control" placeholder="First name">
</div>
<div class="col-3">
<label for="">Last Name</label>
<input type="text" class="form-control" placeholder="Last name">
</div>
<div class="col-3">
<label for="">Gender</label>
<input type="text" class="form-control" placeholder="Gender">
</div>
<div class="col-3">
<label for="">Age</label>
<input type="text" class="form-control" placeholder="Age">
</div>
<div class="col-3">
<label for="">Phone Numher</label>
<input type="text" class="form-control" placeholder="Phone Numher">
</div>
<div class="col-3 btn-control">
<p></p>
<button class="btn btn-info btn-circle">+</button>
</div>
</div>
<div class="form-row flex-nowrap">
<div class="col-3">
<label for="">First Name</label>
<input type="text" class="form-control" placeholder="First name">
</div>
<div class="col-3">
<label for="">Last Name</label>
<input type="text" class="form-control" placeholder="Last name">
</div>
<div class="col-3">
<label for="">Gender</label>
<input type="text" class="form-control" placeholder="Gender">
</div>
<div class="col-3">
<label for="">Age</label>
<input type="text" class="form-control" placeholder="Age">
</div>
<div class="col-3">
<label for="">Phone Numher</label>
<input type="text" class="form-control" placeholder="Phone Numher">
</div>
<div class="col-3 btn-control">
<p></p>
<button class="btn btn-info btn-circle">+</button>
</div>
</div>
<div class="form-row flex-nowrap">
<div class="col-3">
<label for="">First Name</label>
<input type="text" class="form-control" placeholder="First name">
</div>
<div class="col-3">
<label for="">Last Name</label>
<input type="text" class="form-control" placeholder="Last name">
</div>
<div class="col-3">
<label for="">Gender</label>
<input type="text" class="form-control" placeholder="Gender">
</div>
<div class="col-3">
<label for="">Age</label>
<input type="text" class="form-control" placeholder="Age">
</div>
<div class="col-3">
<label for="">Phone Numher</label>
<input type="text" class="form-control" placeholder="Phone Numher">
</div>
<div class="col-3 btn-control">
<p></p>
<button class="btn btn-info btn-circle">+</button>
</div>
</div>
</form>
</div>
</div>
<!-- Optional JavaScript; choose one of the two! -->
<!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
</body>
</html>
I have set position: fixed but does not solve the problem as currently, the button has fixed position on both horizontal and vertical. I found some resources, but it does not fit my need.
Thanks.
position sticky should work
.btn-control {
position: sticky;
right: 0;
}
if it work you can change right position

Vertical align not working for custom control [duplicate]

This question already has answers here:
Vertical Align Center in Bootstrap 4 [duplicate]
(20 answers)
Closed 2 years ago.
I'm trying to build a simple filter-bar which has a custom-control element.
The element should be aligned vertically but it seems not to be working. This is the markup for the filter-bar:
<div class="bg-light p-3 card">
<form class="form-inline">
<div class="form-row">
<div class="col-auto">
<input type="text" placeholder="The team's name" class="form-control" value="" />
</div>
<div class="col-auto">
<select class="form-control">
<option value="">Select the season</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-secondary">
<i class="fas fa-search"/> Search</button>
</div>
<div class="col-auto">
<div class="custom-control custom-switch align-center">
<input type="checkbox" class="custom-control-input" id="vpx" checked="" />
<label class="custom-control-label" for="vpx">Hide 0-point teams</label>
</div>
</div>
</div>
</form>
</div>
But the result is shown below:
No matter which align- class I add, the element won't move a bit. Already tried to set the display property to inline-block as stated in the bootstap-docs.
Only way to achieve something like centered is to add mt-2 class but this seems not to be the correct way this should be done.
Add align-items-center to form-row and also close the <i> tag correctly on Search button
body{ min-width: 800px;}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="bg-light p-3 card">
<form class="form-inline">
<div class="form-row align-items-center">
<div class="col-auto">
<input type="text" placeholder="The team's name" class="form-control" value="" />
</div>
<div class="col-auto">
<select class="form-control">
<option value="">Select the season</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-secondary">
<i class="fas fa-search"> Search</i></button>
</div>
<div class="col-auto">
<div class="custom-control custom-switch align-center">
<input type="checkbox" class="custom-control-input" id="vpx" checked="" />
<label class="custom-control-label" for="vpx">Hide 0-point teams</label>
</div>
</div>
</div>
</form>
</div>

How to make labels and inputs on the same row?

I use Bootstrap 3 in my project.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="row well well-sm">
<div class="col-xs-2">
<label for="txtMonth"> month:</label>
<input type="number" class="form-control input-sm" id="txtMonth" ng-model="month" />
</div>
<div class="col-xs-3">
<label for="txtMonth"> year:</label>
<input type="number" class="form-control input-sm" id="txtYear" ng-model="year" />
</div>
<div class="col-xs-2">
<input type="button" class="btn btn-default btn-sm" id="btnGetRecords" ng-click="getRecords()" value="Enter" />
</div>
</div>
As you can see each input number has label, but the label and input not on the same row how to make label and input show on a single row?
use col-xs-4[label] in your label and create new div for and col-xs-8 [input div]
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row well well-sm">
<div class="col-xs-5">
<label class="col-xs-4" for="txtMonth"> month:</label>
<div class="col-xs-8">
<input type="number" class="form-control input-sm" id="txtMonth" ng-model="month" />
</div>
</div>
<div class="col-xs-5">
<label class="col-xs-4" for="txtMonth"> year:</label>
<div class="col-xs-8">
<input type="number" class="form-control input-sm" id="txtYear" ng-model="year" />
</div>
</div>
<div class="col-xs-2">
<input type="button" class="btn btn-default btn-sm" id="btnGetRecords" ng-click="getRecords()" value="Enter" />
</div>
</div>
Just use the form-inline to align your label and input inline as below.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<form class="form-inline">
<div class="well well-sm">
<div class="form-group">
<label for="txtMonth"> month:</label>
<input type="number" class="form-control input-sm" id="txtMonth" ng-model="month" />
</div>
<div class="form-group">
<label for="txtMonth"> year:</label>
<input type="number" class="form-control input-sm" id="txtYear" ng-model="year" />
</div>
<div class="form-group">
<input type="button" class="btn btn-default btn-sm" id="btnGetRecords" ng-click="getRecords()" value="Enter" />
</div>
</div>
</form>
You need to wrap you Code into a div with CSS class of form-group like this:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="row well well-sm">
<div class="form-group">
<label class="col-xs-2" for="txtMonth"> month:</label>
<div class="col-xs-2">
<input type="number" class="form-control input-sm" id="txtMonth" ng-model="month"/>
</div>
<label class="col-xs-2" for="txtMonth"> year:</label>
<div class="col-xs-2">
<input type="number" class="form-control input-sm" id="txtYear" ng-model="year"/>
</div>
<input type="button" class="btn btn-default btn-sm" id="btnGetRecords" ng-click="getRecords()" value="Enter"/>
</div>
</div>
It is because of the form-control class having width:100% and display:block properties. If you overwrite this one that will work automatically.
Also Increase the xs values for fixing the label and text boxes in a same row.
Apply some new class and override the above properties like below.
.newclass {
display:inline-block;
width:auto;
}
<div class="row well well-sm">
<div class="col-xs-5">
<label for="txtMonth"> month:</label>
<input type="number" class="form-control newclass input-sm" id="txtMonth" ng-model="month" />
</div>
<div class="col-xs-5">
<label for="txtMonth"> year:</label>
<input type="number" class="form-control newclass input-sm" id="txtYear" ng-model="year" />
</div>
<div class="col-xs-2">
<input type="button" class="btn btn-default btn-sm" id="btnGetRecords" ng-click="getRecords()" value="Enter" />
</div>
</div>
DEMO
Hope it will work
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="col-xs-12">
<label for="txtMonth" class="col-xs-2"> month:</label>
<label for="txtMonth" class="col-xs-3"> year:</label>
<div class="col-xs-7">
</div>
</div>
<div class="col-xs-12">
<div class="col-xs-2">
<input type="number" class="form-control input-sm" id="txtMonth" ng-model="month" />
</div>
<div class="col-xs-3">
<input type="number" class="form-control input-sm" id="txtYear" ng-model="year" />
</div>
<div class="col-xs-2">
<input type="button" class="btn btn-default btn-sm" id="btnGetRecords" ng-click="getRecords()" value="Enter" />
</div>
<div class="col-xs-5">
</div>
</div>

Prevent Bootstrap columns overlapping

Could you lease tell me how to prevent the brands select box from being hidden behind the date. I gave minimum width to dates because I do not want them to collapse more than the minimum width.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<div class="col-md-12 row widget-simple remove-padding-top margin-bottom-10">
<form accept-charset="UTF-8" method="post">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓">
</div>
<div class="row">
<div class="col-md-3 col-sm-4 col-xs-12">
<div class="form-group credits-daterange">
<label class="col-sm-2 col-xs-12 control-label vertical-center" for="start_date">Date</label>
<div class="col-sm-10 col-xs-12">
<div class="input-group input-daterange" data-date-format="yyyy-mm-dd" style="min-width: 236px;">
<input class="form-control text-center" id="start_date" name="start_date" placeholder="From" required="required" type="text" value="2013-08-01"> <span class="input-group-addon"><i class="fa fa-angle-right"></i></span>
<input class="form-control text-center" id="end_date" name="end_date" placeholder="To" required="required" type="text" value="2015-08-03">
</div>
</div>
</div>
</div>
<div class="col-md-3 col-sm-3 col-xs-12">
<div class="form-group">
<select class="form-control" id="brand" name="brand">
<option value="0">All Brands</option>
<option value="9618">Brand 1</option>
<option value="10268">Brand 2</option>
<option value="10232">Brand 3</option>
</select>
</div>
</div>
<div class="col-md-2 col-sm-3 col-xs-12">
<div class="form-group">
<select class="form-control" id="status" name="status">
<option value="Created">status 1</option>
<option value="Downloaded">Status 2</option>
</select>
</div>
</div>
<div class="col-md-2 col-sm-12 pull-right">
<input class="hidden" id="search_txt" name="search_txt" type="text" value="">
<input class="btn btn-primary" id="agencies-export-excel" name="commit" type="submit" value="Export to Excel">
</div>
</div>
</form>
</div>
</body>
</html>
Demo
All suggestions are welcome.
Thanks
The overlap is due to style="min-width: 236px;
If you remove it, you can this that the issue is gone away.
Here is a fiddle : http://www.bootply.com/KFgIJ6z5ld
Your code without your custom style :
<div class="col-md-12 row widget-simple remove-padding-top margin-bottom-10">
<form accept-charset="UTF-8" method="post">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓">
</div>
<div class="row">
<div class="col-md-3 col-sm-4 col-xs-12">
<div class="form-group credits-daterange">
<label class="col-sm-2 col-xs-12 control-label vertical-center" for="start_date">Date</label>
<div class="col-sm-10 col-xs-12">
<div class="input-group input-daterange" data-date-format="yyyy-mm-dd">
<input class="form-control text-center" id="start_date" name="start_date" placeholder="From" required="required" type="text" value="2013-08-01"> <span class="input-group-addon"><i class="fa fa-angle-right"></i></span>
<input class="form-control text-center" id="end_date" name="end_date" placeholder="To" required="required" type="text" value="2015-08-03">
</div>
</div>
</div>
</div>
<div class="col-md-3 col-sm-3 col-xs-12">
<div class="form-group">
<select class="form-control" id="brand" name="brand">
<option value="0">All Brands</option>
<option value="9618">Brand 1</option>
<option value="10268">Brand 2</option>
<option value="10232">Brand 3</option>
</select>
</div>
</div>
<div class="col-md-2 col-sm-3 col-xs-12">
<div class="form-group">
<select class="form-control" id="status" name="status">
<option value="Created">status 1</option>
<option value="Downloaded">Status 2</option>
</select>
</div>
</div>
<div class="col-md-2 col-sm-12 pull-right">
<input class="hidden" id="search_txt" name="search_txt" type="text" value="">
<input class="btn btn-primary" id="agencies-export-excel" name="commit" type="submit" value="Export to Excel">
</div>
</div>
</form>
</div>
One other way is to make bigger your cells :
Bootply : http://www.bootply.com/KzRD6fb0Pw
Code :
<div class="col-md-12 row widget-simple remove-padding-top margin-bottom-10">
<form accept-charset="UTF-8" method="post">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓">
</div>
<div class="row">
<div class="col-md-3 col-sm-6 col-xs-12">
<div class="form-group credits-daterange">
<label class="col-sm-2 col-xs-12 control-label vertical-center" for="start_date">Date</label>
<div class="col-sm-10 col-xs-12">
<div class="input-group input-daterange" data-date-format="yyyy-mm-dd">
<input class="form-control text-center" id="start_date" name="start_date" placeholder="From" required="required" type="text" value="2013-08-01"> <span class="input-group-addon"><i class="fa fa-angle-right"></i></span>
<input class="form-control text-center" id="end_date" name="end_date" placeholder="To" required="required" type="text" value="2015-08-03">
</div>
</div>
</div>
</div>
<div class="col-md-3 col-sm-2 col-xs-12">
<div class="form-group">
<select class="form-control" id="brand" name="brand">
<option value="0">All Brands</option>
<option value="9618">Brand 1</option>
<option value="10268">Brand 2</option>
<option value="10232">Brand 3</option>
</select>
</div>
</div>
<div class="col-md-3 col-sm-2 col-xs-12">
<div class="form-group">
<select class="form-control" id="status" name="status">
<option value="Created">status 1</option>
<option value="Downloaded">Status 2</option>
</select>
</div>
</div>
<div class="col-md-3 col-sm-2 pull-right">
<input class="hidden" id="search_txt" name="search_txt" type="text" value="">
<input class="btn btn-primary" id="agencies-export-excel" name="commit" type="submit" value="Export to Excel">
</div>
</div>
</form>
</div>

Twitter Bootstrap - form in Modal formatting - ASP.NET

I have a form in a modal that formats perfectly if the main window is not too wide (with the labels above the fields). However, if the browser is maximised or large enough it alters the layout of the contents inside the modal so that the labels are now to the left of the fields (which means one word per line and looks very messy).
Any idea how I have overcome this behavior so that the class form-horizontal is working to the width of the modal, not the browser window?
Update with code:
Here is the structure of the modal
<div id="CCNewModal" class="modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button class="close" aria-hidden="true" type="button" data-dismiss="modal">×</button>
<h4 id="CCTitle" runat="server" class="modal-title">New Credit Card</h4>
</div>
<div class="modal-body">
<div>
<fieldset>
<div id="selectCardDiv" class="form-group">
<label id="selectCardLabel" class="col-lg-2 control-label" for="selectCard">Card Type</label>
<div class="col-lg-10">
<select class="form-control" id="selectCard" runat="server">
<option value="0">Select Card</option>
<option value="Amex">Amex</option>
<option value="MasterCard">MasterCard</option>
<option value="Visa">Visa</option>
</select>
</div>
</div>
<div id="selectNameDiv" class="form-group">
<label id="selectNameLabel" class="col-lg-2 control-label" for="inputCCName">Name</label>
<div class="col-lg-10">
<input class="form-control" id="inputCCName" runat="server" type="text" placeholder="Name (as on statement)">
</div>
</div>
<div id="selectNumberDiv" class="form-group">
<label id="selectNumberLabel" class="col-lg-2 control-label" for="inputCCNumber">Number</label>
<div class="col-lg-10">
<input class="form-control" id="inputCCNumber" runat="server" type="text" placeholder="Credit Card Number (no spaces)">
</div>
</div>
<div id="selectMonthDiv" class="form-group">
<label id="selectMonthLabel" class="col-lg-2 control-label" for="selectMonth">Expiry Month</label>
<div class="col-lg-10">
<select class="form-control" id="selectMonth" runat="server">
<option value="0">Month</option>
<option value="01">Jan</option>
<option value="02">Feb</option>
<option value="03">Mar</option>
<option value="04">Apr</option>
<option value="05">May</option>
<option value="06">Jun</option>
<option value="07">Jul</option>
<option value="08">Aug</option>
<option value="09">Sep</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>
</div>
</div>
<div id="selectYearDiv" class="form-group">
<label id="selectYearLabel" class="col-lg-2 control-label" for="selectYear">Expiry Year</label>
<div class="col-lg-10">
<select class="form-control" id="selectYear" runat="server"></select>
</div>
</div>
<div id="selectCVVDiv" class="form-group">
<label id="selectCVVLabel" class="col-lg-2 control-label" for="inputCVV">CVV</label>
<div class="col-lg-10">
<input class="form-control" id="inputCVV" runat="server" type="text" placeholder="Security Code (CVV)">
</div>
</div>
<div id="selectBillingAddressDiv" class="form-group">
<label id="selectBillingAddressLabel" class="col-lg-2 control-label" for="inputBillingAddress">Billing Address</label>
<div class="col-lg-10">
<textarea class="form-control" id="inputBillingAddress" runat="server" rows="3" placeholder="Address as on Statement"></textarea>
</div>
</div>
<div id="selectBillingCityDiv" class="form-group">
<label id="selectBillingCityLabel" class="col-lg-2 control-label" for="inputCCCity">City</label>
<div class="col-lg-10">
<input class="form-control" id="inputCCCity" runat="server" type="text" placeholder="City">
</div>
</div>
<div id="selectBillingStateDiv" class="form-group">
<label id="selectBillingStateLabel" class="col-lg-2 control-label" for="selectState">State</label>
<div class="col-lg-10">
<select class="form-control" id="selectState" runat="server">
<option value="0">Select State</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
</div>
</div>
<div id="selectBillingZipDiv" class="form-group">
<label id="selectBillingZipLabel" class="col-lg-2 control-label" for="inputCCZip">Zip / Postcode</label>
<div class="col-lg-10">
<input class="form-control" id="inputCCZip" runat="server" type="text" placeholder="City">
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label" for="inputCCCountry">Country</label>
<div class="col-lg-10">
<input disabled="" value="United States" class="form-control" id="inputCCCountry" runat="server" type="text" placeholder="City">
</div>
</div>
<div id="selectTelephoneDiv" class="form-group">
<label id="selectTelephoneLabel" class="col-lg-2 control-label" for="inputCCTelephone">Telephone</label>
<div class="col-lg-10">
<input class="form-control" id="inputCCTelephone" runat="server" type="text" placeholder="Telephone (format as 123-456-7890)">
</div>
</div>
<div class="checkbox">
<label>
<input id="selectPrimaryCB" runat="server" type="checkbox" checked>Primary (default) card</label>
</div>
</fieldset>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-default" type="button" data-dismiss="modal">Close</button>
<button id="CreditCardSaveButton" runat="server" class="btn btn-primary" type="button">Save</button>
</div>
</div>
</div>
</div>
and the following CCS override
#CCNewModal .modal-body {
max-height: 420px;
overflow-y: auto;
}
Master page is basically - the page I am working on fits into the MainContent placeholder
<form runat="server" class="form-horizontal">
<div class="container body-content">
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
<hr />
<footer>
<asp:Label ID="FooterLabel" runat="server" Font-Size="Small"></asp:Label>
</footer>
</div>
</form>
This is what the form looks like using form-vertical
..and it really does a number with the modal formatting
.. using form-horizontal (wide and narrow)
Can I override this behavior so that the class form-horizontal is working to the width of the modal, not the browser window?
In short, No. The styles for form-horizontal are based on media queries run against the size of the window. Media queries cannot be run against the size of other elements. Of course, you can use javascript to figure out the size of other elements and change other elements accordingly, but you'd lose all the native performance benefits of using plain old CSS and letting the browser do the work.
If the browser is maximised or large enough it alters the layout of the contents inside the modal so that the labels are now to the left of the fields
But that's exactly what horizontal forms do. If you don't want that kind of functionality, why don't you change it to a basic form? It sounds like, even if this wasn't space constrained by the modal window, this form would run into trouble if you have long label names and not enough column width to display them on a single line.
Here's an example of a Horizontal Form in a Modal
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModalHorizontal">
Launch Horizontal Form
</button>
<!-- Modal -->
<div class="modal fade" id="myModalHorizontal" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<button type="button" class="close"
data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">
Modal title
</h4>
</div>
<!-- Modal Body -->
<div class="modal-body">
<form class="form-horizontal" role="form">
<div class="form-group">
<label class="col-sm-2 control-label"
for="inputEmail3">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control"
id="inputEmail3" placeholder="Email"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label"
for="inputPassword3" >Password</label>
<div class="col-sm-10">
<input type="password" class="form-control"
id="inputPassword3" placeholder="Password"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox"/> Remember me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Sign in</button>
</div>
</div>
</form>
</div>
<!-- Modal Footer -->
<div class="modal-footer">
<button type="button" class="btn btn-default"
data-dismiss="modal">
Close
</button>
<button type="button" class="btn btn-primary">
Save changes
</button>
</div>
</div>
</div>
</div>
Here's an example of a basic form that maintains it's structure at all screen sizes:
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModalNorm">
Launch Normal Form
</button>
<!-- Modal -->
<div class="modal fade" id="myModalNorm" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<button type="button" class="close"
data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">
Modal title
</h4>
</div>
<!-- Modal Body -->
<div class="modal-body">
<form role="form">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control"
id="exampleInputEmail1" placeholder="Enter email"/>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control"
id="exampleInputPassword1" placeholder="Password"/>
</div>
<div class="checkbox">
<label>
<input type="checkbox"/> Check me out
</label>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
<!-- Modal Footer -->
<div class="modal-footer">
<button type="button" class="btn btn-default"
data-dismiss="modal">
Close
</button>
<button type="button" class="btn btn-primary">
Save changes
</button>
</div>
</div>
</div>
</div>
Alternatively, you could try to override some of the styles that .form-horizontal applies if you don't have much control over the markup. You could add some CSS like this (depending on the structure of your horizontal form)
.modal-body .form-horizontal .col-sm-2,
.modal-body .form-horizontal .col-sm-10 {
width: 100%
}
.modal-body .form-horizontal .control-label {
text-align: left;
}
.modal-body .form-horizontal .col-sm-offset-2 {
margin-left: 15px;
}
Horizontal Form with CSS
.modal-body .form-horizontal .col-sm-2,
.modal-body .form-horizontal .col-sm-10 {
width: 100%
}
.modal-body .form-horizontal .control-label {
text-align: left;
}
.modal-body .form-horizontal .col-sm-offset-2 {
margin-left: 15px;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModalHorizontal">
Launch Horizontal Form
</button>
<!-- Modal -->
<div class="modal fade" id="myModalHorizontal" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<button type="button" class="close"
data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">
Modal title
</h4>
</div>
<!-- Modal Body -->
<div class="modal-body">
<form class="form-horizontal" role="form">
<div class="form-group">
<label class="col-sm-2 control-label"
for="inputEmail3">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control"
id="inputEmail3" placeholder="Email"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label"
for="inputPassword3" >Password</label>
<div class="col-sm-10">
<input type="password" class="form-control"
id="inputPassword3" placeholder="Password"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox"/> Remember me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Sign in</button>
</div>
</div>
</form>
</div>
<!-- Modal Footer -->
<div class="modal-footer">
<button type="button" class="btn btn-default"
data-dismiss="modal">
Close
</button>
<button type="button" class="btn btn-primary">
Save changes
</button>
</div>
</div>
</div>
</div>
Here's a Demo in jsFiddle

Resources