-2

Why set body background expicitly to white? As in:
body { background: #fff }

Isn't browsers' default background color white anyway?
It seems to be an extremely common practice yet I can't find an answer.

And yes, I know that users can use their own CSS which means they can also override the white, which defeats the purpose of setting it white 'just in case'.

mhpreiman
  • 139
  • 2
  • 4
  • 13
  • 4
    I don't think that's a common practice in terms of fixing anything, they're probably just being explicit. – Tallboy Feb 11 '18 at 19:43
  • 1
    You can't count on everyone not changing their browser defaults. – j08691 Feb 11 '18 at 19:44
  • 1
    the good practise is to never rely on default ... as one day they may change, so better rely on true things. When you set it white it will always be white but the default one you don't know – Temani Afif Feb 11 '18 at 19:47
  • 2
    For an 'extremely common practice' I've never done that once – Sterling Archer Feb 11 '18 at 19:47
  • `I know that users can use their own CSS which means they can also override the white` you style can be placed the last one and override all of them ;) unless there is more specific styles – Temani Afif Feb 11 '18 at 19:49
  • @Tallboy I'm not sure what you mean by fixing. I mean that take a look at Facebook or even Stackoverflow. They have expicitly set body background to #fff. But why? – mhpreiman Feb 11 '18 at 19:49
  • @TemaniAfif By the same logic we can't be sure that some other CSS rules won't break in the future. If the body color standard might change (I can't see why) then why not set every other rule expicitly? – mhpreiman Feb 11 '18 at 19:54
  • yes same logic apply to other Style :) like defaut font-sizes, margin, padding, etc etc ... and all are handled by modern frameworks like bootstrap. We always override all the defaults style to be sure we have something accurate that works everywhere – Temani Afif Feb 11 '18 at 19:56
  • @TemaniAfif But about the overriding user stylesheet? Wouldn't that be considered a bad practice? Akin to scrolljacking which seems to be really hated because it forces user mouse to act differently from default. If user wants to have their style in a certain way why force them to use yours? – mhpreiman Feb 11 '18 at 20:00
  • you are not forcing you are creating you own website, your own territory where you decide about rules :) if you want your website to have a custom cursor you are free, if you want to override default style or not your are free ... then it will be a question of UI/UX, design, ergonomy, accessibility, etc – Temani Afif Feb 11 '18 at 20:03
  • @Teman I meant [scrolljacking](https://envato.com/blog/scroll-hijacking/) (not to be confused with cursor). By the same rule I could make the worst website and tell people that they shouldn't criticise because it's _my site_. If I want to create a site for users then wouldn't it be important to consider their comfort (instead of forcefeeding everything)? – mhpreiman Feb 11 '18 at 20:13
  • @mhpreiman quite simply you don't control or even know EVERY default stylesheet, in every device, in every language, in every country of the planet. That is just absurd to think that something, somewhere, out there doesn't do something weird (say, a light gray background). They're just being explicit like I said in my original answer. – Tallboy Feb 11 '18 at 20:14
  • @Tallboy Yet if my site is for the local area only? Same as I wouldn't make a website supporting IE6 if my fixed target group wasn't people who used IE6? – mhpreiman Feb 11 '18 at 20:17
  • I don't know what you're asking. You should look for reasons to be more supportive of your userbase, not less. – Tallboy Feb 11 '18 at 20:19
  • @Tallboy Well that's what I believe. Therefore also not forcing them to use my stylesheet if they really want to override it with theirs. – mhpreiman Feb 11 '18 at 20:22
  • `then wouldn't it be important to consider their comfort (instead of forcefeeding everything)?` --> as i said, this called UX/UI, ergonomy, accessibility, etc :) You need to focus on this and not the default – Temani Afif Feb 11 '18 at 20:22
  • I still don't know what you're saying, I think you're overcomplicating it. You asked why websites like SO explicitly state `#fff` on the body even though its the browser default. It's because SO doesn't want to rely on default "fallback" styling of browsers, on the off chance (say 1% chance) that some device out there doesn't do it... perhaps some crappy android phone or something. There's nothing having to do with "comfort" or "forcing" or anything like that. It is what it is. – Tallboy Feb 11 '18 at 20:26

2 Answers2

3

This is somewhat subjective, but here are a few reasons you might want to do that.

There is no standard background color for a page, even though pretty much all browsers will use white. It is generally a good idea not to rely too heavily on defaults in case they change. While it is unlikely that this specific case will change, some people will avoid exceptions and never rely on defaults. EDITED: Modern Firefox actually lets you pick the default color for pages. I'm now using it to view markdown-generated HTML1 files and text files in white-on-black. So a site not specifying background color but specifying text color would be black on black for me.

Also, as the Zen of Python says: explicit is better than implicit. Whether you agree or disagree is up to you. When I look at the CSS for the html and body elements, I like seeing background. It makes it obvious. I don't have to ask myself if there's another place where the background might be set. But then again, this is subjective. Others will say it makes the code longer to read for no reason.

A much more plausible explanation is that big websites tend to use another language to make their stylesheets and then compile them to CSS. They use variables for most colors so that the palette can be easily altered without having to search and replace the color values everywhere manually. They'll extract the site's background color to a variable as well. I do that very often, but the result is that if I settle for a white background, the compiled CSS will include background: #fff;. Why specifically #fff? Because the compiler prefers using as little characters as possible to represent a color, and white would be longer.

Domino
  • 6,314
  • 1
  • 32
  • 58
  • 1
    Just to add on to this a bit, back in the day the default background color for web pages was #808080, not white. – Daniel Beck Feb 24 '18 at 18:37
  • Stack Overflow is not the place for subjective questions so subjective answers should not be made either. You waste your time on questions like this which are, now, closed and soon deleted. – Rob Mar 12 '18 at 03:36
  • 1
    @Rob I am aware of such practice, but in my own judgement the compiler hypothesis was worth mentioning. I wouldn't delete the question, for in the process it actually thought me something useful: not specifying a default background and font color can screw with Firefox users who customize defaults. In fact I recently had this exact problem with xkcd which doesn't set color: black. – Domino Mar 13 '18 at 17:35
-1

There is no default background color for browser. One can develop a browser with even pink or black background as default.

Marcin Szwarc
  • 569
  • 3
  • 13