Are overlapping HTML formatting tags always a bad practice?

前端 未结 2 787
天涯浪人
天涯浪人 2021-01-23 03:35

Suppose I need to have a text with a bold and an italic part that overlap each other, like you see this sentence have.

Now, I know the proper w

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-23 04:12

    As of HTML5, the procedure for dealing with these "overlapping tags" is standardized: https://html.spec.whatwg.org/multipage/syntax.html#adoption-agency-algorithm

    When parsing

    I want this text to have both bold and italic

    we'll encounter the following step when we get to the tag:

    1. If formatting element [here, the element started with ] is not the current node [here, the element], this is a parse error. (But do not abort these steps.)

    The specification goes on to close the and and re-open a new in this case. So while you do get a "parse error," this markup will still do what you want, according to the standard.

    So why might it be a bad practice?

    I'll try to stick with objective facts.

    This automatic fixup is limited to closing up to 8 levels of formatting tags (see step 3 in the algorithm). Because of that, it's not appropriate as a general technique.

    Also, elsewhere in the spec, https://html.spec.whatwg.org/multipage/syntax.html#parse-error

    ... user agents, while parsing an HTML document, may abort the parser at the first parse error that they encounter for which they do not wish to apply the rules described in this specification.

    Browser vendors are free to stop the parser if they don't want to deal with this situation.

提交回复
热议问题