How can I make my website's background transparent without making the content (images & text) transparent too?

前端 未结 8 1339
小鲜肉
小鲜肉 2020-12-15 04:25

I\'m doing a website for a school project, and I\'m currently having a small problem... I can\'t make the body\'s background transparent without it also affecting the conten

相关标签:
8条回答
  • 2020-12-15 05:06

    You probably want an extra wrapper. use a div for the background and position it below your content..

    http://jsfiddle.net/pixelass/42F2j/

    HTML

    <div id="background-image"></div>
    <div id="content">
        Here is the content at opacity 1
        <img src="http://lorempixel.com/100/50/fashion/1/">
    
    </div>
    

    CSS

    #background-image {
        background-image: url(http://lorempixel.com/400/200/sports/1/);
        opacity:0.4;
        position:absolute;
        top:0;
        left:0;
        height:200px;
        width:400px;
        z-index:0;
    }
    #content {
        z-index:1;
        position:relative;
    }
    
    0 讨论(0)
  • 2020-12-15 05:07

    Make the background image transparent/semi-transparent. If it's a solid coloured background just create a 1px by 1px image in fireworks or whatever and adjust its opacity...

    0 讨论(0)
  • 2020-12-15 05:07
    background:rgba(0,0,0,0);
    opacity:1;
    
    0 讨论(0)
  • 2020-12-15 05:14

    I think what's happening, is that, since the wrapper id is relatively position, it just appears on the same position with the body tag, what you should do, is that you can add a Z-index to the wrapper id.

    #wrapper {
    margin: auto;
    text-align: left;
    width: 832px;
    position: relative;
    padding-top: 27px;
    z-index: 99; /* added this line */
     }
    

    This should make layers above the transparent body tag.

    0 讨论(0)
  • 2020-12-15 05:17

    There is a css3 solution here if that is acceptable. It supports the graceful degradation approach where css3 isn't supported. you just won't have any transparency.

    body {
        font-family: tahoma, helvetica, arial, sans-serif;
        font-size: 12px;
        text-align: center;
        background: #000;
        color: #ffffd4d4;
        padding-top: 12px;
        line-height: 2;
        background-image: url('images/background.jpg');
        background-repeat: no-repeat;
        background-attachment: fixed;
        background-size: 100%;
        background: rgb(0, 0, 0); /* for older browsers */
        background: rgba(0, 0, 0, 0.8); /* R, G, B, A */
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#CC000000, endColorstr=#CC0000); /* AA, RR, GG, BB */
    
    }
    

    to get the hex equivalent of 80% (CC) take (pct / 100) * 255 and convert to hex.

    0 讨论(0)
  • 2020-12-15 05:18

    I think the simplest solution, rather than making the body element partially transparent, would be to add an extra div to hold the background, and change the opacity there, instead.

    So you would add a div like:

    <div id="background"></div>
    

    And move your body element's background CSS to it, as well as some additional positioning properties, like this:

    #background {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background-image: url('images/background.jpg');
        background-repeat: no-repeat;
        background-attachment: fixed;
        background-size: 100%;
        opacity: 0.8;
        filter:alpha(opacity=80);
    }
    

    Here's an example: http://jsfiddle.net/nbVg4/4/

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