question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a default background and color on :root?

See original GitHub issue

So I wanted to suggest this because as it turns out, some browsers don’t set the body’s background to #FFFFFF or its colour to #000000 by default, this can happen on some browser themes or if the user set their own colours.

And this breaks many many websites, but many people who use settings and themes like that blame it on the devs of the website.

Since not a lot of devs are aware of this and yet most people would want to have a reliable background and text colour, maybe it would be a good idea to add

:root {
    background: #FFF;
    color: #000;
}

Since :root has the lowest CSS priority, it should not break anything, most people apply the background on html or body anyway.

I don’t wanna debate on if those people are right or wrong to change their default background and text colour, there is also no actual HTML or CSS standards saying browsers should set the background to a certain colour as far as I’m aware, so it’s not like they’re 100% wrong.

I am not sure if this is in the minimalistic scope of your project, I do feel like it is because it is a very elemental reset that you could argue would help many websites out there. And that, if more people were aware of this, everyone would start their project by resetting the background and text colour.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:7
  • Comments:17

github_iconTop GitHub Comments

1reaction
sammducommented, Apr 6, 2019

@Pizzacus I decided to test it this way because I tried to use it on a login modal I was working on, and the login modal did not take up the whole screen obviously, then the background started acting weird.
To respond to your comment, this is what I mean by avoiding to take a position with minireset. By keeping clear of more aggressive defaults, this stylesheet can remain “mini” and “inclusive”.
However, I appreciate your dedicated efforts to get this right.

0reactions
Pizzacuscommented, Apr 6, 2019

Ah, I see, the reason why that is is probably that the :root element takes the background of <body> if no other background is set.

So my next question was “is this a standardised thing”, and the answer is yes.

https://drafts.csswg.org/css-backgrounds-3/#body-background

3.11.2. The Canvas Background and the HTML <body> Element

For documents whose root element is an HTML HTML element or an XHTML html element [HTML]: if the computed value of background-image on the root element is none and its background-color is transparent, user agents must instead propagate the computed values of the background properties from that element’s first HTML BODY or XHTML body child element. The used values of that BODY element’s background properties are their initial values, and the propagated values are treated as if they were specified on the root element. It is recommended that authors of HTML documents specify the canvas background for the BODY element rather than the HTML element.

There is, unfortunately, no way to replicate this behaviour in CSS while setting default values… If we set a default on body then there will be problems if someone tries to set it on html, if we set it on html then there may be some problems with setting it on body

This has nothing to do with the use of pseudo-elements, :root is just the same as writing html.


But the real question is is this acceptable? I’m really not sure, I do think that setting a default on :root or html is the best, it’s still less likely to break stuff, even if you found an instance where it does, since normally, the body element will take the whole screen, so it doesn’t matter what colour the :root is any more.

But yeah, perhaps the best solution would be to petition W3C to make white the standard background xD different colours always break websites and yet browsers still use them because… reasons!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Default background color of SVG root element - Stack Overflow
Save this question. Show activity on this post. I'd like to set a default background color for the entire SVG document, to red...
Read more >
React Native - Set RootView Background Color - Josh Buchea
In the RootView of a React Native app, the default background color is white. We can change this to another color by following...
Read more >
XSETROOT - X.Org
Use ''color'' as the foreground color. Foreground and background colors are meaningful only in combination with -cursor, -bitmap, or -mod. -bg color. Use...
Read more >
Customizing Colors - Tailwind CSS
Customizing the default color palette for your project. ... palette with your own custom colors, add your colors directly under the theme.colors section...
Read more >
root - CSS: Cascading Style Sheets - MDN Web Docs - Mozilla
In HTML, :root represents the element and is identical to the selector html ... of the document: <html> in the case of HTML...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found