CSS "Color" VS. "Font-Color"

CSS color vs. font-color

I would think that one reason could be that the color is applied to things other than font. For example:

div {
border: 1px solid;
color: red;
}

Yields both a red font color and a red border.

Alternatively, it could just be that the W3C's CSS standards are completely backwards and nonsensical as evidenced elsewhere.

HTML/CSS font color vs span style

You should use <span>, because as specified by the spec, <font> has been deprecated and probably won't display as you intend.

How to change font color in HTML?

You can go several ways of doing this. You can devine your color by predefined colors (see e.g. http://www.w3schools.com/html/html_colornames.asp) or by define the color yourself by hex code (see e.g. http://html-color-codes.info/webfarben_hexcodes/).

Inline CSS

<p style="color:blue">Blue inline CSS font</p>

Internal CSS

Define a style element in your head seaction and all your specified elements will be formatted.

<style>
p {
color: blue;
}
</style>

<p>Blue!</p>

External CSS

Same way as internal CSS except you link a CSS file instead of define the sytle in your head section.

This is the common way of styling with CSS, because the file is cached by the browser, so page load time is can be reduced.
To link you could load a CSS file e.g. by

<link rel="stylesheet" type="text/css" href="style.css">

Old depricated way

<font color="blue">Blue font</font>

or

<font color="#1B29A3">Selfdefined blue text</font>

How to decide font color in white or black depending on background color?

Building on my answer to a similar question.

You need to break the hex code into 3 pieces to get the individual red, green, and blue intensities. Each 2 digits of the code represent a value in hexadecimal (base-16) notation. I won't get into the details of the conversion here, they're easy to look up.

Once you have the intensities for the individual colors, you can determine the overall intensity of the color and choose the corresponding text.

if (red*0.299 + green*0.587 + blue*0.114) > 186 use #000000 else use #ffffff

The threshold of 186 is based on theory, but can be adjusted to taste. Based on the comments below a threshold of 150 may work better for you.


Edit: The above is simple and works reasonably well, and seems to have good acceptance here at StackOverflow. However, one of the comments below shows it can lead to non-compliance with W3C guidelines in some circumstances. Herewith I derive a modified form that always chooses the highest contrast based on the guidelines. If you don't need to conform to W3C rules then I'd stick with the simpler formula above. For an interesting look into the problems with this see Contrast Ratio Math and Related Visual Issues.

The formula given for contrast in the W3C Recommendations (WCAG 2.0) is (L1 + 0.05) / (L2 + 0.05), where L1 is the luminance of the lightest color and L2 is the luminance of the darkest on a scale of 0.0-1.0. The luminance of black is 0.0 and white is 1.0, so substituting those values lets you determine the one with the highest contrast. If the contrast for black is greater than the contrast for white, use black, otherwise use white. Given the luminance of the color you're testing as L the test becomes:

if (L + 0.05) / (0.0 + 0.05) > (1.0 + 0.05) / (L + 0.05) use #000000 else use #ffffff

This simplifies down algebraically to:

if L > sqrt(1.05 * 0.05) - 0.05

Or approximately:

if L > 0.179 use #000000 else use #ffffff

The only thing left is to compute L. That formula is also given in the guidelines and it looks like the conversion from sRGB to linear RGB followed by the ITU-R recommendation BT.709 for luminance.

for each c in r,g,b:
c = c / 255.0
if c <= 0.04045 then c = c/12.92 else c = ((c+0.055)/1.055) ^ 2.4
L = 0.2126 * r + 0.7152 * g + 0.0722 * b

The threshold of 0.179 should not be changed since it is tied to the W3C guidelines. If you find the results not to your liking, try the simpler formula above.

how do I change font color in html

When using the style attribute to apply a unique style to a single paragraph element, you should include a semicolon after each CSS property.

<p style="color: red;">
This text is red
</p>

<p style="font-family: impact;">
This text is impact font
</p>

<p style="color: red; font-family: impact;">
This text is red and impact font
</p>

Get the correct text-color and font-weight

a { 
background-color: rgb(100, 145, 239);
color: rgba(234, 255, 46, 1);
text-shadow: -2px 1px rgba(0,0,0,.4);
padding: 30px 30px;
letter-spacing: -0.05em;
font-weight: 600;
font-size: 16px;
text-decoration: none;
}
<a href="#">Community</a>

How to invert text color according to what's behind it? (not only background)

I thought mix-blend-mode: difference; would do just that. ✌️

#bg {
background-image:url(https://thumbs.dreamstime.com/b/beautiful-multicolored-bokeh-lights-holiday-glitter-background-christmas-new-year-birthday-celebration-high-resolution-image-133585052.jpg);
}
#test {
font-size:20vw;
color:#FFF;
mix-blend-mode: difference;
}
<div id="bg">
<div id="test">testing</div>
</div>


Related Topics



Leave a reply



Submit