via定制html和css,Injecting HTML via CSS

问题

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

.myclass {

content "%5C%22hello.png%5C%22";

}

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

hello.png

How can I inject arbitrary HTML using CSS?

回答1:

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 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.

回答2:

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:

7eadb7201c98a27c6e0cea4a9a5c9c64.png

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.

回答3:

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.

回答4:

This can be done. For example with Firefox

css

#hlinks

{

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

}

stackexchange.xml

xmlns:html="http://www.w3.org/1999/xhtml">

privileges

|

log out

ref 1

ref 2

回答5:

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.

来源:https://stackoverflow.com/questions/9740134/injecting-html-via-css

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值