Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Wikipedia:Reference desk/Archives/Computing/2022 December 17

From Wikipedia, the free encyclopedia
Computing desk
< December 16 << Nov | December | Jan >> December 18 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


December 17

[edit]

Making buttons to change the colors on a page.

[edit]

I had buttons on a site that changed the bgcolor and text color back and forth between "day time" and "night time." The code just changed the default colors. But now I want to use to as declaring variables, i.e., color1, color2, which can change, upon button click. Here's what I have

<style type="text/css">
		body.theme-day { color1: green; color2: red; color3: lightgray; }
		body.theme-night { color1: darkgreen; color2: darkred; color3: gray; }

		/* hide elements for the opposite theme 
		body.theme-day .theme-night { display: none; }
		body.theme-night .theme-day { display: none; }
         */
	</style>
	<script type="text/javascript">
		function change_theme(theme) {
			document.body.className = document.body.className.replace(/\s*theme-\S+/, "") + " theme-" + theme;
		}
	</script>
//And then the buttons.
<P align="center"><button class="theme-night" type="button" onclick="change_theme('day');">Day version</button>
<button class="theme-day" type="button" onclick="change_theme('night');">Night version</button></P>

And so my issue is, using color1 and color2 are not recognized, say, when I use font color="color1" for example. Thanks. 67.165.185.178 (talk) 16:29, 17 December 2022 (UTC).reply

I'm not used to CSS, but I had a look at the relevant page on W3Schools.
Variables: it seems that their names have to start with --, so you would have to say --color1: green; --color2: red;, and so on. Then, it seems that you have refer to them using the var() function. So rather than saying style="color: color2;" it would be style="color: var(--color2);".  Card Zero  (talk) 22:07, 17 December 2022 (UTC)reply
Okay, I got half of it to work. The part that works is
th, td {
  border: 2px solid var(--color2);
}

(Above is all within <style> tags. But the part that doesn't work is <body style="bgcolor: var(--color3);">,

<table style="bgcolor: var(--color3);">

, and

<td style="bgcolor: var(--color1); width:83%">

67.165.185.178 (talk) 00:08, 18 December 2022 (UTC).reply

Is it possibly background-color rather than bgcolor?

 Card Zero  (talk) 00:49, 18 December 2022 (UTC)reply

Okay, got it to work, it was indeed background-color. 67.165.185.178 (talk) 01:07, 18 December 2022 (UTC).reply