How to add a specific color to an element using CSS ?
Last Updated :
29 Sep, 2021
In this article, we will discuss elements and how to add a specific color to an element using CSS properties.
Basically, the HTML element is the collection of start and end tags with the content inserted in between them. Elements can be nested.
<tagname> Content </tagname>
Note: Please refer to the HTML Elements article for a better understanding.
Approach:
- To add color to the Text, use the CSS color property.
- To add color to the Box, use the CSS background-color property.
- To add color to the Border, use the CSS border-color property.
Example 1: Add color to the text using the above approach.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Element Color</title>
<style>
h1 {
color: green;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
</body>
</html>
In the above code, we have created an h1 tag and change the color of the text to green. To change the text color, we have used CSS color property.
Output:
Text color
Example 2: Add color to the Box.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Element Color</title>
<style>
.box-color {
background-color: lightgreen;
height: 50px;
width: 100px;
}
</style>
</head>
<body>
<div class="box-color">GeeksforGeeks</div>
</body>
</html>
In the above code, we have created a div tag with a class name box-color. In the style tag, we have used the CSS background-color property to change the color of the background.
The same background-color property is also used to change the background in the body tag to change the color of the entire webpage.
Output:
Box color
Note: The dimensions of the layout is totally depending on the user.
Example 3: Add color to the Border.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Element Color</title>
<style>
.border {
border-color: green;
border-style: solid;
height: 50px;
width: 100px;
}
</style>
</head>
<body>
<div class="border">GeeksforGeeks</div>
</body>
</html>
In the above code, we have created a div tag with a class name border. In the style tag, we have used the CSS border-color property to change the color and border-style property to make it visible.
Output:
Border Colour
Similar Reads
How to Set Text Color for a Specific Paragraph using CSS ? In web development, setting different text colors for a specific paragraph can be useful for highlighting certain content. We will see how to set different text colors for a specific paragraph using CSS. This can help make certain parts of your text stand out. Tip: Prefer External Stylesheets for ea
3 min read
How To Add Opacity To A Color in CSS? To add opacity to a color in CSS, you can use the rgba() function, which stands for red, green, blue, and alpha (opacity). The rgba() function allows you to specify the opacity of a color by adding a fourth parameter called the "alpha channel." The alpha value ranges from 0 (completely transparent)
3 min read
How to Add Background Color in Input and Text Fields using CSS ? In this article, we will explore different approaches to adding background color to input and text fields using CSS. Customizing the appearance of input and text fields is an essential aspect of web design, and changing the background color is a powerful way to enhance the visual appeal of these ele
2 min read
How to change background color when hover over li elements using CSS ? In this article, we will see how to change the background color of li (list item) on hover using CSS.The hover effect may be used to indicate interactivity or might just be there for aesthetic purposes, like highlighting different items in case of a list. Hover event can be styled using the :hover p
2 min read
How to pick a random color from an array using CSS and JavaScript ? The task is to pick a random color from an array using CSS. There is no way to do this using CSS only. CSS doesn't support logical statements because CSS is purely deterministic (there is nothing like array etc in CSS). We can use client-side JavaScript to select a random color from the array. Below
1 min read