How do I add a "search" button in a text input field? - html

How do I create a similar “search” element to the one in this site?
If we view source, he has one textbox followed by a <span> tag.
<input type="text" name="q" id="site-search-input" autocomplete="off" value="Search" class="gray" />
<span id="g-search-button"></span>
Where do I get a similar "magnifying glass" image?

Put the image into the span, for example using background-image, then give it a relative position and move it to the left so it overlaps the right end of the search box, for example:
#g-search-button {
display: inline-block;
width: 16px;
height: 16px;
position: relative;
left: -22px;
top: 3px;
background-color: black; /* Replace with your own image */
}
Working example on JSBin

Your eyes are deceiving you. The button is not within the text box. Using a background image is NOT the way to go, as it wont provide the clickable submit button.
What you need to do is add a wrapper div around the input:text and input:submit.
The wrapper will look like it's a text box, but will actually contain a transparent text box and a submit button. You'll need to specifically remove the styles for the input:text and input:submit elements.
It's very important that you keep the submit button, otherwise hitting enter while searching will not have a default reaction. Additionally placing the submit button after the text field allows people to actually click on the button.
You can make your own magnifying image, they're pretty easy to make in a 20x20px transparent png.
.search {
border: 1px solid #000000;
width: 200px;
}
.search input[type="text"] {
background: none;
border: 0 none;
float: left;
height: 1.5em;
line-height: 1.5em;
margin: 0;
padding: 3px 0;
width: 180px;
}
.search input[type="submit"] {
background: #CCCCCC url(path/to/image.jpg);
border: 0 none;
height: 1.5em;
line-height: 1.5em;
margin: 0;
padding: 3px 0;
text-indent: 100px;
width: 20px;
}
<form ...>
<div class="search">
<input type="text" />
<input type="submit" />
</div>
</form>

If you view the page in Google Chrome, right-click on the search button and select “Inspect element”, you’ll be able to see the CSS used to achieve this effect.
If you’re not familiar with CSS, I thoroughly recommend ‘CSS: The Definitive Guide’.

A site like Iconspedia has a number of free icons that are similar.
Wherever you get the icon be careful to ensure that you have the rights to use it in your application. Many graphics are protected and some have restrictive licenses.

If you use a background image on the field then there's no way to bind to it to get the click action. So the solution is to have a separate search field and image, so you can bind click event in jQuery to the image. Fiddle here:
http://jsfiddle.net/Lzm1k4r8/23/
You can adjust left: position to be left or right side of the search box.

To help with user feedback why not add the pointer icon to your mouse when you're hovering over the magnifying glass? Just att this to your CSS:
.search:hover {
cursor:pointer;
}

