Loading an external font via inline CSS

后端 未结 4 1857
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 10:41

Is it possible to load an external font via inline CSS?

Note: I\'m not talking about using an external CSS file with a @font-face definition, b

4条回答
  •  旧时难觅i
    2020-11-28 11:38

    You cannot include a @font-face rule in a style attribute (which is “inline CSS” in the most narrow sense). By the HTML 4.01 specification, you cannot include such a rule inside the body element at all (“inline CSS” in a broader sense, which includes style elements). But in practice it is possible.

    In practice, if you include a style element inside body, it will be processed by browsers just as if it were in the syntactically correct place, i.e. inside the head element. It even works “backwards”, effecting elements appearing before it.

    You can even make this approach – which should be used only if you cannot change the head – formally correct as per HTML5 CR. It allows a style element at the start of any element with flow content as its content model. Current browsers ignore the scoped attribute.

    Update: the following is not relevant any more, since the validator bug has been fixed.

    However, there is a bug in the W3C Markup Validator and in validator.nu: they disallow style at the start of body. To overcome this bug, and to make your document validate in addition to being valid, you can use an extra div element:

    
    

提交回复
热议问题