8

I need to basically set the content of something with HTML from CSS. I'm currently doing the following:

.myclass {
    content "<img src=\"hello.png\"/>";
}

However, instead of the image, I see the literal text:

<img src="hello.png"/>

How can I inject arbitrary HTML using CSS?

Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411

5 Answers5

15

HTML stores the data, and is the Model
CSS stores the styles, and is the View
JS stores the interactions, and is the Controller

If you're trying to add data to the page, it should be done via HTML. If you're simply trying to add an image as a style, use the background-image property. You don't need to inject an <img> element in the page to do that.

Don't ever do this, ever

As far as being able to inject HTML into the page via CSS, it's not directly possible, however it's possible to add JavaScript into the page using CSS, which can then add HTML to the page.

I can't emphasize enough how wrong that approach would be.

Community
  • 1
  • 1
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
7

Unless there is some strange hack that I am not aware of, this cannot be done with pure CSS.

The content property is only able to insert text; if you try to put in HTML, it will be escaped.

That said, you can do something like this with pure CSS:

enter image description here

This is the CSS that can perform that effect:

.myClass:before {
  display: inline-block;
  width: 32px;
  height: 32px;
  content: "";
  background-image: url("img.gif");
}

You can see this in action on this jsFiddle page.

Peter Olson
  • 139,199
  • 49
  • 202
  • 242
4

In this particular case, you can use a pseudo-class (eg ::before), background-image, display:block and a fixed width and height to show the image.

Also, make sure that the colon : is added between content and its value.

A relatively new concept at the horizon is the element() value for backgrounds. This will display HTML as if it were an image: See also -moz-element.

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Unfortunately, I'm working with print media and so I can't use the `:before` selector on my `@page @top-left` selector, I've tried :( – Naftuli Kay Mar 16 '12 at 16:11
  • @TKKocheran Another exotic approach: Add a negative left margin to your element, and a positive left padding. Then add the background image on the element *itself*. Make sure that you use `background-repeat:no-repeat` to show the image only once. – Rob W Mar 16 '12 at 16:19
  • The [evil button example](https://developer.mozilla.org/en-US/docs/Web/CSS/element#A_somewhat_more_bizarre_example) looks dangerous but is actually [harmless](https://www.w3schools.com/code/tryit.asp?filename=G5026YYGWOTV). – Cees Timmerman Jun 21 '19 at 12:05
1

This can be done. For example with Firefox

css

#hlinks
{
  -moz-binding: url(stackexchange.xml#hlinks);
}

stackexchange.xml

<bindings xmlns="http://www.mozilla.org/xbl"
  xmlns:html="http://www.w3.org/1999/xhtml">
  <binding id="hlinks">
    <content>
      <children/>
      <html:a href="/privileges">privileges</html:a>
      <html:span class="lsep"> | </html:span>
      <html:a href="/users/logout">log out</html:a>
    </content>
  </binding>
</bindings>

ref 1

ref 2

Community
  • 1
  • 1
Zombo
  • 1
  • 62
  • 391
  • 407
  • [`-moz-binding`](https://developer.mozilla.org/en-US/docs/Web/CSS/-moz-binding) is deprecated since Gecko 57 (Firefox 57 / Thunderbird 57 / SeaMonkey 2.54) – Cees Timmerman Jun 21 '19 at 11:55
0

You can't. It's not what it's for. The CSS is for the presentation layer while the HTML is the data layer.

Actually, you can, but just for characters, not HTML. You can use the content property. With some CSS selectors like :before, you can do nice stuff like adding your own character as a bullet for your list. But not much more.

Florian Margaine
  • 58,730
  • 15
  • 91
  • 116