15

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?

Community
  • 1
  • 1
Jason94
  • 13,320
  • 37
  • 106
  • 184

4 Answers4

29

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]

Bazzz
  • 26,427
  • 12
  • 52
  • 69
8

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]

Community
  • 1
  • 1
Chris
  • 458
  • 4
  • 17
3

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;
Paul
  • 139,544
  • 27
  • 275
  • 264
  • Have you tried using padding-right? I copied your example but for some reason the gif will always stay top-right and vertically-centered. – libjup Jul 29 '13 at 14:39
  • 1
    just figured out if you use background-position: right 30px top 10px positioning works well – libjup Jul 29 '13 at 14:46
2

Using image and SVG. Padding works well in all browsers (mars 2017)

enter image description here

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;
}
SandroMarques
  • 6,070
  • 1
  • 41
  • 46