Positioning background image on form element with CSS sprites - html

I am trying to get a magnifying glass as the background for my input element. The magnifying glass icon is part of a CSS sprite that looks like this:
To position it, I've used these properties:
#search-form input[type="text"] {
background: url('../images/icons.png') no-repeat left center;
background-position: 0px -30px;
border: 1px solid #a9e2ff;
padding: 3px;
width: 200px;
font-size: 1em;
padding-left: 20px;
}
But the background still appears at the top of the input box rather than aligned in the vertical middle and to the left. I've also tried doing:
background: url('../images/icons.png') no-repeat left middle;
but that doesn't work either.
If it matters, although I'm guessing it doesn't, here's my form markup:
<form action="/search" method="get" id="search-form">
<input type="text" placeholder="Search..." name="s">
</form>
Thanks for any help.

You declared background-position two times. The first one at the end of the background short-hand property, the second one on the next line. Solution: Split all single background rules like this (additionally, 0 -24px is the correct value):
#search-form input[type="text"] {
background-image: url('http://i.stack.imgur.com/MFpLm.png');
background-repeat: no-repeat;
background-position: 0 -24px;
border: 1px solid #a9e2ff;
padding: 3px;
width: 200px;
font-size: 1em;
padding-left: 20px;
}
And now you can face the real problem with your design: other sprites will be visible in the input area if there are no sufficient space between them.

Related

How do I make the text in the input start from the top?

I have an input with a height and width more than regular. And the text starts at the center vertically and overflows to the right when I type in it. How do I make it so the text starts at the top-right corner and doesn't overflow past the width of the input without using JavaScript? I also don't want to use a textarea. Here is my code:
.notesheet {
background-color: rgb(224, 214, 67);
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
margin-top: 300px;
border: none;
outline: none;
box-shadow: 0 0 10px #9c9898;
border-radius: 15px;
height: 300px;
width: 500px;
}
<input class="notesheet"></input>
Why not use the <textarea> tag?
you can use attribute resize: none; to prevent user change width and height
eg:
​<textarea class="notesheet" rows="30" cols="50"></textarea>
if you are insisted to use input element there is a workaround for that like below:
.notesheet {
... your css goes here
background-color: rgb(224, 214, 67);
position: absolute;
border: none;
outline: none;
box-shadow: 0 0 10px #9c9898;
border-radius: 15px;
width: 500px;
/* the point that i want to make */
padding: 5px 5px 280px 5px;
text-align: right;
}
instead of using height property, you can remove it and use padding to give input padding bottom of something like 280px for making your text aligned to top section of the input. also for aligning it to stick it to the right, you can use text-align: right;.
but overall there is no clear solution in css for input element to act like textarea and wrap your words in it's boundary, or if there is, i don't know it yet!
Here is the code snippet that i tested and modified your code:
http://jsfiddle.net/4xw6urjz/7/

Create HTML Element with text that extends into border

