-3

Relevant JSFiddle - http://jsfiddle.net/Efqda/

CSS -

.social img {
    width: 128px;
    height: 128px;
    opacity: .5;
}

.social img:hover {
    opacity: 1.0;
}

.social #facebook img {
    content: "https://raw.github.com/danleech/simple-icons/master/icons/facebook/facebook-128-black.png";
}

HTML -

<a href="facebook.com"><img class="social" id="facebook"></a>

How do I get the image to show up? I don't get any errors in the console.

Narabhut
  • 839
  • 4
  • 13
  • 30

7 Answers7

4

To fix your code, first of all you would need to fix the selectors. For example, .social img means every <img> element which is a child of another (not the same) element with class social. Also all id's must be unique so there is no need for .social #facebook, .#facebook is just enough.

However, this approach is flawed from the outset because using CSS to set the src of an img (content: url("http://....");) only works in Chrome (and other WebKit browsers).


You should use the following approach instead:

a[href="http://facebook.com"] {
    background: url("https://raw.github.com/danleech/simple-icons/master/icons/facebook/facebook-128-black.png") no-repeat;
    width: 128px;
    height: 128px;
    opacity: .5;
    display: inline-block;
}
a[href="http://facebook.com"]:hover{
    opacity: 1.0;
}

with this HTML markup:

<a href="http://facebook.com"></a>

Fiddle here

See also:

Community
  • 1
  • 1
Petr R.
  • 1,247
  • 2
  • 22
  • 30
  • 1
    This is now a reasonable answer. I zapped the first set of code because nobody should ever use it, since it only works in WebKit browsers and is overall a bad idea. Feel free to revert if you disagree. – thirtydot Aug 03 '13 at 17:22
  • @thirtydot - thanks for the cleanup of my post, there really is no need to post a code that doesn't work in all browsers. BTW, I'm not a native speaker so thanks again for the correction of the text. – Petr R. Aug 04 '13 at 10:14
1

use the following

 img.social {
    background :url(/facebook-128-black.png) no-repeat;
    width: 128px;
    height: 128px;
    opacity: .5;
}
bhai
  • 305
  • 3
  • 9
  • 19
1

Couple of issues, your CSS selector is wrong, if you have an id then just use that, also you need to specify that the content comes from an external source using url()

#facebook {
    content: url("https://raw.github.com/danleech/simple-icons/master/icons/facebook/facebook-128-black.png");
}
omma2289
  • 54,161
  • 8
  • 64
  • 68
0
img.social {
  width: 128px;
  height: 128px;
  opacity: .5;
  background-image:url(https://raw.github.com/danleech/simple-icons/master/icons/facebook/facebook-128-black.png);
}

Also your link href is wrong.. it should be

<a href="http://www.facebook.com">
   <img class="social" id="facebook">
</a>
Naveen Kumar Alone
  • 7,536
  • 5
  • 36
  • 57
Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56
0

Here is JSFiddle

Please change your .social #facebook img {...} as #facebook {...}

Because you have to set image url to img tag id

And also change contect:... to content:url("");

Because you need to set content url from where you have to get image.

Like

#facebook {
  content: url("https://raw.github.com/danleech/simple-icons/master/icons/facebook/facebook-128-black.png");
}
Naveen Kumar Alone
  • 7,536
  • 5
  • 36
  • 57
0

With css you just can to set bacground image.

With js you can set src attribute.

Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143
0

Your Issue was that you need to enclose your image URL inside url() in content and linking of CSS. Either use Class or ID but not both.

According to your style sheet .social #facebook img

there is a class social inside that there is an element with id facebook and inside an element img.

here you can see an example matching your CSS style.JSFiddle

and here is a straight forward solution JSFiddle

You can also use in-line CSS

<a href="facebook.com">
    <img class="social" id="facebook" style="content: url(https://raw.github.com/danleech/simple-icons/master/icons/facebook/facebook-128-black.png&quot;);"/>
</a>
Ayush
  • 3,989
  • 1
  • 26
  • 34