I'd like to plug a new jQuery plugin I wrote because I feel it answers to the OP's request.
It's called jQuery.iconfield: https://github.com/yotamofek/jquery.iconfield.
It lets you easily add an icon to the left side of your text field. For using the icon as a button, you can easily bind to the 'iconfield.click' event, which is triggered when the user clicks the icon. It also takes care of changing the cursor when the mouse is hovering over the icon.
For instance:
$('#search-field').iconfield( {
'image-url': 'imgs/search.png', // url of icon
'icon-cursor': 'pointer' // cursor to show when hovering over icon
} );
$('#search-field').on( 'iconfield.click', function( ) {
$('#search-form').submit()
}
I would love to get some feedback on my work.

Related

Custom input field border behavior when trying to include password eye

I am trying to create a custom input field with a text input and a button with an eye icon to show/hide the password text.
Now, I know that simply create an input field and absolutely position the eye button but the problem with that is that on certain browsers, the Lastpass extension or Safari keychain will insert their respective icons thus overlapping the eye icon which I always want to be visible and clickable. As an example, this is bad:
Therefore, this is what I want to achieve:
I am trying something like this:
<div className="input-wrapper">
<input className="password-input" type="password" />
<button className="password-eye">
</div>
CSS
.input-wrapper {
position: relative;
border: 1px solid #ccc;
}
.password-input {
border: none;
width: calc(100% - 32px);
height: 40px;
}
.password-eye:extend(.icon-password-eye all) {
background: white;
border: none;
height: 40px;
font-size: 24px;
position: absolute;
right: 0px;
top: 0px;
padding-right: 8px;
}
.icon-password-eye::before {
content: '\e618';
}
The problem that I'm facing is that because I want the whole wrapper to behave as if it were an input component, when the user selects the input field, only the actual input part is selected. In addition, if the password validation errors out, only input field is shown to be red while I want the entire portion to be red.
This is what is happening:
Any help would be greatly appreciated.
what you want to do is changing the border CSS of the input-wrapper class.
and not the input
you need to change the code that changing the input css to change the input-wrapper css.
if you need help in that , please give the full code.

Extend chechbox clickable area without label

My code looks like this:
<div class="hovereffect">
<img class="img-responsive" src="/some-image" alt="">
<input type="checkbox" class="img-checkbox">
</div>
.hovereffect {
width: 100%;
height: 100%;
margin-bottom: 20px;
float: left;
overflow: hidden;
position: relative;
text-align: center;
cursor: default;
}
.hovereffect .img-checkbox{
position: absolute;
width: 18px;
height: 18px;
top: 3px;
right: 5px;
cursor: pointer;
}
So there is the checkbox in the right upper corner over the image and would like to extend the clickable are to the whole image for a better user experience.
As you can see the checkbox has no label and I would like to achieve the goal without a label.
I tried tricks with the ::after element which kinda worked with chrome but not really with firefox and I couldn't make the clickable area responsive that is to say, extend to the whole area of the image.
Can you use Javascript/jQuery?
You can start by assigning unique id to every image you have (Ex: img1,img2,img3) and every checkbox associated to the image (Ex: img1-checkbox).Then you can use the code below:
$('#img1').click(
$('#img1-checkbox').attr('checked', true);
);
Or something like that.
This has been a problem since a very long time and you simply cannot achieve your goal with pure css. The only available ways of getting it done are ou using label or jquery/javascript or :after pseudo class.
If you want to expand the checkbox size, then try this:
.hovereffect input[type=checkbox]
{
width:100px !important; //adjust as per need
height:100px !important; //adjust as per need
}
This will increase the clickable area of the checkbox field. Working Link
But, if you want the checkbox to be transparent and show the image behind, then you will have to use label and set it's background color to transparent.
I'm waiting to see someone prove me wrong with working code.

how to prevent text inside an input from being overridden

I'm trying to implement an html control similar to the how search works in google chrome. I'm aware that in chrome it might not be built in html, but I'm trying to achieve the same functionality.
The way the search works is that adjacent to the text entered into the input, there's an extra text field that present the current index of a total number. It's also worth while noticing that the highlighted border is of the entire input (including the entered text and the index and sub total count). Also, when entering a long text, the index and sub total indicator (e.g. 0 of 10) doesn't get overridden, and the text itself scrolls.
Does anyone have any simple approach for implementing this feature set?
A simple aproach would be adding padding to the input, and positioning the text you want to keep over it. Rewriting the value of the input with pogramming.
HTML
<div class="form-text">
<input type=text placeholder="0 of 0" id="youridhere"/>
<label for="youridhere" class="static-value">Get this label to appear</label>
</div>
CSS
.form-text{
position:relative;
}
input{
padding:5px 5px 5px 150px;
}
.static-value{
position:absolute;
left:10px;
font-size:0.85em;
top:9px;
}
Pen of it working
This is the simplest solution, but you can find other better results and best practices using javascript. Wich I think would be something like detecting the pressed key and adding it the to string programatically instead of the standart browser behaviour.
I like this CSS based solution cause you can customize the fixed text, and don't need to change the standart behaviour of input fields.
-------- Edit --------
Actually, the best solution would be with the label element, with the for attribute, link it to the field. So when clicked it leads the user to the field. Edited.
I've made the foundation of it with only CSS, see the demo follows.
jsfiddle
* {
box-sizing: border-box;
}
div {
position: relative;
width: 10em;
}
input {
width: 100%;
padding: 4px;
border: 1px solid;
padding-right: 52px;
}
span {
display: block;
border-left: 1px solid;
width: 50px;
text-align: center;
position: absolute;
right: 0;
top: 2px;
}
<div><input type="text"><span>etc</span></div>

Styling drag and drop uploading only html/css

I'm working on a drag and drop field for files and so far I have been able to style it however I wish. Except I need to somehow add text that says 'drag and drop' inside the area. I'm not really sure how to accomplish this.
I have tried adding content:'drag and drop';
And I have tried a background image, but it was not scalable and not something that I believe is the right solution.
my html looks like this
<input type="file" class="form-control" name="uploadCover" value="" />
and my css looks like this
input[type="file"]{
padding-bottom: 4em;
padding-top: 2em;
width: 100%;
border: 1px dashed #000;
}
input[type="file"]:hover{
border: 1px dashed #67a5c5;
}
and the field looks like this...ignore the ugly yellow!
Any help or ideas would be wonderful! I'm really trying to avoid javascript.
How about this for a starting point? It uses a :after pseudo element, so that the content you're adding follows the control itself.
It is "centre aligned" (using margin auto 50%) to avoid overlapping the filename text, but you'll probably need add some more CSS to make sure this doesn't happen on your web page.
input[type="file"]:after {
content: "Drag and drop";
display: block;
min-width: 20em;
margin: auto 50%;
}
http://jsfiddle.net/jrG7v/
Edit: as pointed out this only works in Chrome. No text is displayed in Firefox, and IE11 (seems I've been upgraded since I last used IE10) still displays the text-style input box. Hopefully somebody else has a better answer.
For compatibility with browsers, I'd go with:
<div>
<input type="file" class="form-control" name="uploadCover" value="" />
<div class="drag">Drag and Drop</div>
</div>
div.drag { min-width: 100px; margin-top: -25px; padding-left: 10px; font-family: arial; font-size: 12px;}
Fiddle:
http://jsfiddle.net/jrG7v/3/

HTML Custom Upload Button with hand cursor on hover?

Following the CSS style trick from this question I was able to create a custom upload button. Now, the challenge is to make the entire portion of the button change the mouse pointer icon to the hand one.
The way I partially achieved this can be seen here (jSFiddle). As you can see, the cursor only appears to be a hand while hovering the right area of the the button (I'm in the latest version of firefox).
The css (also on jSFiddle)
<span id="uploadMask">
<input type="file" multiple="multiple" name="file" id="file_upload">
<span class="button">Select Documents</span>
</span>
The css (also on jSFiddle)
#uploadMask {
width:160px;
display: block;
float:left;
overflow: hidden;
height:32px;
margin-right: 5px;
position: relative;
}
#uploadMask input {
position: absolute;
top:0;
bottom: 0;
right:0;
opacity: 0;
z-index: 2;
cursor: pointer;
}
#uploadMask .button {
background:#ccc;
line-height:24px;
padding:5px 15px;
display:block;
}
Any ideas?
There's nothing you can do it seems to get the cursor property to work on the "text" portion of <input type="file">, but the "button" part does display the hand pointer.
http://jsfiddle.net/gN2JM/17/
No hand cursor on the red part!
Borrowing from the solution to this question:
Is there a way to make the native `browse` button on a file input larger cross browser?
You can enlarge the button size by adding:
#uploadMask input {
font-size:200px; /* Any high number to guarantee it's big enough,
overflow:hidden on the parent takes care of the rest */
}
http://jsfiddle.net/gN2JM/15/
If you take off the opacity: 0;, you'll see why there is a place where the cursor:pointer doesn't show up. http://jsfiddle.net/gN2JM/13/ Whenever you are moused over the actual button, it gives the regular cursor.
Use THIS for a solution that supposedly works all the time, or just set the position of the button to go off the edge of the screen>> http://jsfiddle.net/gN2JM/14/