Border Opacity v1.4.0+
Control the opacity of an element's border color using the .border-opacity-
To control an element's border color opacity at a specific breakpoint, add a
For more information about Tailwind's responsive design features, check out the Responsive Design documentation.
To customize the opacity values for all opacity-related utilities at once, use the opacity section of your tailwind.config.js theme configuration:
If you want to customize only the border opacity utilities, use the borderOpacity section:
Learn more about customizing the default theme in the theme customization documentation.
Responsive and pseudo-class variants
By default, only responsive, hover and focus variants are generated for border opacity utilities.
You can control which variants are generated for the border opacity utilities by modifying the borderOpacity property in the variants section of your tailwind.config.js file.
For example, this config will also generate active and group-hover variants:
If you don't plan to use the border opacity utilities in your project, you can disable them entirely by setting the borderOpacity property to false in the corePlugins section of your config file:
Can you set a border opacity in CSS?
Is there a straight forward CSS way to make the border of an element semi-transparent with something like this?
If not, does anyone have an idea how I could do so without using images?
11 Answers 11
Unfortunately the opacity property makes the whole element (including any text) semi-transparent. The best way to make the border semi-transparent is with the rgba color format. For example, this would give a red border with 50% opacity:
For extremely old browsers that don’t support rgba (IE8 and older), the solution is to provide two border declarations. The first with a fake opacity, and the second with the actual. If a browser is capable, it will use the second, if not, it will use the first.
The first border declaration will be the equivalent color to a 50% opaque red border over a white background (although any graphics under the border will not bleed through).
I’ve added background-clip: padding-box; to the examples above to ensure the border remains transparent even if a solid background color is applied.
It’s easy, use a solid shadow with 0 offset:
Also, if you set a border-radius to the element, it gives you pretty rounded borders
As others have mentioned, CSS3 supports the rgba(. ) syntax to specify a border color with an opacity (alpha) value.
It works in Safari and Chrome (probably works in all webkit browsers).
It works in Firefox
I doubt that it works at all in IE, but I suspect that there is some filter or behavior that will make it work.
There’s also CSS RGBA border / background alpha double, which suggests some other issues—namely, that the border renders on-top-of any background color (or background image) that you’ve specified; thus limiting the usefulness of border alpha in many cases.
If you check your CSS coding with W3C validator, you will see if your CSS code is acceptable, even if it worked in the major browsers.
Creating a transparent border via CSS, as written above,
is not accepted by W3C standards, not even for CSS3. I used the direct input validator with the following CSS code,
The results were,
Value Error : border Too many values or values are not recognized : 1px solid rgba(255,0,0,0.5 )
Unfortunate that the alpha value (the letter «a» at the end of «rgb») is not accepted by W3C as part of the border color values as yet. I do wonder why it is not standardized, since it works in all browsers. The only hitch is whether you want to stick to W3C standards or step aside from it to create something in CSS.
Always a good idea to use a validator to check your work, it really helps finding small or even large errors in coding when your going cross-eyed after hours of coding work.
*Not as far as i know there isn’t what i do normally in this kind of circumstances is create a block beneath with a bigger size((bordersize*2)+originalsize) and make it transparent using
here is an example
This answer is outdated, since after all this question is more than 8 years old. Today all up to date browsers support rgba, box shadows and so on. But this is a decent example how it was 8+ years ago.
As an alternate solution that may work in some cases: change the border-style to dotted .
Having alternating groups of pixels between the foreground color and the background color isn’t the same as a continuous line of partially transparent pixels. On the other hand, this requires significantly less CSS and it is much more compatible across every browser without any browser-specific directives.
Other answers deal with the technical aspect of the border-opacity issue, while I’d like to present a hack(pure CSS and HTML only). Basically create a container div, having a border div and then the content div.
And then the CSS:(set content border to none, take care of positioning such that border thickness is accounted for)
One may also consider other border styles ( dashed , dotted ) to make the border partly fully transparent:
Thinking outside the box a bit here: if you are working with SVG basic shapes, you can use a combination of stroke , stroke-width (browser support > 97%), and stroke-opacity (browser support > 99%) to do essentially the same thing the OP is asking.
For example, this declaration:
will add a translucent blue halo around the perimeter of a <circle> with #000 fill.
This JSFiddle provides a demo for several SVG basic shapes. The fiddle uses a red fill and blue stroke to highlight a major difference here between stroke and border — half the stroke-width is inside the perimeter of the basic shape.
This gives a "double border" appearance (from outside towards the center for each basic shape: blue->(blue + red = purple)->red) that would be impossible to achieve with CSS border alone (but border + outline could — see the above JSFiddle for an example using a <div> ), and difficult to achieve (in the case of radial basic shapes) with a radial-gradient() .
How to set border opacity in CSS
Many candidates are rejected or down-leveled in technical interviews due to poor performance in behavioral or cultural fit interviews. Ace your interviews with this free course, where you will practice confidently tackling behavioral interview questions.
In CSS, the opacity property allows us to make an element semi-transparent. Although we cannot use this property to change the transparency of a border, we can achieve a similar effect by using an RGBA color value with the border property.
An RGBA color value is specified using the rgba() function, which takes four arguments: the red, green, and blue channel values, and the alpha channel value. The alpha channel determines the transparency of the color, with a value of 0.0 0.0 0.0 indicating full transparency and a value of 1.0 1.0 1.0 indicating full opacity. By setting the alpha channel to a value between 0.0 0.0 0.0 and 1.0 1.0 1.0 , we can create a transparent border using the border property.
The basic syntax for setting the transparency of a border using CSS is as follows:
The code specifies a border that has a red color and is half transparent.
Suppose we have a div with a thick border. We want to make the border more transparent, but still visible. Our HTML, CSS, and output are shown below, and you can experiment with changing the values in the CSS code to see how the property behaves:
CSS Border Opacity
Borders are used to enclose many things like text, images, or any other form of data, as we know that every aspect or element in Hyper Text Markup Language can be styled by using CSS. So, we will be using CSS to style a border by making it opaque and semitransparent. CSS can be done in different ways like style tag CSS or inline CSS, which can have some different types of effects on the border. The basic script to make a border opaque and control its transparency to some degree in CSS is written below:
In the above script, we can see that the entity is called by its name, its size is defined in pixels, and the color is defined in “rgba” format, which will look over the color coding and transparency of the border.
Now we will implement this concept by writing the script in Notepad++ and running it on a browser.
Example 01: Using CSS in a Head Tag in an HTML File
In this example, we’ll make an HTML file with a “div” with borders, and the “div” will include some information on the style tag in between the HTML file’s head tag.
In the above script, we can see that the head tag of the HTML file has two tags in it. In the first tag, the title of the file is written, and the second tag, which is the style tag, has the visual properties of how a “div” will look like when the file is executed on a browser. In this part of the code, the padding property is defined in pixels, then the properties for the border of the div are defined by giving it size and the color format in the standard RGBA method in which the last parameter is for opacity, and we have given it a certain value. Then we will close the style tag along with the head tag. After this, we will open the body tag and give the body a heading and then a div that has some styling done to it through the header CSS, which will be applicable on every div in the HTML file.
After this, we will save this file in the “.html” format and open this file on our browser to visualize the file.
In the above snippet, we can see that the heading which was provided in the body tag is present along with a div container in which some text is written. This div container also has a red semitransparent border which means that the border has some opacity defined in the style tag of the Header of the HTML file.
Example 02: Using CSS to Make the Border’s Top Opaque in an HTML File
In this example, we will create an HTML file with a “div” that has borders, and the “div” will have some details on the style tag in between the HTML file’s Head tag, but in this case, we will add another tag that will just opaque the top portion of the border.
The above script shows that the HTML file’s head tag has two tags. The title of the file is written in the first tag, and the style tag has the visual attributes of how a “div” would look when the file is performed on a browser. The padding value is supplied in pixels in this portion of the code, and the properties for the div’s border are established by providing it a size and color format in the standard RGBA method, with the last parameter being opacity, which we have set to “0.”
Following that, we opened a border-top tag in the rgba format, and this time we will set the opaque option to a value that will make just the top semitransparent. The style tag and the head tag will thereafter be closed. After that, we will open the body tag and give it a heading, followed by a div with some styling applied via the header CSS, which will be applied to every div in the HTML file.
After that, we will save the script in “.html” format and open it in our browser to view it.
We can see in the above snippet that the heading from the body tag is there, as well as a div container with some content. The border also has a red semitransparent top, indicating that the border’s top part has some opacity, which was determined by the style tag of the HTML file’s Header.
Example 03: Using CSS to Make the Border’s Bottom Part Opaque and Differentiating its Color From the Sides and Top in an HTML File
In this example, we will construct an HTML file with a “div” that has borders, and the “div” will have some information on the style tag in between the HTML file’s Head tag, but in this case, we will add two more tags that will just opaque the bottom section of the border.
The following script demonstrates that the HTML file’s head element has two tags. The first tag contains the file’s title, and the style tag contains the visual elements of how a “div” would appear when the file is executed in a browser. The padding value is supplied in pixels in this portion of the code, and the properties for the div’s border are established by providing it a size and color format in the standard RGBA method, with the last parameter being opacity, which we have set to “0.”
After that, we opened a rgba border-bottom tag and altered the color parameters to a new color. We then opened the border-bottom tag again and changed the opacity parameter to “0.2” to make the bottom section of the border semitransparent. After that, the style and head tags will be closed. Following that, we will open the body element and add a heading to it, followed by a div with some styling applied via the header CSS, which will be applied to all divs in the HTML file.
The script will then be saved in “.html” format and opened in our browser to be seen.
The above snippet shows the header from the body tag, as well as a div container with some text. The border also has a grey semitransparent bottom, suggesting that the bottom section of the border has some opacity and distinct color, as specified in the HTML file’s Header style tag.
In this article, we discussed several methods to add opacity to a border in CSS. As we discussed, CSS allows us to style and edit every element of an HTML page, which includes a div container with a border. We implemented this phenomenon on an HTML page and made several changes to the styling by making changes to the CSS style tag in the Notepad++ environment.
About the author
I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.