Why is it impossible to change content in css?

前端 未结 3 651
猫巷女王i
猫巷女王i 2021-01-13 14:41

CSS2.1 pseudo-selectors such as ::after and ::before allows to add text content to the page. For example :

CSS



        
3条回答
  •  别那么骄傲
    2021-01-13 15:19

    As specified by the spec content: is only usable together with the *pseudo-elements :before and :after, there isn't a better answer than that I'm afraid.

    12.2 The 'content' property

    This property is used with the :before and :after pseudo-elements to generate content in a document.


    However, if the question should be interpreted as why aren't we allowed to add content anywhere we like using CSS? The answer is only theoretical but as simple:

    CSS is not meant to be a way to provide new content, it is a way to format existing such.

    "You are not supposed to change any content using CSS, there is not that much more than that to it. The :before and :after pseudo-elements have been discussed quite frequently because they break the barrier between formatting and content." - refp


    :before/:after are a great resources because they allow you to format your data in a way that wasn't possible before their appearance. Notice that I use the word "format" because I still consider it to be a formatter, not a data generator.

    Coming up with a usable alternative that would still have a clear line between formatting and content would've been very hard (and frustrating), so this is (at least according to me) an acceptable middle-way which I can live it.

    I must admit that I wasn't a big fan of the pseudo-elements in question when they first appeared.

提交回复
热议问题