How to apply an opacity without affecting a child element with html/css?

前端 未结 12 2186
臣服心动
臣服心动 2020-11-29 04:12

I want to achieve this using html and css:

\"schema\"

I have tried to set the opacity of the container

相关标签:
12条回答
  • 2020-11-29 04:25

    Another workaround is to simply use an overlay background to create a similar effect.

    I personally like a black overlay with about a 65% opacity, but for what you are trying to do you may want to use a white overlay at round 70%.

    Create a small (100 x 100 or less) PNG in Photoshop or GIMP that has the color and opacity you want. Then just set that as the background of your light box.

    If you create multiple PNGs at different opacities you can easily switch between them with JS or dynamically at load via backend scripting.

    It's not technically what you are trying to do, but aesthetically it can give a very similar effect and UX wise accomplishes the same thing. It is also very easy to do, and widely supported across pretty much everything.

    0 讨论(0)
  • 2020-11-29 04:31

    Apply this css rule

    .alpha60 { 
    
    /* Fallback for web browsers that doesn't support RGBa */ 
    
    background: rgb(0, 0, 0); 
    
    /* RGBa with 0.6 opacity */ 
    
    background: rgba(0, 0, 0, 0.6); 
    
    /* For IE 5.5 - 7*/ 
    
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000,      endColorstr=#99000000); 
    
    /* For IE 8*/ 
    
    -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000,     endColorstr=#99000000)"; 
    }
    

    In addition to this, you have to declare background: transparent for IE web browsers.

    For more details visit the following link:

    http://robertnyman.com/2010/01/11/css-background-transparency-without-affecting-child-elements-through-rgba-and-filters/

    0 讨论(0)
  • 2020-11-29 04:33

    This might not be the most orthodox method but you can use a small semi-transparent background image for each div / container that repeats. It does seem that in this day and age you should be able to achieve this in pure (simple not hackish) css with no js but as the answers above show it isn't that straight forward...

    Using a tiled image might seem dated but will work no worries across all browsers.

    0 讨论(0)
  • 2020-11-29 04:33

    You can add a container's sibling absolutely positioned behind container, with the same size, and apply opacity to it.

    And use no background on your container.

    Now container's children have no opaque parent and the problem vanishes.

    0 讨论(0)
  • 2020-11-29 04:36

    Any child of an element with opacity set will take on that opacity.

    To achieve this style you could use rgba colours and filters for IE for the background, and opacity on the textual elements. So long as the second box isn't a child of one of the text elements, then it won't inherit the opacity.

    0 讨论(0)
  • 2020-11-29 04:36

    You can't apply an opacity property without affecting a child element!

    "Opacity applies to the element as a whole, including its contents, even though the value is not inherited by child elements. Thus, the element and its children all have the same opacity relative to the element's background, even if they have different opacities relative to one another... If you do not want to apply opacity to child elements, use the background property instead." https://developer.mozilla.org/en-US/docs/Web/CSS/opacity

    If you want the opacity to be applied only to the background, without affecting the child elements, use:

    background-color: rgba(255, 255, 255, .3)

    However, you can achieve the desired effect if you place them inside a div parent element and use CSS position property:

    .parent {
      border: solid green 3px;
      position: relative;
      width: 400px;
      height: 200px;
    }
    
    .sibling-one {
      border: solid red 3px;
      position: absolute;
      box-sizing: border-box;
      width: 400px;
      height: 200px;
      opacity: .3;
     }
    
    .sibling-two {
        border: solid blue 1px;
        margin: 10px;
        width: 200px;
        height: 100px;
        position: absolute;
        transform: translateY(50%);
    }  
    <div class="parent">
      <div class="sibling-one">
      <p>A sibling's one element</p>
    </div>
    <div class="sibling-two">
        <p>A sibling's two element</p>
    </div>
    </div>

    0 讨论(0)
提交回复
热议问题