I'm trying to create an HTML element that looks like this:-
Basically, a <div> or other element with a border, and the internal (possibly multi-line) text centred within the div, but extending into the border area.
So far, the only scheme I have that works is to use three(!) divs : One for the border, a second one as a container for the third one, to ensure the vertical centring is right.
<div class="BORDER" style = "left: 190px; top: 50px;">
</div>
<div class = "WRAPPER" style = "left: 190px; top: 50px;">
<div>TEST THREE</div>
</div>
This feels awkward: Is there a way of achieving the same look using fewer elements?
Restrictions (clarified)
The text can have one or more lines
The border will be an image, and will eventually be stretched via the border-image mechanism.
JSFiddle with CSS and some other (failed) attempts is here. http://jsfiddle.net/6wB3k/
I'm not sure if it's adaptable to your real use case but I can achieve your display with only one div :
HTML :
<div class=dystroy>TEST FOUR</div>
CSS :
.dystroy {
position: fixed;
left: 50px; top: 50px;
margin: 0px;
padding: 16px;
height : 48px;
width : 50px;
display : table-cell;
vertical-align : middle;
text-align : center;
color: #000000;
font-size : 16px;
font-family : Calibri;
}
.dystroy:after {
position: relative;
display : table-cell;
top: -48px; left:0px;
border: solid;
border-width: 16px 16px;
border-color: #e0e0e0;
height: 32px;
width: 50px;
content:" ";
z-index:-1;
font-size : 16px;
}
Demonstration
EDIT : in fact there's no real dynamic vertical centering here, which would need an additional div.
If you are open to use CSS3 shadows, then you can try this:
Fiddle: http://jsfiddle.net/6wB3k/2/
div {
width: 50px;
height: 50px;
text-align: center;
border: 1px solid #ccc;
-webkit-box-shadow: 0 0 0px 11px #ccc inset;
}
Syntax: box-shadow: x-offset y-offset blur spread #color inset
You can experiment with blur and size to adjust according to your requirements.
Update:
As per your comment regarding the need to use border-image, here is one try using background-image instead of a 9-grid border-image. I think, this can suit your purpose of using images?
Updated Fiddle: http://jsfiddle.net/6wB3k/3/
div {
width: 50px;
height: 50px;
display: table-cell;
vertical-align: middle;
text-align: center;
background-image: url(http://placehold.it/11x11), url(http://placehold.it/11x11), url(http://placehold.it/11x11), url(http://placehold.it/11x11);
background-position: left top, left bottom, top right, bottom right;
background-repeat: repeat-x, repeat-y, repeat-y, repeat-x;
}
The following works:
<div style="border: 9px solid #ccc; width: 40px;">
<p style="margin: 0 -.5em;">Test text</p>
</div>
However, if you're spilling over your border, it's not strictly a border in the literal sense so much as it is a background image; perhaps there's another way of looking at your layout?

Why buttons (and also images) tend to dive under the text line?

I want to make a simple search form within a table cell:
<td style="vertical-align: center">
<div id="search">
<form action="search" method="get">
Search:
<input type="text" value="enter something" class="autoempty" />
<button type="submit"></button>
</form>
</div>
</td>
The button is supposed to have a search icon, using this CSS:
div#search button {
/*reset the normal button behavior*/
padding: 0px;
border-width: 0px;
border-style: none;
background-color: transparent;
/*Make it square*/
width: 30px;
height: 30px;
/*set background*/
background-image: url("../images/search_grey.png");
background-position: center center;
background-repeat: no-repeat;
background-size: contain;
/*fancy cursor*/
cursor: pointer;
}
This is what it looks like:
The colors show the element dimensions displayed by Firebug. Light-blue is the <td> element.
I hoped vertical-align property would fix it, but it didn't. I also had the same problems with adding <img> tags to text.
When I have similar alignment problems, especially when dealing with inline-block elements (not experienced enough to know how closely tables relate to that in terms of similarities), what usually helps me is some combination of:
vertical-align: top;
adjusting height:;
adjusting padding-top:; or more commonly, margin-top:; to re-align images from there
AKA: Instead of v-aligning center, v-align from top and use padding/margin to position from there. Hope this helps some.
Add this to your CSS
#search button, #serach input
{
display: inline-block;
vertical-align: middle;
}
Working Fiddle

Align Image To Left Of Input Box & Text To The Right

I have aligned the image to the left of the input box but the writing is overlapping the image. How do I get around this? The image has been aligned to the left just fine, I just need the input text to be aligned to the right of the image now instead of overlapping
CSS
input {
background: url(../../core/images/search.png) no-repeat center left 5px #FFF;
text-align: left;
padding: 5px;
font-family: 'Raleway', sans-serif;
color: #444;
border: 3px solid #FFF;
outline: none;
border-radius: 3px;
margin: -4px 20px;
position: absolute;
width: 270px;
}
HTML
<input type="text" autocomplete="off" name="name">
give padding-left to input according to width of image
Why not to use padding ? Have a look:
padding-left: 65px;
JSFiddle: http://jsfiddle.net/8JLXj/9/
Or text-indent :
text-indent: 65px;
JSFiddle: http://jsfiddle.net/8JLXj/10/
use text-index property:
text-index:(pic\'s width+gap between pics and first letter)px;
or you can also use padding-left:10px; to create the distance from pic.

css position label text in the true center (vertical and horizontal) without moving the label

I am trying to use CSS to position text of a label in the exact center of a control. The CSS below still leaves the text at the top of the label. Any ideas please?
JSFIDDLE
CSS
.plate_well {
float: left;
margin: 0;
padding: 0;
height: 30px;
width: 30px;
text-align: center;
vertical-align: 15px; /* this doesnt do anything ?? */
font-size: 7pt;
-moz-border-radius: 15px;
-webkit-border-radius: 15px;
-khtml-border-radius: 15px;
border-radius: 15px;
border: 1px solid #AAAAAA;
}
HTML
<label class="plate_well" id="foo">bar</label>
(I also have the issue of IE being unable to render curves but that's a separate question)
You can use line-height: 30px; to center a single line of text instead. The 30px needs to be the same value as the height of the element you wish to vertically center text in.
Note: The fiddle has height:40px whereas the code in question has height:30px. Just choose the line-height to match the height.
vertical-align would actually moving the entire <label> (and text) down but only applies to inline-level and table-cell elements. It is not applying in your case as the <label> is floated.