Home > Web Front-end > CSS Tutorial > How do I create semi-transparent borders in CSS?

How do I create semi-transparent borders in CSS?

Barbara Streisand
Release: 2024-11-10 04:43:02
Original
1028 people have browsed it

How do I create semi-transparent borders in CSS?

How to Establish Semi-Transparent Borders in CSS

In CSS, setting the opacity property affects the transparency of the entire element, including its text. For creating semi-transparent borders, a straightforward approach is not available. However, the rgba color format enables you to achieve this effect.

For instance, the following code sets a 50% opaque red border:

div {
    border: 1px solid rgba(255, 0, 0, .5);
    -webkit-background-clip: padding-box; /* for Safari */
    background-clip: padding-box; /* for IE9+, Firefox 4+, Opera, Chrome */
}
Copy after login

For browsers that don't support rgba (IE8 and earlier), a double border strategy is necessary. The first border is set to a fake opacity, while the second represents the actual desired opacity:

div {
    border: 1px solid rgb(127, 0, 0);
    border: 1px solid rgba(255, 0, 0, .5);
    -webkit-background-clip: padding-box; /* for Safari */
    background-clip: padding-box; /* for IE9+, Firefox 4+, Opera, Chrome */
}
Copy after login

The first border simulates a 50% opaque red border over white, creating the desired effect on most browsers.

To ensure the border remains transparent even with a solid background color, add background-clip: padding-box; as shown in the examples above.

The above is the detailed content of How do I create semi-transparent borders in CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template