Got a bit of a problem with my form layout if anyone can spare a bit of help: My understanding was that if I had a fixed width container, using fr instead of a percentage width for grid-template-columns would take into account only the free space available, and so grid-gap would not cause the grid to overflow its container, as described in this answer:
As a solution, instead of percentage units, try using fr units, which apply only to free space. This means that fr lengths are calculated after any grid-gap lengths are applied.
and also here:
The fr unit works only with the free space in the container.
But my code still seems to cause the grid to overflow (horizontally is all I care about):
https://codepen.io/nwoodward/pen/bGLpBbP
* {
margin: 0;
}
.container {
display: flex;
justify-content: center;
width: 100%;
height: 100vh;
background-color: firebrick;
}
.form {
display: flex;
flex-direction: column;
padding: 2rem;
margin: 2rem;
width: 25rem;
background-color: white;
}
.content {
border: 1px solid red;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 4rem;
width: 100%;
}
<div class="container">
<form class="form">
<div class="content">
<div class="field">
<label for="title" class="label">Title</label>
<input type="text" placeholder="Job Title" id="title" class="input">
<i class="icon icon--success"></i>
<i class="icon icon--fail">
</i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="company" class="label">Company</label>
<select name="company" id="company" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="location" class="label">Location</label>
<select name="location" id="location" class="input">
<!-- options added in js -->
</select>
<i class="icon"></i>
<i class="icon"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="wage" class="label">Wage</label>
<input type="text" placeholder="Wage" id="wage" class="input">
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="type" class="new-job__label">Type</label>
<select name="type" id="type" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="position" class="label">Position</label>
<select name="position" id="position" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="pqe" class="label">PQE</label>
<select name="pqe" id="pqe" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="featured" class="label">Featured</label>
<select name="featured" id="featured" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
</div>
<button class="new-job__submit">Submit</button>
</form>
</div>
I guess that part of the issue is that the input elements have a fixed minimum width of some sort, but even if that is true, fixing the width of either the .field or the inputs to a smaller value that does fit isn't really what I'm after at all.
*EDIT: Overflowing with inputs
**EDIT: Not overflowing with divs
Update the code like below (related question to understand the first code adjustment: Why does minmax(0, 1fr) work for long elements while 1fr doesn't?)
* {
margin: 0;
}
.container {
display: flex;
justify-content: center;
background-color: firebrick;
}
.form {
display: flex;
flex-direction: column;
padding: 2rem;
margin: 2rem;
width: 25rem;
background-color: white;
}
.content {
border: 1px solid red;
display: grid;
grid-template-columns: repeat(2, minmax(0,1fr)); /* here */
grid-gap: 4rem;
}
/* here */
input {
max-width: 100%;
box-sizing: border-box;
}
<div class="container">
<form class="form">
<div class="content">
<div class="field">
<label for="title" class="label">Title</label>
<input type="text" placeholder="Job Title" id="title" class="input">
<i class="icon icon--success"></i>
<i class="icon icon--fail">
</i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="company" class="label">Company</label>
<select name="company" id="company" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="location" class="label">Location</label>
<select name="location" id="location" class="input">
<!-- options added in js -->
</select>
<i class="icon"></i>
<i class="icon"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="wage" class="label">Wage</label>
<input type="text" placeholder="Wage" id="wage" class="input">
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="type" class="new-job__label">Type</label>
<select name="type" id="type" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="position" class="label">Position</label>
<select name="position" id="position" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="pqe" class="label">PQE</label>
<select name="pqe" id="pqe" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="featured" class="label">Featured</label>
<select name="featured" id="featured" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
</div>
<button class="new-job__submit">Submit</button>
</form>
</div>
The issue is caused because you gave a parent a fixed height:
.container { height: 100vh; }
To solve it just change the property from height to min-height. The container will grow then instead of letting the cotent overflow:
* {
margin: 0;
}
.container {
display: flex;
justify-content: center;
width: 100%;
min-height: 100vh;
background-color: firebrick;
}
.form {
display: flex;
flex-direction: column;
padding: 2rem;
margin: 2rem;
width: 25rem;
background-color: white;
}
.content {
border: 1px solid red;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 4rem;
width: 100%;
}
<div class="container">
<form class="form">
<div class="content">
<div class="field">
<label for="title" class="label">Title</label>
<input type="text" placeholder="Job Title" id="title" class="input">
<i class="icon icon--success"></i>
<i class="icon icon--fail">
</i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="company" class="label">Company</label>
<select name="company" id="company" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="location" class="label">Location</label>
<select name="location" id="location" class="input">
<!-- options added in js -->
</select>
<i class="icon"></i>
<i class="icon"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="wage" class="label">Wage</label>
<input type="text" placeholder="Wage" id="wage" class="input">
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="type" class="new-job__label">Type</label>
<select name="type" id="type" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="position" class="label">Position</label>
<select name="position" id="position" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="pqe" class="label">PQE</label>
<select name="pqe" id="pqe" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
<div class="field">
<label for="featured" class="label">Featured</label>
<select name="featured" id="featured" class="input">
<!-- options added in js -->
</select>
<i class="icon icon--success"></i>
<i class="icon icon--fail"></i>
<small class="error-msg"></small>
</div>
</div>
<button class="new-job__submit">Submit</button>
</form>
</div>
Related
I have a row and 2 columns in there which I am trying to make the same height as there are border lines top and bottom that should be alined, also box shadow.
I've tried tons of recommended solutions recommended here excluding the ones involving javascript, How can I make Bootstrap columns all the same height? and none worked. E.g. adding this CSS to the row class didn't work:
.equal {
display: flex;
display: -webkit-flex;
flex-wrap: wrap;
}
My CSS:
.contact .info {
padding: 30px;
background: #fff;
box-shadow: 0 0 24px 0 rgba(0, 0, 0, 0.1);
margin-bottom: 40px;
}
.contact .php-email-form {
padding: 30px;
background: #fff;
box-shadow: 0 0 24px 0 rgba(0, 0, 0, 0.1);
margin-bottom: 40px;
}
<section id="contact" class="contact">
<div class="container">
<div class="section-title">
<h2>CONTACT</h2>
</div>
<div class="row">
<div class="col-md-5">
<div class="info">
<div class="email">
<i class="bi bi-envelope"></i>
<h4>Email:</h4>
<p>email</p>
</div>
<div class="phone">
<i class="bi bi-phone"></i>
<h4>Telephone:</h4>
<p>95 966 0</p>
</div>
<div class="address">
<i class="bi bi-geo-alt"></i>
<h4>Messenger:</h4>
<p>address</p>
</div>
<div class="phone">
<i class="bi bi-phone"></i>
<h4>Whatsapp:</h4>
<p>745 2720</p>
</div>
<div class="phone">
<i class="bi bi-phone"></i>
<h4>Telegram:</h4>
<p>745 2720</p>
</div>
</div>
</div>
<div class="col-md-7">
<form action="forms/contact.php" method="post" role="form" class="php-email-form">
<div class="row">
<div class="form-group col-md-6">
<label for="name">N</label>
<input type="text" name="name" class="form-control" id="name" required>
</div>
<div class="form-group col-md-6">
<label for="name">Email</label>
<input type="email" class="form-control" name="email" id="email" required>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label for="name">Mobile</label>
<input type="text" class="form-control" name="subject" id="subject" required>
</div>
<div class="form-group col-md-6">
<label for="name">F</label>
<input type="text" class="form-control" name="subject" id="subject" required>
</div>
</div>
<div class="form-group">
<label for="name">T</label>
<input type="text" class="form-control" name="subject" id="subject" required>
</div>
<div class="form-group">
<label for="name">U</label>
<textarea class="form-control" name="message" rows="6" required></textarea>
</div>
<div class="err">
<div class="processing">Loading</div>
<div class="error-message"></div>
<div class="sent-message">Your message has been sent.!</div>
</div>
<div class="text-center"><button type="submit">Send</button></div>
</form>
</div>
</div>
</div>
</section>
You have to be very specific with your css.
I added a border to your cols so you have a visual of the col height.
BS3 was not designed to work with flex, but that doesn't mean it can't.
Update:
Created 2 css classes .d-flex, and .d-flex-item
In CSS flex model display:flex items make their children flex items which by default stretch to fit their container's height.
This goes only one level deep so to make this work with your layout:
div.row is a flex container
div.col-md-5:
is a flex item of its parent: div.row
is a flex container to its child div.info
div.info is a flex item of its parent: div.col-md-5
div.col-md-7 is a flex item of its parent: div.row
All of this is to allow the flex items to stretch to their parent containers. Setting a CSS height on any flex-item will override the default stretch for that item.
/* No changes here to your css */
.contact .info {
padding: 30px;
background: #fff;
box-shadow: 0 0 24px 0 rgba(0, 0, 0, 0.1);
margin-bottom: 40px;
}
.contact .php-email-form {
padding: 30px;
background: #fff;
box-shadow: 0 0 24px 0 rgba(0, 0, 0, 0.1);
margin-bottom: 40px;
}
/* flex css, see your HTML class attribute */
.d-flex {
display: flex;
}
.d-flex-item {
flex: 1 0 auto;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons#1.9.1/font/bootstrap-icons.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#3.4.1/dist/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/jquery#3.5.1/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#3.4.1/dist/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
<section id="contact" class="contact">
<div class="container">
<div class="section-title">
<h2>CONTACT</h2>
</div>
<div class="row d-flex">
<div class="col-md-5 d-flex-item d-flex">
<div class="info d-flex-item">
<div class="email">
<i class="bi bi-envelope"></i>
<h4>Email:</h4>
<p>email</p>
</div>
<div class="phone">
<i class="bi bi-phone"></i>
<h4>Telephone:</h4>
<p>95 966 0</p>
</div>
<div class="address">
<i class="bi bi-geo-alt"></i>
<h4>Messenger:</h4>
<p>address</p>
</div>
<div class="phone">
<i class="bi bi-phone"></i>
<h4>Whatsapp:</h4>
<p>745 2720</p>
</div>
<div class="phone">
<i class="bi bi-phone"></i>
<h4>Telegram:</h4>
<p>745 2720</p>
</div>
</div>
</div>
<div class="col-md-7 d-flex-item">
<form action="forms/contact.php" method="post" role="form" class="php-email-form">
<div class="row">
<div class="form-group col-md-6">
<label for="name">N</label>
<input type="text" name="name" class="form-control" id="name" required>
</div>
<div class="form-group col-md-6">
<label for="name">Email</label>
<input type="email" class="form-control" name="email" id="email" required>
</div>
<div class="form-group col-md-6">
<label for="name">Mobile</label>
<input type="text" class="form-control" name="subject" id="subject" required>
</div>
<div class="form-group col-md-6">
<label for="name">F</label>
<input type="text" class="form-control" name="subject" id="subject" required>
</div>
<div class="form-group col-md-6">
<label for="name">T</label>
<input type="text" class="form-control" name="subject" id="subject" required>
</div>
<div class="form-group col-md-6">
<label for="name">U</label>
<textarea class="form-control" name="message" rows="6" required></textarea>
</div>
<div class="err">
<div class="processing">Loading</div>
<div class="error-message"></div>
<div class="sent-message">Your message has been sent.!</div>
<div class="text-center"><button type="submit">Send</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
<div class="card">
<div class="col-md-12">
<span style="margin-left:20px;">
<input rows="4" cols="50" type="text" name="box" id="box" style="width:50px;float:left;"></span>
</span>
<i class="fa fa-long-arrow-left" style="font-size:36px;vertical-align:-19%;color:green"></i>
<span>
<input type="text" name="boxx" id="boxx" style="width:200px;">
</span>
<i class="fa fa-long-arrow-right" style="font-size:48px;color:green"></i>
<span style="margin-right:20px;">
<input rows="4" cols="50" type="text" name="box" id="box" style="width:50px;float:right;">
</span>
</div>
</div>
i want to align the middle textbox in the center of the div and the icon should be close to the textbox.
You set the font size of the second < i > to 48 and caused this error. And I changed the bootstrap classes to a row with 3 cols 3-6-3. make sure that your bootstrap references were referenced well :
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class ="row">
<div class="col-sm-3">
<span>
<input rows="4" cols="50" type="text" name="box" id="box" style="width:50px;float:left;"></span>
</span>
<i class="fa fa-long-arrow-left" style="font-size:36px;color:green"></i>
</div>
<div class="col-sm-6">
<span>
<input type="text" name="boxx" id="boxx" style="width:200px;">
</span>
<i class="fa fa-long-arrow-right" style="color:green"></i>
</div>
<div class="col-sm-3">
<span>
<input rows="4" cols="50" type="text" name="box" id="box" style="width:50px;float:right;">
</span>
</div>
</div>
You can use display: flex to center everything :
.col-md-12 {
display: flex;
align-items: center;
}
#input-left {
width:50px;
}
#input-center {
width:200px;
}
#input-right {
width:50px;
}
.middle-box {
margin-left: auto;
margin-right: auto;
display: flex;
align-items: center;
}
.fa-long-arrow-left {
font-size:36px !important;
/*vertical-align:-19%;*/
color:green;
}
.fa-long-arrow-right {
font-size:48px !important;
color:green;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="card">
<div class="col-md-12">
<span style="margin-left:20px;">
<input rows="4" cols="50" type="text" name="box" id="input-left"> </span>
<span class="middle-box">
<i class="fa fa-long-arrow-left"></i>
<span>
<input type="text" name="boxx" id="input-center">
</span>
<i class="fa fa-long-arrow-right"></i>
</span>
<span style="margin-right:20px;">
<input rows="4" cols="50" type="text" name="box" id="input-right">
</span>
</div>
</div>
I have found a widget on ServiceNow share that allows you to add a table set of variables and then dynamically get the user to add multiple rows. We would like to use this, however our requirements are to have around 25 variables as columns in the table.
On this widget once you get past 10 columns, it starts to truncate the width of the columns, and this continues until they are too small to be useable. Once you get past 20 columns it starts to add further columns outside of the window. I was wondering how we could amend this widget to stop the columns width truncating and then once you reach the limit of the container for a horizontal scroll bar to appear. Below is the HTML of the Widget:
<fieldset class="form-group form-group-has-focus" ng-class="{'edit_mode':edit_mode}">
<label ng-if="data.title != ''" class="field-label" style="display:block;">
<span class="field-decorations">
<span ng-show="data.is_mandatory" style="padding-right: .25em" title="${Mandatory}" class="fa fa-asterisk mandatory" ng-class="{'mandatory-filled': mandatory_filled()}" aria-hidden="false"></span>
</span>
{{data.title}}
<a ng-if="data.can_edit" ng-hide="edit_mode" class="pull-right" href="javascript:void(0)" ng-click="goToEditMode()">
<i title="Edit Table" class="fa fa-pencil-square-o"></i>
</a>
</label>
<div ng-if="edit_mode">
<div class="alert alert-warning" ng-if="!data.columns_specified">
No table definition provided, define your table information and then click Setup Data Table to start building the table definition record
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="columnLength">Table Label</label>
<div class="col-sm-10">
<input class="form-control" ng-model="data.title" type="text" />
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="columnLength">Number of Rows to Display</label>
<div class="col-sm-10">
<input class="form-control" ng-model="data.table_options.row_count" type="number" />
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="columnLength">Max Number of Rows (set to -1 for infinite)</label>
<div class="col-sm-10">
<input class="form-control" ng-model="data.table_options.max_rows" type="number" />
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="columnLength">Allow new Rows</label>
<div class="col-sm-10">
<input ng-model="data.table_options.allow_row_add" type="checkbox" />
</div>
</div>
<button ng-if="temp_columns.length > 0" ng-disabled="data.title==''" ng-click="setupTable()" class="btn btn-primary">Setup Data Table</button>
</div>
<div ng-if="!edit_mode && data.columns_specified">
<table wt-responsive-table>
<tbody>
<tr>
<th ng-repeat="tableHeader in data.table_options.fields|filter:{'visible':true}">
{{tableHeader.label}}
<span class="field-decorations">
<span ng-show="tableHeader.mandatory" style="padding-left: .25em" title="${Mandatory}" class="fa fa-asterisk mandatory" aria-hidden="false"></span>
</span>
</th>
</tr>
<tr ng-repeat="(rowID,rowCounter) in table_data" id="tableRow_{{rowCounter.number}}">
<td ng-repeat="(columnID,field) in rowCounter.fields" ng-if="isVisibleColumn(columnID)" class="column-data">
<div class="form-group" ng-class="{'has-success' : field.value != ''}">
<jb-tablevar-column field="field" data="data" c="c" table_data="table_data" rowid="rowID" columnid="columnID" run-on-change="runOnChange" parse-reference-qual="parseReferenceQual"></jb-tablevar-column>
</div>
</td>
<td ng-if="$index > 0">
<a class="remove-row-link" href="javascript:void(0);" ng-click="deleteRow(rowID)">
<i title="{{data.table_options.remove_row_title}}" class="fa fa-minus-circle fa-2x remove-row"></i>
</a>
<button class="remove-row-btn btn btn-danger" ng-click="deleteRow(rowID)">
{{data.table_options.remove_row_title}}
</button>
</td>
</tr>
<tbody>
</table>
<div ng-if="((data.table_options.row_count < data.table_options.max_rows) || data.table_options.max_rows == -1) && data.table_options.allow_row_add">
<button ng-click="addRow()" class="btn btn-primary">{{data.table_options.add_row_title}}</button>
</div>
</div>
</fieldset>
This is the CSS:
fieldset.edit_mode {
border-left:10px solid #dedede;
padding-left:10px;
}
.has-success .form-control-success {
background-image: url("JB_GreenTick.pngx");
}
.form-control-danger, .form-control-success, .form-control-warning {
padding-right: 2.25rem;
background-repeat: no-repeat;
background-position: center right .625rem;
-webkit-background-size: 1.25rem 1.25rem;
background-size: 1.25rem 1.25rem;
}
i.remove-row {
padding: 0px 5px 20px;
color: #a0303c;
cursor: pointer;
}
button.remove-row-btn {
display:none;
}
.responsive td .form-group {
padding-left:5px;
padding-right:5px;
}
#media only screen and (max-width: 600px) {
.responsive tr{margin-top: 5px;}
a.remove-row-link {display:none;}
button.remove-row-btn {display:block;}
}
I'm trying to put a Font-awesome icon inside an input tag.
This is my HTML code:
<div class="input-group">
<input class="form-control" type="password" placeholder="Password">
<span class="input-group-addon"><i class="fa fa-key fa-fw"></i></span>
</div>
But that icon is bigger than input box as you can see in this picture:
How can I fix the icon?
Bootstrap 3
Checkout the Bootstrap examples in the Font Awesome documentation:
<div class="input-group margin-bottom-sm">
<span class="input-group-addon"><i class="fa fa-envelope-o fa-fw"></i></span>
<input class="form-control" type="text" placeholder="Email address">
</div>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-key fa-fw"></i></span>
<input class="form-control" type="password" placeholder="Password">
</div>
It should work out of the box, so if you still have height differences, check that there is not other CSS stuff that override your input-group-addon height
Working JSFiddle: https://jsfiddle.net/vs0wpy80
Bootstrap 4
Bootstrap 4 introduced some new classes for input groups, we need to use input-group-prepend and input-group-append:
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-envelope-o fa-fw"></i></span>
</div>
<input class="form-control" type="text" placeholder="Email address">
</div>
<div class="input-group mb-3">
<input class="form-control" type="text" placeholder="Email address">
<div class="input-group-append">
<span class="input-group-text"><i class="fa fa-envelope-o fa-fw"></i></span>
</div>
</div>
Working JSFiddle: https://jsfiddle.net/8do9v4dp/5/
input.form-control{
margin-top: 0;
}
In my case it helps
With CSS only
<div class="wrapper">
<input type="text" class="input">
<i class="icon" class="fas fa-some-icon"></i>
</div>
Wrap the input and the icon in a 2 columns grid with this column template: 1fr 0
.wrapper {
display: grid;
grid-template-columns: 1fr 0;
}
Give the icon a relative position from the right:
.icon{
width: 40px;
position: relative;
right: 45px;
font-size: 35px;
line-height: 40px;
cursor: pointer
z-index: 1;
}
Give the input a nice padding on the margin:
.input{
padding-right: 57px;
}
The input will take 100% of the space in the grid and the icon will float atop.
Works well with responsive designs and you can click on the icon.
How can we add a Twitter Bootstrap icon icon-search to the right of a text input element?
The following attempt placed all the icons inside the input element, how can we crop it so it only displays the icon for icon-search?
Current Attempt
CSS
input.search-box {
width: 180px;
background: #333;
background-image: url('/img/glyphicons-halflings-white.png');
background-position: -48px 0;
padding-left: 30px;
margin-top: 45px;
border: 0;
float: right;
}
Updated Bootstrap 3.x
You can use the .input-group class like this:
<div class="input-group">
<input type="text" class="form-control"/>
<span class="input-group-addon">
<i class="fa fa-search"></i>
</span>
</div>
Working Demo in jsFiddle for 3.x
Bootstrap 2.x
You can use the .input-append class like this:
<div class="input-append">
<input class="span2" type="text">
<button type="submit" class="btn">
<i class="icon-search"></i>
</button>
</div>
Working Demo in jsFiddle for 2.x
Both will look like this:
If you'd like the icon inside the input box, like this:
Then see my answer to Add a Bootstrap Glyphicon to Input Box
Bootstrap 5 with 3 different way.
Icon with default bootstrap Style
<div class="input-group">
<input type="text" class="form-control" placeholder="From" aria-label="from" aria-describedby="from">
<span class="input-group-text"><i class="fas fa-map-marker-alt"></i></span>
</div>
Icon Inside Input with default bootstrap class
<div class="input-group">
<input type="text" class="form-control border-end-0" placeholder="From" aria-label="from" aria-describedby="from">
<span class="input-group-text bg-transparent"><i class="fas fa-map-marker-alt"></i></span>
</div>
Icon Inside Input with small custom css
<div class="input-group">
<input type="text" class="form-control rounded-end" placeholder="From" aria-label="from" aria-describedby="from">
<span class="icon-inside"><i class="fas fa-map-marker-alt"></i></span>
</div>
Custom Css
.icon-inside {
position: absolute;
right: 10px;
top: calc(50% - 12px);
pointer-events: none;
font-size: 16px;
font-size: 1.125rem;
color: #c4c3c3;
z-index:3;
}
.icon-inside {
position: absolute;
right: 10px;
top: calc(50% - 12px);
pointer-events: none;
font-size: 16px;
font-size: 1.125rem;
color: #c4c3c3;
z-index:3;
}
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
<div class="container">
<h5 class="mt-3">Icon <small>with default bootstrap Style</small><h5>
<div class="input-group">
<input type="text" class="form-control" placeholder="From" aria-label="from" aria-describedby="from">
<span class="input-group-text"><i class="fas fa-map-marker-alt"></i></span>
</div>
<h5 class="mt-3">Icon Inside Input <small>with default bootstrap class</small><h5>
<div class="input-group">
<input type="text" class="form-control border-end-0" placeholder="From" aria-label="from" aria-describedby="from">
<span class="input-group-text bg-transparent"><i class="fas fa-map-marker-alt"></i></span>
</div>
<h5 class="mt-3">Icon Inside Input<small> with small custom css</small><h5>
<div class="input-group">
<input type="text" class="form-control rounded-end" placeholder="From" aria-label="from" aria-describedby="from">
<span class="icon-inside"><i class="fas fa-map-marker-alt"></i></span>
</div>
</div>
Bootstrap 4.x with 3 different way.
Icon with default bootstrap Style
<div class="input-group">
<input type="text" class="form-control" placeholder="From" aria-label="from" aria-describedby="from">
<div class="input-group-append">
<span class="input-group-text"><i class="fas fa-map-marker-alt"></i></span>
</div>
</div>
Icon Inside Input with default bootstrap class
<div class="input-group">
<input type="text" class="form-control border-right-0" placeholder="From" aria-label="from" aria-describedby="from">
<div class="input-group-append">
<span class="input-group-text bg-transparent"><i class="fas fa-map-marker-alt"></i></span>
</div>
</div>
Icon Inside Input with small custom css
<div class="input-group">
<input type="text" class="form-control rounded-right" placeholder="From" aria-label="from" aria-describedby="from">
<span class="icon-inside"><i class="fas fa-map-marker-alt"></i></span>
</div>
Custom Css
.icon-inside {
position: absolute;
right: 10px;
top: calc(50% - 12px);
pointer-events: none;
font-size: 16px;
font-size: 1.125rem;
color: #c4c3c3;
z-index:3;
}
.icon-inside {
position: absolute;
right: 10px;
top: calc(50% - 12px);
pointer-events: none;
font-size: 16px;
font-size: 1.125rem;
color: #c4c3c3;
z-index:3;
}
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
<div class="container">
<h5 class="mt-3">Icon <small>with default bootstrap Style</small><h5>
<div class="input-group">
<input type="text" class="form-control" placeholder="From" aria-label="from" aria-describedby="from">
<div class="input-group-append">
<span class="input-group-text"><i class="fas fa-map-marker-alt"></i></span>
</div>
</div>
<h5 class="mt-3">Icon Inside Input <small>with default bootstrap class</small><h5>
<div class="input-group">
<input type="text" class="form-control border-right-0" placeholder="From" aria-label="from" aria-describedby="from">
<div class="input-group-append">
<span class="input-group-text bg-transparent"><i class="fas fa-map-marker-alt"></i></span>
</div>
</div>
<h5 class="mt-3">Icon Inside Input<small> with small custom css</small><h5>
<div class="input-group">
<input type="text" class="form-control rounded-right" placeholder="From" aria-label="from" aria-describedby="from">
<span class="icon-inside"><i class="fas fa-map-marker-alt"></i></span>
</div>
</div>
Since the glyphicons image is a sprite, you really can't do that: fundamentally what you want is to limit the size of the background, but there's no way to specify how big the background is. Either you cut out the icon you want, size it down and use it, or use something like the input field prepend/append option (http://twitter.github.io/bootstrap/base-css.html#forms and then search for prepended inputs).
For bootstrap 4
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<form class="form-inline my-2 my-lg-0">
<div class="input-group">
<input class="form-control" type="search" placeholder="Search">
<div class="input-group-append">
<div class="input-group-text"><i class="fa fa-search"></i></div>
</div>
</div>
</form>
Demo
Bootstrap 4.x
With Bootstrap 4 (and Font Awesome), we still can use the input-group wrapper around our form-control element, and now we can use an input-group-append (or input-group-prepend) wrapper with an input-group-text to get the job done:
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Search" aria-label="Search" aria-describedby="my-search">
<div class="input-group-append">
<span class="input-group-text" id="my-search"><i class="fas fa-filter"></i></span>
</div>
</div>
It will look something like this (thanks to KyleMit for the screenshot):
Learn more by visiting the Input group documentation.
Bootstrap 5
According to Bootstrap 5 docs
.input-group-append and .input-group-prepend. You can now just add
buttons and .input-group-text as direct children of the input groups.
Example:
<div class="input-group">
<div class="input-group-text">File input</div>
<input class="form-control" id="formFile" type="file" />
<button class="btn border" type="button" style="background-color: #e9ecef;">
<i class="fas fa-times"></i>
</button>
</div>