开发者

With CSS properties, which is more work for the browser?

I'd li开发者_开发技巧ke to know which of the ways to do this syntax is more work for the browser:

First:

margin: 0;

Second:

margin-top: 0;
margin-right: 0;
margin-bottom: 0;
margin-left: 0;

Third:

margin: 0 0;

And finally, the same questions except with 0px;.


In my opinion you should specify a unit like 0px all the time even when you're using 0, to avoid forgetting to add the unit on when you change the value to something non-zero, but that's a matter of personal preference.

As for the browser, those are all equivalent. It takes negligible time for the browser to understand any of those relative to the download time of the css. For that reason margin: 0px; is the best because it is only 12 bytes compared to the longest version you posted which (with `px) units added on) is 69 bytes so nearly 6x the amount of data to download.

60 bytes isn't much anyways, but if you repeat that a lot, then when it's down to the wire, the shorter syntax is faster. It's also nicer to read :)


This may not be the answer you're looking for, but I feel that...

This is SOOOO inconsequential as far as computer performance goes. What matters much, much more is readability and maintainability. Which variation do you think would be easiest to maintain and understand for someone your company hires off the street? That's the one you should choose.


This depends entirely on the browser's internal implementation. It's most likely for any given browser that the more verbose definitions will be very slightly more work simply because there's more text to parse. In practice, though, CSS engines are really blazingly fast; none of these is enough work to matter for processing time.


The "work" would be negligable. I guess, technically, the second one would be more work since it would have to parse 3 more lines instead of just 1. The first one is how I would suggest doing it.

Personally, I always do:

margin: 0px 0px 0px 0px;

Just to help if I decide to change something in the future.

Its really not important, though. There is a negligible difference between all of them


I have wondered this too, for performance reasons.

If maximum performance is your concern - supposing there's no major difference for the browser - then margin: 0 would help to keep the weight of your CSS file down overall.

If you work with CSS a lot mentally extrapolating margin: 0; to longhand or even margin: 4px 0 3px; should not be much of a leap. Why waste the time typing and the character weight?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