简体   繁体   中英

PNG image's transparency overlaps a CSS background property

The PNG image is the sidebar, and the black part is the CSS background, the PNG's alpha seems to override the black box.

在此处输入图片说明

When I change the image's opacity, you can see the box continues through the entire image, but is still overridden and I double-checked the sidebar's transparency, but it's set up properly.

在此处输入图片说明

It does this on Google Chrome as well as Firefox.

Relevant CSS:

.sidebar{
    background: url('side1.png') lightgray 10% 50%;
    background-repeat: no-repeat;
    background-size: 100%;
    height: 600px;
    width: 173px;
    z-index:1;
    float:left;
    position:relative;
    opacity:0.5;
}

.header{
    background: black;
    background-position: top right;
    float:right;
    width:100%;
    height: 200px;
    z-index:0;
    position:absolute;
}

Relevant HTML:

<div class="sidebar">
    <img src="images/pic1.png" class="icon">
</div>

<div class="header"></div>

This appears to be just a simple case of the division going back behind the floated content. Most people don't realize that just because there is floated content there, the division still expands back behind it all the way to the edge, like it normally would if the floated content wasn't there.

That division is taking up its maximum amount of available space like it is expected too. The floated content is only pushing the content, which at this point, there isn't any. Making your sidebar partially opaque, this issue becomes visible as you can see that box behind your image now. A quick fix, per say, would be to add a margin to the division to push it out from behind the sidebar, like so:

.header {
    margin-left: 173px; /* The width of your sidebar */
}

Note, however, that you would have to apply this margin to the left side of all your block-level elements that need pushed out from under. So it would make sense to put all the right content into a single box that gets pushed out, to prevent confusion.


Edit: The reason your black background doesn't pull through on the sidebar image is that you're setting it's background to light grey here:

background: url('side1.png') lightgray 10% 50%;

This will put a light grey background behind the image rather than letting the transparent part of your image go through to whatever is behind it. Try removing it:

background: url('side1.png') 10% 50%;

See the jsFiddle example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM