In C#, the console has properties that can be used to change the background color of the console, and the foreground (text) color of the console.
Console.BackgroundColor // the background color
Console.ForegroundColor // the foreground/text color
The issue is that background color applies only where text is written, not to free space.
Console.BackgroundColor = ConsoleColor.White; // background color is white
Console.ForegroundColor = ConsoleColor.Blue; // text color is blue
Now, with the above code, it does indeed turn the text blue, but it only turns the background of the text white, instead of the entire console window's background.
Here's an example of what I mean:
As you can see, the white background only displays behind the text, and does not change the color of the entire console window.
How do I change the color of the entire console window?
To change the background color of the console window as a whole, set the BackgroundColor property and call the Clear method.
Colorizing text and console background in C++ In C++ programming, the default background of the output screen is black and the text color is the white color, the task is to color both the background and text color in the output screen. console_color = GetStdHandle(STD_OUTPUT_HANDLE);
To add background color in HTML, use the CSS background-color property. Set it to the color name or code you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a table, heading, div, or span tag.
You need to clear the console window AFTER setting the colors but BEFORE you write the text...
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Green;
Console.Clear();
Console.WriteLine("Hello World");
Console.ReadLine();
Pardon the shameless self-promotion, but I've created a small plugin (available on NuGet) that allows you to add any (if supported by your terminal) color to your console output, without the limitations of the classic solutions.
It works by extending the String
object, and the syntax is very simple:
"colorize me".Pastel("#1E90FF");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With