Clickable Icon Input Feature
This code snippet has been tested on IE9+, Firefox, and Chrome browsers. Unlike other solutions using z-index
, this code allows users to click on the icon for input, making it user-friendly across different browsers.
.input-prepend {
border-radius: 4px;
background: white;
}
.input-prepend .add-on {
margin-right: -28px;
}
.input-prepend input {
border-radius: 4px;
background: none;
padding-left: 34px; /*28 for icon, 6 for normal padding*/
}
Technical Breakdown
The negative right margin of the icon causes the input
element to overlap it. The input
is given all the necessary styles like border-radius
and transparent background to ensure the icon remains visible. Extra padding on the right of the input
accommodates the icon. Also, the wrapper element is styled with border-radius
and background color to maintain consistency.
Update: Removing Inset Shadow on Icon
This updated version addresses the issue of inset shadow mentioned in a comment. Some browsers may not fully support the pointer-events
property, causing a small portion of the icon area to be non-responsive for triggering input actions.
.input-prepend:before,
.input-prepend:after{
content: '';
display: block;
top: 1px;
left: 1px;
width: 24px;
height: 4px;
border-radius: 4px 0 0 0;
border: 2px solid #eee; /* matching icon's background */
border-width: 2px 0px 0px 2px;
position: absolute;
z-index: 2;
pointer-events: none; /* for browsers that support it */
}
.input-prepend:before {
width: 4px;
height: 24px;
top: 4px;
border-radius: 0 0 0 4px;
}