Determining the best CSS based on the user's preferred color scheme using media queries

Is it possible to separate my CSS into two files, one for dark mode and one for light mode, and conditionally load them using only HTML without the need for JavaScript?

I haven't been able to find any examples on https://developer.mozilla.org. Could it be done like this?

<link rel="stylesheet" href="/light.css" type="text/css" media="(prefers-color-scheme: light)">
<link rel="stylesheet" href="/dark.css" type="text/css" media="(prefers-color-scheme: dark)">

Answer №1

Implementing CSS based on any media query expression may not align with the specified guidelines for the media attribute.

For a list of acceptable values for the media attribute, refer to this resource:
https://www.w3schools.com/tags/att_link_media.asp

The allowed values for the media attribute are limited to specific ones such as: width, color, orientation, among others. Refer to the tables in the link provided above for more details.

It is considered best practice to load all CSS, including media queries, directly rather than separately. Loading all styles at once during the initial phase allows the browser to have everything in memory and render elements swiftly.
Loading each media query individually would require the browser to determine which CSS file to request from the server when, for example, the user rotates their tablet. This could lead to delays and cause jittery movements on the page, significantly impacting user experience.

Although loading extra kilobytes for media queries CSS might seem like an issue, consolidating all styles into a few files offers benefits:

  1. Reduces the number of TCP connections that need to be established
  2. Enhances efficiency in compression during data transmission
  3. If the styles remain unchanged, they can be cached by the browser and requested only once

Answer №2

It is not possible to achieve that in HTML, but you can accomplish it within the CSS file using either

@media(prefers-color-scheme: light) {}

or

@media(prefers-color-scheme: dark) {}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the utilization of attributes with slots in Vue.js

Can attributes be set on a slot so that the element from the parent inherits those attributes? Parent <vDropdown> <button slot="button">new button</button> <ul>content</ul> </vDropdown> Dropdown.vue <d ...

Problem with see-through images against a see-through backdrop

I'm experiencing a strange issue with a graphic that has rounded corners. On my HTML page, I have the body set to transparent (style="filter:alpha(opacity=100);opacity:100;background-color:transparent;"), and within this body is a div containing a PN ...

What is the process for including icons on buttons?

I have been researching how to use Font Awesome software to incorporate icons into my HTML elements, but I am uncertain about the implementation process for my specific case. Currently, I have created a basic webpage with buttons and I would like to includ ...

What is the reason border property does not transition effectively?

I am trying to remove the border property after a certain period of time (1 second) and make it smooth. Here is what I have attempted: var elem = $('div').css({'border-top':'6px solid #FC9A24', 'border-le ...

Unfortunately, Rem fails to honor the preferences of users on their smartphones

Trying to incorporate rem units to respect user-defined font-size: Initially, I included the following line in my CSS file: html { font-size: 62.5%; } Subsequently, I applied font-size using rem units to my element. For example: .header{ font-size: 1.5 ...

Creating solid, dotted, and dashed lines with basic HTML/CSS for tcpdf rendering

Take a look at the image below. It combines two graphs, with different curves represented by solid lines, dotted lines, and various forms of dashed lines with varying stroke lengths. In summary: I am looking for a simple way to create solid lines, dashed ...

Utilizing AJAX for autocomplete in HTML and inserting it into a page using innerHTML is causing issues with the functionality of forms on the website

As I work on developing my website, I am facing an issue with nesting HTML code generated by a PHP page using Ajax innerHTML. While the PHP-generated page loads successfully, it appears modified compared to how I want it to be uploaded. The main problem li ...

Enhance the visual appeal of your checkboxes in React Fluent UI by customizing the color of the checked mark and

I have a React application using Fluent UI. Currently, the <Checkbox/> component is displaying with its default colors and behavior like this: I want to customize the color of the checked mark and label (Green for checked mark and brown for label). ...

Associate information with HTML elements

I've come across this question multiple times, but the solutions are mostly focused on HTML5. My DOCTYPE declaration is: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> I'm looking t ...

Using jQuery to create a flawless animation

I am currently working on an animation project, and I have shared my progress on jsfiddle. Below is the code snippet I have utilized: /* JavaScript: */ var app = function () { var self = this; var allBoxes = $('.box&apos ...

Incorporating CSS into React.js applications

I am currently working on a project using MERN.io. In my project, I am utilizing the React.js component Dropdown. However, the Dropdown component does not come with its own style and CSS, resulting in no styling at all. ... import Dropdown from 'rea ...

Syntax error detected: Unexpected T_ELSEIF statement

I have been attempting some basic coding on codeacademy and struggling with an issue for the past hour. What could be causing this error to display: "Parse error: syntax error, unexpected T_ELSEIF on line 12 " <html> <head> <title&g ...

Paragraph opacity adjustments can alter both the text and background opacities

Is there a way to change the opacity of only the background of a paragraph without affecting the text? When I try to adjust the opacity, it changes both the text and the background. How can I resolve this issue? HTML <div id="opener"> <p id="in ...

Turn off automatic calling of Javascript function via the menu

Can anyone help me with calling a JS function from an action link? Here's my declaration: <li><a href="javascript:myFunc()"><span class="glyphicon glyphicon-ok"></span>&nbsp;Test</a></li> The issue is that the ...

Mastering the art of combining images and text harmoniously

I am having trouble aligning my lion image and h1 tag side by side. There seems to be an issue but I can't figure out what it is. h2 { width:50%; float:right; padding:30px 0px 0px 0px; margin:0 auto; } .lion { width:10%; float: left; paddin ...

PHP DOMDocument - Script tag containing Chinese characters is improperly formatted

Can anyone help me figure out why my PHP DomDocument is converting Chinese characters inside a script tag to strange symbols in the output? <?php $html = <<<EOD <!DOCTYPE html> <html> <head> <script> ...

What is the best way to manage photos from your phone without having to upload them online?

I'm currently developing an application using AngularJS/Javascript, HTML, and CSS within a cloud-based environment on c9.io. My next task is to create and test an app that can access the phone's photo album, apply filters to images, and I'm ...

Are you experiencing issues with the .submit() function when used in conjunction with other

Currently, I am working on a survey form that incorporates JQuery to dynamically display or hide fields based on user selections. //FORM CONDITIONALS: This script is responsible for hiding all the helpfulness radios and only displaying them when "Yes" fo ...

When the user presses either the refresh button or the back button, they will be redirected

After the user presses refresh or uses the browser back button, I need to redirect them to a specific page in order to restart the application. My JavaScript code is as follows: var workIsDone = false; window.onbeforeunload = confirmBrowseAway; function ...

Using JavaScript to modify a section of an anchor link attribute

I am looking to dynamically update part of the URL when a specific radio button is selected. There are three purchase links, each representing a different amount. After choosing an animal, I need to select one of the three amounts to spend. How can I modi ...