How To Change Link Colors With CSS - W3docs
Có thể bạn quan tâm
You can style links differently with CSS properties. In general, the properties used to style links are color, font-family and background-color.
There are three ways of changing the link color: inline, internal and external.
Inline method
Add the style attribute directly to the hyperlink code and specify the color property through the style attribute, then give a color value to it.
Example of changing a link color with the inline method:
<!DOCTYPE html> <html> <head> <title>Title of the document</title> </head> <body> <p>Visit our <a href="https://www.w3docs.com/" style="color: #8ebf42">website</a>.</p> </body> </html> Try it Yourself »Result
Visit our website.
Internal method
Internal method requires you to use the <style> tag within the <head> section of your HTML document. In the <style> tag, we set the color of our link.
Example of changing the link color with the internal method:
<!DOCTYPE html> <html> <head> <title>Title of the document</title> <style> a { color: #8ebf42; } </style> </head> <body> <p>Visit our <a href="https://www.w3docs.com/">website</a>. </p> </body> </html> Try it Yourself »There are 4 link states that links can be styled depending on what state they are in:
- a:link - a normal, unvisited link,
- a:visited - a link the user has visited,
- a:hover - a link when a user mouses over it,
- a:active - the moment a link is clicked.
When setting the style for several link states, follow these rules:
- a:hover MUST come after a:link and a:visited,
- a:active MUST come after a:hover.
Example of using different link states:
<!DOCTYPE html> <html> <head> <title>Title of the document</title> <style> /* unvisited link */ a:link { color: #ccc; } /* visited link */ a:visited { color: #095484; } /* mouse over link */ a:hover { color: #8ebf42; } /* selected link */ a:active { color: #800000; } </style> </head> <body> <p>Visit our <a href="https://www.w3docs.com/">website</a>. </p> </body> </html> Try it Yourself »How to change the colors of the hyperlink underline and anchor text
To change the underline color, first of all, you need to remove it with the "none" value of the text-decoration property and set the "none" value, then add the border-bottom property with the width (in this case, used as a hyperlink underline width) and border-style (solid, dotted, or dashed) properties. For the anchor text color, use the color property.
Example of changing a hyperlink underline and anchor text color:
<!DOCTYPE html> <html> <head> <title>Title of the document</title> <style> a { text-decoration: none; } a:link { color: #000; border-bottom: 1px solid #ff0000; } a:visited { color: #e600e6; border-bottom: 1px solid #b3b3b3; } a:hover { color: #2d8653; border-bottom: 1px solid #000099; } </style> </head> <body> <p>Visit our <a href="https://www.w3docs.com/">website</a>. </p> </body> </html> Try it Yourself »Example of styling links with the mentioned methods:
<!DOCTYPE html> <html> <head> <title>Title of the document</title> <style> a.one:link { color: #ccc; } a.one:visited { color: #095484; } a.one:hover { color: #8ebf42; } a.two:link { color: #ccc; } a.two:visited { color: #095484; } a.two:hover { font-size: 150%; } a.three:link { color: #ccc; } a.three:visited { color: #095484; } a.three:hover { background: #8ebf42; } a.four:link { color: #ccc; } a.four:visited { color: #095484; } a.four:hover { font-family: monospace; } a.five:link { color: #095484; text-decoration: none; } a.five:visited { color: #095484; text-decoration: none; } a.five:hover { text-decoration: overline underline; } </style> </head> <body> <p>Mouse over the links and watch how they will be changed:</p> <p> <a class="one" href="#">This link changes color</a> </p> <p> <a class="two" href="#">This link changes font-size</a> </p> <p> <a class="three" href="#">This link changes background-color</a> </p> <p> <a class="four" href="#">This link changes font-family</a> </p> <p> <a class="five" href="#">This link changes text-decoration</a> </p> </body> </html> Try it Yourself »Now, we'll demonstrate another example, where we use the color property with its "inherit" value. This will make the element take the color of its parent.
Example of changing the link color with the "inherit" value of the color property:
<!DOCTYPE html> <html> <head> <title>Title of the document</title> <style> p { color: green; } p a { color: inherit; } </style> </head> <body> <h1>Example</h1> <a href="https://www.w3docs.com/">W3docs.com</a> <p>Visit our <a href="https://www.w3docs.com/">W3docs.com</a> website. </p> </body> </html> Try it Yourself »Example of styling links with the CSS text-decoration-color property:
<!DOCTYPE html> <html> <head> <title>Title of the document</title> <style> a { text-decoration-color: grey; } a:link { color: #777777; } a:hover { color: #2d8653; } </style> </head> <body> <p>Visit our <a href="https://www.w3docs.com/">website</a>.</p> </body> </html> Try it Yourself »External method
Using external stylesheets you can take control of all the hyperlinks of your site. With external stylesheets, many attractive hyperlink effects can be created to develop the look of your website.
With the external method, you’ll link your web pages to an external .css file that can be created by any text editor in your device. This is a more efficient method, especially when you need to style a large website. You can change your whole site at once by editing one .css file.
Learn more about CSS links.
Từ khóa » Html Color Tag Inline
-
How To Set Font Color In HTML? - Tutorialspoint
-
HTML Styles - W3Schools
-
HTML Styles CSS - W3Schools
-
HTML: Changing Colors Of Specific Words In A String Of Text
-
HTML Font Style – How To Change Text Color And Size With An ...
-
HTML Text Color Code
-
How To Change Text Color In Html - Javatpoint
-
Inline Styles In HTML - Codecademy
-
How To Change The HTML Font Color - Kinsta
-
Inline Css Text Color Code Example - Code Grepper
-
How To Add & Change Background Color In HTML - HubSpot Blog
-
How To Change Text And Background Color In CSS - HubSpot Blog
-
Applying Color To HTML Elements Using CSS - MDN Web Docs
-
Change Font Color Inline Html