Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8678307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:41:00+00:00 2026-06-12T20:41:00+00:00

Is it possible to position a sprite icon as a background of an element?

  • 0

Is it possible to position a sprite icon as a background of an element?
I have a file, “icons.png” which contains several icons. I want to select one of those as a background of an element.
Usually I would use
.sprite {
background: url('imgs/icons.png') no-repeat 0 -21px;
width: 17px;
height: 10px;
}
and use this class for a button, etc…

The problem is I have a text input and I want to modify it’s placeholder
.First I did this, which works perfectly if the file I use is the icon itself

:-webkit-input-placeholder{ background: url('singleIcon.jpg') center right no-repeat; }

But now I want to use a file which contains more icons.
Is it possible to use something like this ?

:-webkit-input-placeholder{ background: url('imgs/icons.jpg') center right no-repeat; }

The problem in the last line of code is that it will select all my image (which of course contains all my icons I want to use on the website), I want to select only a part of that image ( the icon I want to use )

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:41:01+00:00Added an answer on June 12, 2026 at 8:41 pm

    Actually, the sprites are used only as background (or you’ve to set up some kind of complicated cropping).

    What you have to do is to set the size of the element to the same sprite’s part that you have to show, and the position of the background equal to the x and y coordinates of the icon in the sprite, starting from the top left.


    An example taken from this nice article:

    sprite example 1

    “Item 2” is 116×48, begins at 12px (x coord) and 70px (y coord).

    So your element’s CSS should be:

    .element {
        width:116px;
        height:48px;
        background:url(sprites.png) -12px -70px no-repeat;
    }
    

    But, what if your element is taller/wider than the above dimensions? Then, you’ve to isolate that icon with enough transparent/white space so that the other icons won’t show up.

    sprite example 2

    If you look up at Facebook sprites, you’ll notice that some of them are very long, some others groupped, some others isolated. You’ve to adapt the sprite for each situation.


    Edit: ok, i got your actual needing.

    It’s not easy with inputs because you can’t use pseudo-elements on it. Here comes a workaround.

    Demo

    First of all, wrap the input inside a div:

    <div class="inputWrapper">
        <input type="text" placeholder="placeholder text">
    </div>
    

    Then add some CSS:

    div.inputWrapper {
        position:relative; /* that's important */
        float:left; /* or display:inline-block; */
    }
    div.inputWrapper:after {
        background:#000 url(sprites.png) 0 -2px no-repeat; /* adjust background position */
        content:" "; /* whitespace needed for the pseudo-element to be displayed */
        position:absolute;
        top:1px; right:2px; /* some room for the borders */
        width:16px; /* icon width */
        height:18px; /* icon height */
    }​
    div.inputWrapper input {
        padding-right:16px; /* so the text won't go behind the icon */
    }
    

    I know it’s complicated, but the alternative is to create another http-request … the choice is yours.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Without using percentages, is it possible to position a background image 2px from the
Is it possible to get the position of an element in the body even
Is it possible to get the top position of an element using javascript/jquery ?
Is it possible to position a background image at the bottom of the window
Is it possible to know position of hardware settings button? I want to write
Possible Duplicate: MySQL get row position in ORDER BY I have a table of
I want to insert a char into every possible position of s string except
Is it possible to position an element within a relative positioned container absolute to
Possible Duplicate: jquery animate background position For a animated hover effect I try to
Is it possible to position one element relative to another specific element using only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.