Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style 标签只能放在 head 里面吗?为什么? #15

Open
liangbus opened this issue Dec 3, 2019 · 0 comments
Open

style 标签只能放在 head 里面吗?为什么? #15

liangbus opened this issue Dec 3, 2019 · 0 comments
Labels

Comments

@liangbus
Copy link
Owner

liangbus commented Dec 3, 2019

前阵子面试遇到一个面试题是就是问这个,当时没答好,当时我的回答是,为了初始化(reset)标签样式之类的 ,当时就感觉到并不是面试官想要的,回来找了好一阵子都没有翻到想要的答案,后来上 Stackoverflow 才找到像样的答案
Does <STYLE> have to be in the of an HTML document?

其中最高赞回答中说了,稍微简单的翻译了一下,意思大致就是这样

style is supposed to be included only on the head of the document.

Besides the validation point, one caveat that might interest you when using style on the body is the flash of unstyled content. The browser would get elements that would be styled after they are displayed, making them shift on size/shape/font and/or flicker. It is generally a sign of bad craftsmanship. Generally you can get away with putting style anywhere you want, but try to avoid it whenever it is possible.

style 标签应该只放置在文档的 head 标签中
除了验证相关的点外,当你在 body 中使用 style 标签会使得未样式化的内容出现闪烁,这样的警示也许会引起你的注意。浏览器会把这些已经展示出来的元素再结合相应的样式渲染,使得它们可能会在大小、形状、字体这些方面切换或者闪烁。通常这并不是一个很好的做法。然而你可以把你的 style 标签放置在任何你想要的地方,但并不推崇这样的做法。

@liangbus liangbus added the html label Dec 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant