2

Is there a CSS hack/technique to take an <img> element (with no other markup) and hide the pixel content of the image while still displaying it as an element with background color and stroke?

For example, take an <img src="foo.jpg"> on the page and make it a 32x32 badge of solid color.

Though I am interested in browser-specific hacks (does Webkit have a solid-fill effect?) or CSS3 awesomesauce (is there an image-content-opacity:0.0?), I need a solution that works on IE8+, FF4+ and thereabouts.

If you are interested in the motivation for this question, see the edit history of this page. It has been removed because it was distracting users into helpfully trying to find workarounds to solve that problem instead of answer this question.

18
  • 1
    The sample could be done with just CSS without an image at all. Make the squares divs with different background colors. Commented Jun 4, 2012 at 23:29
  • Possible alternative option: Make the the image a transparent GIF and change the background color of the img. I'm not sure you CAN assign a background color to an image, though. Commented Jun 4, 2012 at 23:30
  • 1
    @DA I suppose I did not stress strongly enough: if this is to work there cannot be any changes to the markup whatsoever. Certainly if I can change the markup I can do this in a heartbeat. The question is (and I'll edit above to be super bold clear about this): can this be done with the existing markup? Commented Jun 4, 2012 at 23:36
  • Is the 'bar' the teal, the 'a' the border and 'img' the color? If so, you wouldn't need the img, so you could hide it via CSS, then use the 'a' with both a border and background color. Commented Jun 4, 2012 at 23:53
  • I came up with this using your markup unchanged: jsbin.com/opored but I'm not exactly sure what you intend each element to be. Commented Jun 5, 2012 at 0:03

2 Answers 2

1

Hide the image and use the background filled with a solid color.
Example here http://jsfiddle.net/notme/ZUvHN/6/

Sign up to request clarification or add additional context in comments.

3 Comments

This sadly fails the requirement of "The number of items is arbitrary but should distribute vertically throughout the #foo."
@phrogz just get rid of the height on #foo
@DA Not an option :p. See my latest comment in the question (to thirtydot) for why the fixed height is important.
1

This is a take off of my comments above and notme's solution:

http://jsfiddle.net/ZUvHN/7/

What I did was I removed display:table-cell from the a and then set it to display: block

I then set the img to display: none

This lets you then apply the border and background styles to the a tag instead of the img tag. You'll likely have to tweak the margins and spacing a bit.

I don't know if you have the option or not, but it might be easier to tweak the HTML a bit via JavaScript.

1 Comment

+1 for getting closer, but as you can see from this using vertical-align:middle does nothing to center each dot within the "cell" without that style being applied specifically to a display:table-cell element. No acceptance mark because this doesn't actually solve the question asked in the title and first paragraph of this question (separate from the reason I happen to want it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.