使div元素背景对背景图像CSS透明

时间:2017-07-23 12:44:07

标签: html css web transparent

我想在我的网站中制作视差效果,但我无法将DIV元素的背景设置为透明,尽管使用background-color: transparent

基本上,我想在DIV中单独显示文本,并删除背景颜色。

使用W3Schools ..

    body, html {
        height: 100%;
    }
    
    .parallax {
        /* The image used */
        background-image: url('http://www.zastavki.com/pictures/originals/2013/Photoshop_Image_of_the_horse_053857_.jpg');
    
        /* Full height */
        height: 100%; 
    
        /* Create the parallax scrolling effect */
        background-attachment: fixed;
        background-position: center;
        background-repeat: no-repeat;
        background-size: cover;
    }
    
    .parallax2 {
        /* The image used */
        background-image: url('http://www.qygjxz.com/data/out/114/4676052-image.png');
    
        /* Full height */
        height: 100%; 
    
        /* Create the parallax scrolling effect */
        background-attachment: fixed;
        background-position: center;
        background-repeat: no-repeat;
        background-size: cover;
    }
    
    /* Turn off parallax scrolling for tablets and phones. Increase the pixels if needed */
    @media only screen and (max-device-width: 1024px) {
        .parallax {
            background-attachment: scroll;
        }
    }>
    
    <div class="parallax"></div>
    
    <div style="height:100px;background-color:transparent;font-size:36px">
    Some text goes here, need to try to make the background of this transparent? Any ideas?
    </div>
    
    <div class="parallax2"></div>

谢谢

2 个答案:

答案 0 :(得分:1)

如果要将背景颜色设置为透明,则可以执行所有这些操作。

background-color: ; background-color: transparent; background-color: rgba(0, 0, 0, 0);

但是在这种情况下,你需要在这里设置一个不同的位置是代码我希望这是你正在寻找的

<div style="height:100px;font-size:36px; border:1px solid red; position: absolute;">
  Some text goes here, need to try to make the background of this transparent? Any ideas?
</div>

答案 1 :(得分:0)

如果您没有为div指定背景颜色,它将自动透明。这是默认值。我做了jsfiddle并对其进行了测试以确保。

<div style="height:100px;font-size:36px; border:1px solid red;">
    Some text goes here, need to try to make the background of this transparent? Any ideas?
    </div>

如果由于任何原因它无法正常工作,我会在Photoshop中制作一个2 * 2透明PNG并指定为背景颜色,在x和y方向重复。

它不是一个标准的解决方案,但肯定会有效。 :d