Why does CSS work with fake elements?

前端 未结 19 729
野的像风
野的像风 2020-11-28 01:21

In my class, I was playing around and found out that CSS works with made-up elements.

Example:

相关标签:
19条回答
  • 2020-11-28 01:41

    There are a few things about the other answers that are either just poorly phrased or perhaps a little incorrect.

    FALSE(ish): Non-standard HTML elements are "not allowed", "illegal", or "invalid".

    Not necessarily. They're "non-conforming". What's the difference? Something can "not conform" and still be "allowed". The W3C aren't going to send the HTML police to your home and haul you away.

    The W3C left things this way for a reason. Conformance and specifications are defined by a community. If you happen to have a smaller community consuming HTML for more specific purposes and they all agree on some new Elements they need to make things easier, they can have what the W3C refers to as "other applicable specifications". (this is a gross over simplification, obviously, but you get the idea)

    That said, strict validators will declare your non-standard elements to be "invalid". but that's because the validator's job is to ensure conformance to whatever spec it's validating for, not to ensure "legality" for the browser or for use.

    FALSE(ish): Non-standard HTML elements will result in rendering issues

    Possibly, but unlikely. (replace "will" with "might") The only way this should result in a rendering issue is if your custom element conflicts with another specification, such as a change to the HTML spec or another specification being honored within the same system (such as SVG, Math, or something custom).

    In fact, the reason CSS can style non-standard tags is because the HTML specification clearly states that:

    User agents must treat elements and attributes that they do not understand as semantically neutral; leaving them in the DOM (for DOM processors), and styling them according to CSS (for CSS processors), but not inferring any meaning from them

    Note: if you want to use a custom tag, just remember a change to the HTML spec at a later time could blow your styling up, so be prepared. It's really unlikely that the W3C will implement the <imsocool> tag, however.

    Non-standard tags and JavaScript (via the DOM)

    The reason you can access and alter custom elements using JavaScript is because the specification even talks about how they should be handled in the DOM, which is the (really horrible) API that allows you to manipulate the elements on your page.

    The HTMLUnknownElement interface must be used for HTML elements that are not defined by this specification (or other applicable specifications).

    TL;DR: Conforming to the spec is done for purposes of communication and safety. Non-conformance is still allowed by everything but a validator, whose sole purpose is to enforce conformity, but whose use is optional.

    For example:

    var wee = document.createElement('wee');
    console.log(wee.toString()); //[object HTMLUnknownElement]
    

    (I'm sure this will draw flames, but there's my 2 cents)

    0 讨论(0)
  • 2020-11-28 01:42

    Actually you can use custom elements. Here is the W3C spec on this subject:

    http://w3c.github.io/webcomponents/spec/custom/

    And here is a tutorial explaining how to use them:

    http://www.html5rocks.com/en/tutorials/webcomponents/customelements/

    As pointed out by @Quentin: this is a draft specification in the early days of development, and that it imposes restrictions on what the element names can be.

    0 讨论(0)
  • 2020-11-28 01:43

    CSS is a style sheet language that can be used to present XML documents, not only (X)HTML documents. Your snippet with the made-up tags could be part of a legal XML document; it would be one if you enclose it in a single root element. Probably you already have a <html> ...</html> around it? Any current browser can display XML documents.

    Of course it is not a very good XML document, it lacks a grammar and an XML declaration. If you use an HTML declaration header instead (and probably a server configuration that sends the correct mime type) it would instead be illegal HTML.

    (X)HTML has advantages over plain XML as elements have a semantic meaning that is useful in the context of a web page presentation. Tools can work with this semantics, other developers know the meaning, it is less error prone and better to read.

    But in other contexts it is better to use CSS with XML and/or XSLT to do the presentation. This is what you did. As this wasn't your task, you didn't know what you were doing, and HTML/CSS is the better way to go most of the time you should stick to it in your scenario.

    You should add an (X)HTML header to your document so tools can give you meaningful error messages.

    0 讨论(0)
  • 2020-11-28 01:47

    I think made-up tags are just potentially more confusing or unclear than p's with IDs (some block of text generally). We all know a p with an ID is a paragraph, but who knows what made-up tags are intended for? At least that's my thought. :) Therefore this is more of a style / clarity issue than one of functionality.

    0 讨论(0)
  • 2020-11-28 01:49

    ...I simply change all of my made up tags to paragraphs with ID's.

    I actually take issue with his suggestion of how to do it properly.

    1. A <p> tag is for paragraphs. I see people using it all the time instead of a div -- simply for spacing purposes or because it seems gentler. If it's not a paragraph, don't use it.

    2. You don't need or want to stick ID's on everything unless you need to target it specifically (e.g. with Javascript). Use classes or just a straight-up div.

    0 讨论(0)
  • 2020-11-28 01:50

    Why doesn't he want you to use them? They are not common nor part of the HTML5 standard. Technically, they are not allowed. They are a hack.

    I like them myself, though. You may be interested in XHTML5. It allows you to define your own tags and use them as part of the standard.

    Also, as others have pointed out, they are invalid and thus not portable.

    Why didn't he know that they exist? I don't know, except that they are not common. Possibly he was just not aware that you could.

    0 讨论(0)
提交回复
热议问题