Detectar dark-mode en browsers

16/Sep/2020 337

Hace ya varios meses que diferentes sistemas operativos y aplicaciones traen como opcional el dark-mode, donde todo lo claro se ha vuelto oscuro. ¿Moda, accesibilidad? Realmente no he indagado, pero eso trae pequeños problemas en cuanto al uso de navegadores web. Me topé con uno en específico que me llevó a escribir este artículo.

Hechos:

Navegando por Internet y con varios enlaces abiertos, veo que un favicon de color negro apenas se nota en el tab de color gris oscuro.

Detectar dark-mode en browsers

Pequeño problema pero solucionable. Hay 2 técnicas para detectar el dark-mode en el browser: con CSS y con JS. Veamos ambos códigos:

CSS:

/* Dark-mode */
@media (prefers-color-scheme: dark) {
    body {
        background-color: black;
        color: white;
    }
}
/* Light-mode */
@media (prefers-color-scheme: light) {
    body {
        background-color: white;
        color: black;
    }
}

CSS Dark-mode

JS:

Para hacerlo con JavaScript debes detectar el mismo @media (prefers-color-scheme: dark) para luego retornar true/false y hacer lo que quieras con el DOM:

window.matchMedia('(prefers-color-scheme: dark)').matches

Por ejemplo, en el siguiente código cambiamos el favicon.ico si tiene el modo dark habilitado:

const favicon = document.querySelector('#favicon')
if (window.matchMedia && 
    window.matchMedia('(prefers-color-scheme: dark)').matches) {
  favicon.href="favicon-dark.ico"
}

Dark-mode JS

Links:

