Setting color via rgba in CSS
In addition to the rgb
format, there is the rgba
format, which works in a similar way, but the fourth parameter allows you to set the translucency of the color. This parameter accepts fractional values from 0
to 1
. In this case, one corresponds to full opacity, and zero to full transparency.
Let's look at some examples.
Example
First, let's make an opaque block like this:
<div id="parent">
<div id="elem">
Lorem ipsum dolor sit amet.
</div>
</div>
#parent {
background-image: url("bg.png");
background-repeat: no-repeat;
background-size: cover;
display: inline-block;
padding: 30px;
}
#elem {
width: 300px;
height: 200px;
border: 20px solid red;
padding: 10px;
font-size: 50px;
font-weight: bold;
color: rgba(255, 0, 0); /* red gules */
}
:
Example
Now let's add translucency to the text:
<div id="parent">
<div id="elem">
Lorem ipsum dolor sit amet.
</div>
</div>
#parent {
background-image: url("bg.png");
background-repeat: no-repeat;
background-size: cover;
display: inline-block;
padding: 30px;
}
#elem {
width: 300px;
height: 200px;
border: 20px solid red;
padding: 10px;
font-size: 50px;
font-weight: bold;
color: rgba(255, 0, 0, 0.5); /* red translucent */
}
:
Example
Now let's make a translucent border:
<div id="parent">
<div id="elem">
Lorem ipsum dolor sit amet.
</div>
</div>
#parent {
background-image: url("bg.png");
background-repeat: no-repeat;
background-size: cover;
display: inline-block;
padding: 30px;
}
#elem {
width: 300px;
height: 200px;
border: 20px solid rgba(255, 0, 0, 0.5); /* red translucent */
padding: 10px;
font-size: 50px;
font-weight: bold;
color: red;
}
:
Example
Now let's make a semi-transparent background:
<div id="parent">
<div id="elem">
Lorem ipsum dolor sit amet.
</div>
</div>
#parent {
background-image: url("bg.png");
background-repeat: no-repeat;
background-size: cover;
display: inline-block;
padding: 30px;
}
#elem {
width: 300px;
height: 200px;
border: 20px solid red;
background-color: rgba(0, 0, 0, 0.4); /* black translucent */
padding: 10px;
font-size: 50px;
font-weight: bold;
color: red;
}
:
Practical tasks
Set the paragraphs to a red translucent color.
Set the background color to green and semi-transparent.
Set the border color to blue and semi-transparent.