关于 * {margin:0; 的困惑填充:0;}
在我读过的一些文章中,使用 * {margin:0; padding:0;}
不鼓励使用,因为它会影响网站的性能。所以我转向了 reset.css
样式表。
但我想知道,它对性能有何影响?
In some articles that I've read, the use of * {margin:0; padding:0;}
is discouraged as it would affect the web site's performance. So I turned to a reset.css
stylesheet.
But I'm wondering, how does it affect the performance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这篇 Eric Meyer 帖子讨论了其背后的原因。
也就是说,下面的图表来自 Steve Souders 的这篇文章展示了使用通用选择器的测试页面加载时间的差异 与使用后代选择器的页面相比。
The reasoning behind this was discussed in this Eric Meyer post.
That said, the following chart from this Steve Souders post shows the difference in load times for a test page using universal selectors compared with a page using descendant selectors.
它会影响性能,因为浏览器引擎必须将这种样式应用于页面上的每个元素,这将导致大量渲染,特别是在具有大量元素的大页面中,并且这种方法也是一种不好的做法,因为它可能会消除良好的效果某些元素的默认样式,
您可以通过缩小代码的范围来优化此代码,例如仅在某些导致出现问题的元素上使用它
h1,ul
{
保证金:0;
填充:0;
}
it is effect the performance because the browsers engine have to apply this style to every element on the page this will lead to heavy rendering specially in the big pages with a lot of elements and this method is a bad practice too because it may remove a good default styles for some elements
you may optimize this code by reduce the scope of it like using it on just some elements that make the problems like this
h1,ul
{
margin:0;
padding:0;
}
在
stylesheet
中使用*{margin:0;padding:0;}
不会影响性能,并且有助于解决各种格式问题。使用单独的
reset.css
确实会产生一些性能问题,因为您强制用户从服务器请求另一个文件。从长远来看,高速线路上的几 kb 根本不算什么。但对于某人在移动浏览器上的另一个文件来说可能太多了。Using
*{margin:0;padding:0;}
in yourstylesheet
will not affect performance and is helpful in tackling various formatting issues.Using a separate
reset.css
does have some performance issues, as you are forcing the user to requested one more file from the server. In the grand scheme of things, a few kb on a high speed line is nothing. But another file for someone on a mobile browser can be too much.我认为您读到的网站需要检查一下,重置样式表是公平竞争环境的方法。开销非常小,不会产生任何影响,尤其是对于现代浏览器。
I think the website you read that on needs its head checked, a reset style sheet is the way to go to level the playing field. The overhead is so marginal it won't make a difference especially with modern browsers.
它会影响网页显示,因为如果不使用它,我们就必须进行
诸如替换之类的操作,以避免网页左侧和顶部出现狭窄的白色条带。
It effects the webpage display because without its use we have to
etc. like substitutions to avoid a narrow white strip on the left and top of the webpage.