Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"non-opacity" border for "opacity" element

Tags:

html

css

Given this html

    <div id="my_div">   
    </div>

css

         #my_div {
            width: 100px;
            height: 100px; 
            background-color: #0f0;
            opacity: 0.4; 
            border: 3px solid #ff0000;
         }

I want that own div tag will opacity, but dont need border also.

css can make "non-opacity" border for "opacity" element?

like image 773
Oto Shavadze Avatar asked Nov 27 '12 14:11

Oto Shavadze


People also ask

How do you override opacity in a child element?

Basically, you can't override the opacity of a child. The answer you might be looking for will depend on what it is you are actually trying to do. Paulie is right, opacity effects the entire element (including children) and can't be reversed by a child element.

How do you make a translucent border in CSS?

In CSS, we can create a transparent border by using the border property in a nested div tag.

How can you prevent opacity of elements in children?

Answer: Use the CSS RGBA colors There is no CSS property like "background-opacity" that you can use only for changing the opacity or transparency of an element's background without affecting its child elements.


2 Answers

There isn't a non-opacity attribute but what you can do is set the background colour of that div with RGBA. This allows you to specify an opacity essentially, but just for the background (so it won't affect the border)

background: rgba(0, 255, 0, 0.4);

This will achieve what you want, a red border with that transparent looking background. Demo HERE. This won't however, make inner content, such as images or text transparent. Though you can set the opacity on those elements freely without worrying about the border of the parent.

You can find a good article that details the difference between opacity and RGBA here and here

It should be noted that, as expected, IE8 and below do not support RGBA, though it can be "hacked" with CSS3 PIE.

like image 116
Andy Avatar answered Sep 21 '22 07:09

Andy


Use CSS3 rgba, It's CSS so cross-browser will be an issue here, but for IE you can use CSS3 Pie

#my_div {
     width: 100px;
     height: 100px; 
     background-color: rgba(R, G, B, A);
     border: 3px solid #ff0000;
}

Demo

Moreover using rgba() for opaque container won't make your text opaque as using opacity: .7 used to do...

like image 32
Mr. Alien Avatar answered Sep 19 '22 07:09

Mr. Alien