Putting a image inside a input box - css

Similar to Put icon inside input element in a form
But how to I get the icon inside and on the right side of the box?

Try this:
background: url(images/icon.png) no-repeat right center;
The explanation for this CSS is as follows:
background: [url to image] [don't repeat] [horizontal position] [vertical position]

As an addition to Bazzz's answer, if you don't want the icon right up against the right border, you can specify padding on the right side.
background: url(images/icon.png) no-repeat right 10px center;
This will put the icon on the right side, but keep it 10 pixels from the very edge. So, the CSS explanation would look like this:
background: [URL to icon] [specify no repeating] [horizontal position] [padding on right side] [vertical position]

Same answer except change padding-left to padding-right, and change the positioning of the background.
Something like:
background: url(images/comment-author.gif) no-repeat scroll right;
padding-right: 30px;

Using image and SVG. Padding works well in all browsers (mars 2017)
Image file
.date_element {
background-image: url(../img/calendar.png);
background-repeat: no-repeat;
background-position: right center;
background-origin: content-box;
}
SVG file
.date_element {
background-image: url(../img/calendar.svg);
background-repeat: no-repeat;
background-position: right center;
background-origin: content-box;
background-size: 2.5rem 2.5rem;
}

Related

Position label text 10px right of the background-image

I have a label that has a background image and some text. I need to position the text 10px right of the background image, such that the background image is clearly visible.
I need to do this without adding additional HTML elements or Javascript.
Here's the source code I have so far:
label {
background-image: url('http://missouri.municipalbonds.com/img/icons/add.png');
background-repeat: no-repeat;
background-position: left top;
}
<label>This is my label</label>
Any ideas on which CSS property I am missing to achieve this?
Image is 16px wide, 10px to the right of it is a total of 26px.
Use padding or padding-left for the desired effect.
http://jsfiddle.net/L0ctetdg/
label{
background-image: url('http://missouri.municipalbonds.com/img/icons/add.png');
background-repeat: no-repeat;
background-position: left top;
padding-left: 26px;
}
<label>This is my label</label>
It must also be said that this is seriously basic stuff. Before you come running to StackOverflow on the first problem you encounter, try treating the language with a little respect by studying it properly.
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started
http://hashcss.com/schools/
Untested but padding-left: 10px; should do it

CSS How do i repeat a background image horizontally and always keep it at the bottom of a webpage?

I am new to CSS and am tinkering with a few options. I want a pic called 'toplayer.jpg' to be repeated horizontally at the bottom of my webpage. I have used this code in an external style sheet. The pattern repeats horizontally...however it floats to the top of the page.
body {
background-image: url("../Images/toplayer.jpg");
background-position: bottom;
background-repeat: repeat-x;
}
Any suggestions for this noob would be appreciated. Thank you.
Your background declarations should work, I guess the issue is that the body tag has no height :
html,body {
height: 100%;
}
body {
background-image: url("https://farm9.staticflickr.com/8760/17195790401_ceeeafcddb_o.jpg");
background-position: bottom;
background-repeat: repeat-x;
}

background-image with a left and right, the repeat-x of center image makes the right image not show up

.news-header {
background-image: url(maureske_green_left.gif), url(maureske_green_body.gif), url(maureske_green_right.gif);
background-position: left, center, right;
background-repeat: no-repeat, repeat-x, no-repeat;
height: 31px;
}
This works good but the repeat-x of maureske_green_body.gif makes maureske_green_right.gif to not show up.
Setting a width doesnt make the right image to show neither.
If I do no-repeat on the center image all images show up but of course theres a gap between all three. So how do I fix without making center image same width as webpage?
Thanks in advance!
Jarosław
Top image layer is first in your css, so you need to reorder them this way:
background-image: url(maureske_green_left.gif), url(maureske_green_right.gif), url(maureske_green_body.gif);
In short version your new css will be:
.news-header {
background: url(maureske_green_left.gif) no-repeat left top,
url(maureske_green_right.gif) no-repeat right top,
url(maureske_green_body.gif) repeat center top;
height: 31px;
}
Interesting article about multiple css backgrounds here

Multiple background images positioning

I've got three background images, all of width 643px. I want them to be set out like so:
top image (12px height) no-repeat
middle image repeat-y
bottom image (12px height) no repeat
I can't seem to do it without getting them to overlap (which is a problem because the images are partially transparent), is something like this possible?
background-image: url(top.png),
url(bottom.png),
url(middle.png);
background-repeat: no-repeat,
no-repeat,
repeat-y;
background-position: left 0 top -12px,
left 0 bottom -12px,
left 0 top 0;
Your problem is that the repeat-y is going to fill the whole height, no matter where you position it initially. Thus, it overlaps your top and bottom.
One solution is to push the repeating background into a pseudo element positioned off of the container by the 12px at the top and bottom. The result can be seen here (the opacity in the demo is just to show that there is no overlap going on). Without opacity, see here. The relevant code (tested in CSS3 browsers: IE9, FF, Chrome):
CSS
div {
position: relative;
z-index: 2;
background: url(top.png) top left no-repeat,
url(bottom.png) bottom left no-repeat;
}
div:before {
content: '';
position: absolute;
z-index: -1; /* push it to the background */
top: 12px; /* position it off the top background */
right: 0;
bottom: 12px; /* position it off the bottom background */
left: 0;
background: url(middle.png) top left repeat-y;
}
If you needed or wanted IE8 support (which does not support multiple backgrounds), then you could put the top background in the main div, and put the bottom background in by using the div:after pseudo element positioned to the bottom of the container.
If you can add padding/borders to the block equal to the backgrounds you want to position without overlapping other block, you can use the background-clip & background-origin to position the top and bottom backgrounds over the paddings/borders, and the repeating background over the content/paddings+content.
Here is an example: http://dabblet.com/gist/2668803
For your code, you'll possibly need to add something like this:
padding: 12px 0;
background-clip: padding-box, padding-box, content-box;
background-origin: padding-box, padding-box, content-box;
or
border: solid transparent;
border-width: 12px 0;
background-clip: border-box, border-box, padding-box;
background-origin: border-box, border-box, padding-box;
And you'll get what you need. If you can't get the paddings/borders, the pseudo-element like ScottS mentioned would work perfectly.
Try do it like this:
background: url(PICTURE.png) left top no-repeat, url(PICTURE2.png) right bottom no-repeat, url(PICTURE3.jpg) left top no-repeat;
}
EDIT:
Was just an example, but here's the css with your css:
background: url(top.png) left 0px top -12px no-repeat, url(middle.png) left 0px top 0px repeat-y, url(bottom.png) left 0px bottom -12px no-repeat;
}
I actually found a simpler fix, because I was having this same issue with a horizontal navigation.
Rather than adding code like the other answers you just have to list it differently in your CSS. The center image that repeats needs to be listed last, not first or second.
In my code it looks like this:
background-image: url(../images/leftNav.gif), url(../images/rightNav.gif), url(../images/centerNav.gif);
background-position: left, right, center;
background-repeat: no-repeat, no-repeat, repeat-x;
to use backgroud-position with 2 arguments, must to Write in extended writing backgroud-position-x and backgroud-position-y
background-position-x: left 0;
background-position-y: top -12px, bottom -12px, top 0;
A radical but effective way to deal with this, if:
you want to apply backgrounds with no overlapping to a ":before"
the ":before" element as a known max height
&:before {
background: url('vertical-line.png') no-repeat 0px,
url('vertical-line-repeat.png') no-repeat 140px,
url('vertical-line-repeat.png') no-repeat 200px,
url('vertical-line-repeat.png') no-repeat 260px,
url('vertical-line-repeat.png') no-repeat 320px,
url('vertical-line-repeat.png') no-repeat 380px,
url('vertical-line-repeat.png') no-repeat 440px,
url('vertical-line-repeat.png') no-repeat 500px,
url('vertical-line-repeat.png') no-repeat 560px,
url('vertical-line-repeat.png') no-repeat 620px,
url('vertical-line-repeat.png') no-repeat 680px,
url('vertical-line-repeat.png') no-repeat 740px;
}
Here's a method that uses 3 div's for each of the Top, Middle, and Bottom images that are transparent to apply to your webpage.
Background wallpaper is optional.
Tested in modern browsers and is IE8 friendly.
This method allows you to treat the body element as it should be treated, i.e., your webpage markup does not need to be in a wrapper or containing element.
jsFiddle Example
jsFiddle Example with centered filled
Since the above example uses image place holder content that is without transparency for Top and Bottom images, you can verify markup works with transparency with this jsFiddle that uses mini transparent icons in repeat mode HERE.
The only (practical, non hair-threatening) way I see is do do that in Javascript, when the page has loaded, and when it is resized, with a canvas sized to fit the innerHeight and the 3 images: draw the first one once at the top, draw the second as many times as required to cover the remainder of the canvas, and draw the 3rd one at the bottom of the canvas. Position the canvas at 0,0 with a ridiculously negative z-index.
I had a go at it with 3 images (643 x 12, 100 and 12) and of course the first issue I saw is that the 3rd image is drawn over part of the last iteration of the 2nd image -- unless you have a window height of exactly 12+12+(p2.height*X), you'll have some overlap. But that's expected, right?
I think z-index will fix this because z-index only affects CHILD elements, meaning you can't mess up anything else on the page that uses z-index.
top and bottom images z-index:3;
middle image z-index:2; background-repeat:repeat-y;

Aligning background image to right

http://jsfiddle.net/Zx836/
In the second box, notice how the arrow is in the same place. How can I make it flow with the div?
Using scroll right center in the background property works but I want to keep some padding right.
I'm also trying to avoid using 2 divs or the tag. Is this possible just via the .box div?
.right-align-background {
background-position: center right;
}
http://jsfiddle.net/Zx836/1/
You could also use background-origin: content-box; if your you would like your background to line up with the padding you set on the element.
Something like this:
input.error {
background: no-repeat;
background-image: url('../images/field-error.png');
background-position: right center;
background-origin: content-box;
padding-right: 5px;
}
How about adding the image using :after instead?
.box {
background: #a6cf83;
float:left;
color:#333;
font-weight:bold;
padding:8px;
}
.box:after {
content: url(http://cdn1.iconfinder.com/data/icons/humano2/24x24/actions/new-go-next.png);
}
Calc() has 95% browser support in the US now (September, 2017), and provides an elegant solution to this problem.
background-position: calc(100% - 24px) center;
If you need padding on the right, I think you might have to go with percentages :
background-position:95% 50%;
will do quite nicely if your elements are of similar width.

Resources