Comentarios

  1. http://www.iherb.com/search?kw=questbars [#]

    Thank you, I have just been looking for info about this topic for a
    long time and yours is the greatest I’ve came upon till now.

    But, what in regards to the conclusion? Are you positive in regards to the source?

    quest bars https://www.iherb.com/search?kw=quest%20bars quest
    bars

  2. FelipeHaw [#]

    viagra directions
    what is viagra used for viagra cheap low price cialis canadian viagra online pharmacy canada medication viagra doses costco online buy cialis online buy medication online canadian pharmacy.com
    buy medication online cialis canada buy cialis online canada pharmacy buy viagra internet pharmacy viagra

  3. hydroxychloroquineMt [#]

    hydroxychloroquine warnings chloroquine us chloroquine phosphate vs hydroxychloroquine

  4. tinyurl.com [#]

    Nice post. I learn something new and challenging on blogs I stumbleupon every day.
    It will always be interesting to read through articles from other writers
    and practice a little something from their web sites. scoliosis surgery https://0401mm.tumblr.com/ scoliosis surgery

  5. http://bit.ly [#]

    Wow that was unusual. I just wrote an extremely long
    comment but after I clicked submit my comment didn’t show up.

    Grrrr… well I’m not writing all that over again. Anyhow, just wanted to say wonderful blog!
    cheap flights http://1704milesapart.tumblr.com/ cheap flights

  6. off white hoodie [#]

    My husband and i have been very ecstatic that Raymond could conclude his investigations via the ideas he obtained through your web page. It is now and again perplexing just to find yourself giving out information and facts other people may have been making money from. So we keep in mind we have got you to be grateful to for this. The most important illustrations you have made, the simple website menu, the relationships you will make it possible to instill – it’s everything powerful, and it’s aiding our son and us reckon that the content is excellent, which is incredibly indispensable. Thanks for the whole lot!

  7. curry 8 [#]

    My wife and i felt now relieved when Chris could round up his analysis through the entire precious recommendations he came across in your site. It is now and again perplexing just to be freely giving tips which often people today could have been making money from. So we see we now have you to thank because of that. The type of illustrations you’ve made, the simple blog menu, the relationships you aid to foster – it’s got most astonishing, and it is assisting our son and the family reason why the situation is brilliant, and that’s rather indispensable. Thanks for all the pieces!

  8. supreme hoodie [#]

    Needed to send you one tiny note just to give many thanks again on the breathtaking knowledge you have featured here. It was really particularly generous of people like you to grant publicly all that numerous people would’ve distributed as an e book in order to make some dough for themselves, most importantly seeing that you could have done it if you desired. These advice also worked to be a fantastic way to comprehend many people have the identical passion the same as my very own to figure out much more when it comes to this issue. I know there are thousands of more pleasurable instances up front for those who browse through your site.

  9. kobe sneakers [#]

    I am only commenting to let you understand what a incredible experience my child found reading through your site. She even learned numerous details, which include what it is like to have an awesome coaching character to let many others clearly grasp specific advanced topics. You actually exceeded her desires. Thank you for delivering those necessary, safe, explanatory as well as fun thoughts on the topic to Gloria.

  10. nike sb dunks [#]

    I’m just commenting to let you know of the excellent encounter my wife’s child developed viewing the blog. She noticed such a lot of things, which include what it is like to have a very effective teaching mood to get a number of people without difficulty learn about certain tortuous things. You truly did more than people’s expectations. Many thanks for displaying these precious, trusted, revealing and easy tips on the topic to Janet.

  11. kyrie 5 [#]

    Thank you a lot for giving everyone an exceptionally nice possiblity to read critical reviews from this website. It’s usually very fantastic and also stuffed with a good time for me personally and my office colleagues to visit the blog the equivalent of thrice every week to study the fresh guides you have. Of course, I’m also usually fulfilled for the spectacular principles served by you. Certain 3 points in this posting are really the simplest we have all ever had.

  12. kyrie shoes [#]

    I wanted to compose you the very little word to finally thank you so much as before considering the splendid solutions you have shown at this time. This has been so strangely open-handed with people like you to supply unreservedly exactly what a few people would’ve sold as an electronic book in order to make some money on their own, certainly considering the fact that you might well have tried it in the event you wanted. These basics as well worked to provide a fantastic way to fully grasp some people have a similar fervor just as my personal own to know many more pertaining to this issue. I am certain there are numerous more enjoyable situations in the future for people who read your blog.

  13. yeezy boost 350 v2 [#]

    I have to voice my admiration for your kind-heartedness for those people who absolutely need guidance on this theme. Your very own commitment to passing the solution along appeared to be quite important and has continually helped guys and women like me to arrive at their desired goals. Your warm and friendly useful information means much a person like me and a whole lot more to my colleagues. Thanks a lot; from everyone of us.

  14. cheap cialis [#]

    cialis without a doctor prescription cialis 20 mg

  15. coub.com [#]

    Hi there, i read your blog from time to time and i own a similar
    one and i was just wondering if you get a lot of spam responses?
    If so how do you reduce it, any plugin or anything you can suggest?

    I get so much lately it’s driving me mad so any help is very
    much appreciated. scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery

  16. curry 8 [#]

    I actually wanted to type a quick word so as to say thanks to you for those splendid recommendations you are placing on this site. My time consuming internet look up has at the end of the day been rewarded with reliable ideas to share with my pals. I would declare that many of us website visitors are truly fortunate to live in a wonderful network with so many awesome individuals with valuable tips. I feel extremely fortunate to have encountered the webpages and look forward to tons of more pleasurable times reading here. Thanks again for everything.

  17. golden goose sneakers [#]

    I just wanted to post a small remark to express gratitude to you for these fabulous tricks you are placing at this site. My incredibly long internet look up has at the end been paid with reliable know-how to go over with my friends and family. I would say that many of us readers are quite fortunate to exist in a useful community with so many brilliant people with valuable strategies. I feel rather grateful to have come across your web page and look forward to some more awesome times reading here. Thank you once again for a lot of things.

  18. golden goose [#]

    Thank you so much for providing individuals with such a breathtaking possiblity to read critical reviews from here. It can be very superb plus full of a good time for me and my office peers to search your site at a minimum 3 times in a week to learn the fresh guidance you have got. And indeed, we are at all times impressed considering the sensational tactics served by you. Certain 4 tips in this article are undeniably the most impressive we have had.

  19. jordan 11 [#]

    I am also commenting to make you understand what a magnificent encounter our princess obtained viewing your webblog. She came to find many details, with the inclusion of what it’s like to possess an ideal teaching heart to let men and women with no trouble gain knowledge of various advanced matters. You actually did more than my expectations. Many thanks for distributing such beneficial, healthy, educational and also cool tips about this topic to Gloria.

  20. zithromaxeska [#]

    cytromax strep throat wikipedia zithromax used to treat

  21. Aryan Reno [#]

    This is what this site offers. สล็อตแตกง่าย pg

  22. modalertonline [#]

    modafinil pill modafinil side effects

  23. bactrim generic name [#]

    https://bactrimxazole.com/ bactrim ds dosage

  24. a bathing ape [#]

    I definitely wanted to jot down a brief comment to express gratitude to you for some of the splendid guides you are giving out at this site. My time-consuming internet investigation has now been paid with extremely good insight to share with my two friends. I would point out that most of us website visitors are quite lucky to dwell in a wonderful site with so many lovely individuals with valuable tips. I feel really blessed to have seen your entire weblog and look forward to tons of more brilliant minutes reading here. Thanks again for all the details.

  25. air jordan [#]

    Thank you for all your efforts on this site. My aunt loves conducting investigation and it’s obvious why. Almost all learn all concerning the powerful ways you give insightful tips and hints through the blog and even strongly encourage contribution from other ones on this concern then our own princess is without question discovering a great deal. Take pleasure in the rest of the year. You are performing a dazzling job.

  26. yeezys [#]

    Needed to write you this tiny observation to be able to thank you very much the moment again about the pleasant guidelines you have contributed in this article. It has been so generous of you to give easily all that numerous people might have marketed for an e book in making some money for their own end, most notably considering that you might well have done it if you considered necessary. The suggestions likewise acted like the easy way to fully grasp most people have similar passion similar to my own to know much more with regard to this issue. I believe there are some more fun opportunities in the future for those who view your blog.

  27. golden goose sneakers [#]

    I together with my buddies ended up looking at the excellent thoughts located on your web site and so all of the sudden got a horrible feeling I had not expressed respect to the web site owner for those tips. All the people appeared to be certainly warmed to read them and have now actually been taking advantage of these things. Thanks for being quite considerate and also for using variety of incredible themes most people are really wanting to discover. My very own sincere apologies for not expressing appreciation to earlier.

  28. paul george shoes [#]

    I precisely desired to say thanks once more. I’m not certain the things I would have made to happen in the absence of the actual tactics contributed by you relating to such subject matter. It has been a very frightening difficulty for me, nevertheless discovering your specialized mode you processed the issue took me to jump with happiness. I am thankful for your service as well as expect you really know what an amazing job you are always carrying out training others via a web site. Most probably you haven’t come across any of us.

  29. air jordan [#]

    I needed to put you that little note in order to thank you very much as before on the striking opinions you have shown at this time. It was really seriously open-handed of you to offer extensively all that most people could have marketed as an ebook to end up making some bucks on their own, precisely now that you might have done it in case you considered necessary. These creative ideas likewise served to be the fantastic way to be sure that some people have the same fervor just as my personal own to understand a lot more with reference to this issue. Certainly there are millions of more enjoyable moments in the future for those who look into your website.

  30. paul george shoes [#]

    I am commenting to let you understand what a brilliant experience my wife’s girl went through visiting your site. She picked up numerous things, not to mention what it’s like to possess an amazing giving mindset to have other people without hassle know precisely specific complicated matters. You undoubtedly did more than visitors’ expectations. Many thanks for coming up with those warm and helpful, trusted, explanatory and also unique tips about that topic to Gloria.

  31. Delta Air lines San Jose booking customer service [#]

    control of pest San Jose service https://www.goodcarbadcar.net/?s=control+of+pest+%E2%98%8E+1%28844%299484793+San Jose+service+phone+number

  32. zyltrc [#]

    perrigo cetirizine hydrochloride 10mg – https://allergyd.com

  33. what is cetirizine hydrochloride 10mg [#]

    allegra d – http://allergyd.com

  34. cheapest place to buy zyrtec [#]

    allegra coupons – https://allergyd.com

  35. vnjqoptwk [#]

    RS7Qr9 anigetaxzwab, [url=http://ugxjlhplvpgm.com/]ugxjlhplvpgm[/url], [link=http://omefcstjdkaz.com/]omefcstjdkaz[/link], http://mslugkgidmxh.com/

  36. cost of ivermectin pill [#]

    Non-specific Information About this outcome
    https://ivermectinstrom.com ivermectin tablets
    https://ivermectstromect.com ivermectin for sale at tractor supply
    https://ivermectinst.com ivermectine kopen
    https://ivermectinsts.com ivermectin tablets
    https://ivermectinst.com ivermectin tablets for humans

  37. Delta Air lines San Diego booking customer service [#]

    control of pest San Diego service https://www.webdesignerdepot.com/?s=control+of+pest+%E2%98%8E+1%28844%299484793+San Diego+service+phone+number

  38. www.pharmaceptica.com [#]

    hydroxyl chloroquine https://pharmaceptica.com/

  39. GazInvake [#]

    tadalafil 30mg tablet

  40. golden goose [#]

    My wife and i felt very cheerful Emmanuel managed to finish off his researching with the precious recommendations he had out of the web page. It is now and again perplexing to just happen to be making a gift of secrets which usually some other people might have been selling. Therefore we acknowledge we now have the blog owner to be grateful to for that. All of the illustrations you’ve made, the straightforward blog menu, the relationships you can make it easier to engender – it’s got most remarkable, and it is facilitating our son and the family believe that that matter is awesome, and that is extremely vital. Thank you for the whole lot!

  41. cheap jordans [#]

    I want to show appreciation to you for rescuing me from this particular scenario. After exploring through the the net and meeting principles which were not pleasant, I believed my entire life was done. Existing without the solutions to the problems you’ve resolved through your short post is a crucial case, and ones which might have in a wrong way affected my entire career if I had not discovered your blog post. That ability and kindness in handling all the details was very helpful. I am not sure what I would have done if I had not come across such a solution like this. I can now look forward to my future. Thank you so much for your impressive and effective guide. I won’t be reluctant to recommend your blog post to any individual who would need support on this topic.

  42. golden goose [#]

    I intended to compose you a little bit of remark to help say thanks a lot the moment again for your personal magnificent guidelines you have shown on this site. It was simply incredibly open-handed of people like you to give openly precisely what most of us could have sold as an e book to help make some bucks on their own, particularly seeing that you might well have tried it in the event you decided. The advice likewise worked like a great way to fully grasp the rest have similar zeal like my own to learn a little more on the subject of this matter. I know there are several more pleasant occasions in the future for individuals that looked over your blog.

  43. kevin durant shoes [#]

    I would like to show some thanks to you just for bailing me out of this problem. After checking through the world-wide-web and coming across things that were not beneficial, I figured my life was well over. Living without the strategies to the difficulties you’ve sorted out all through your main guide is a critical case, as well as the kind that could have negatively damaged my career if I hadn’t noticed your website. Your own skills and kindness in controlling every aspect was tremendous. I’m not sure what I would’ve done if I hadn’t encountered such a thing like this. I am able to at this time look ahead to my future. Thanks a lot so much for your high quality and results-oriented help. I won’t think twice to refer the website to any person who should get guidance about this problem.

  44. kyrie 6 [#]

    I have to show my appreciation to the writer for rescuing me from this challenge. As a result of looking out through the world-wide-web and meeting advice which were not helpful, I believed my life was over. Living without the presence of answers to the difficulties you’ve resolved as a result of your main website is a serious case, as well as the kind that would have adversely damaged my entire career if I had not encountered your web site. Your main expertise and kindness in maneuvering every aspect was precious. I’m not sure what I would have done if I hadn’t encountered such a subject like this. I am able to now look forward to my future. Thank you very much for the professional and amazing guide. I won’t be reluctant to suggest your web page to anyone who requires recommendations about this subject matter.

  45. russell westbrook shoes [#]

    I truly wanted to develop a small message in order to thank you for some of the magnificent tricks you are posting at this site. My incredibly long internet investigation has at the end of the day been rewarded with good facts to share with my companions. I would assume that many of us site visitors are definitely blessed to be in a superb community with many special professionals with very beneficial tips. I feel pretty happy to have come across your web page and look forward to tons of more enjoyable times reading here. Thank you once more for a lot of things.

  46. adidas yeezy [#]

    I truly wanted to make a quick message so as to appreciate you for the precious tricks you are sharing on this site. My particularly long internet research has finally been honored with excellent insight to exchange with my visitors. I ‘d claim that most of us visitors actually are very much blessed to dwell in a useful site with many awesome people with good hints. I feel pretty grateful to have used the webpages and look forward to so many more awesome moments reading here. Thanks a lot again for everything.

  47. lebron shoes [#]

    I just wanted to post a small word in order to appreciate you for all of the magnificent tricks you are giving here. My extended internet look up has at the end of the day been rewarded with high-quality content to go over with my contacts. I ‘d claim that most of us visitors are unequivocally lucky to exist in a superb network with many marvellous people with useful basics. I feel really blessed to have used your site and look forward to plenty of more entertaining times reading here. Thanks again for everything.

CSSLab