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 */ }
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 */ }
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!