【问题标题】:Is it possible to have a element transparent and see through his parent opaque background是否可以让一个元素透明并看穿他的父级不透明背景
【发布时间】:2015-09-01 14:42:09
【问题描述】:

您好,我正在设计一个表单(HTML / CSS / JS),当用户想要订阅或登录时,它会弹出内容。整个表单背景是纯色不透明颜色,但我希望输入具有透明背景,以便我们可以通过它看到表单背后的内容。我将输入设置为透明没有任何问题,但我不知道如何让它们“通过”它们的父背景颜色。我开始认为只有 CSS 是不可能的。我可以共享代码,但这更像是一个通用问题。谢谢

【问题讨论】:

  • 没有。您需要在父母的背景中具有透明度。
  • 你不能。一种解决方法是让父背景颜色为透明,并在父背景上有一个不透明的背景图像,透明的“区域”与放置输入字段的区域相匹配。
  • 我不认为你可以设置文本框背景来匹配容器的颜色?具有低 alpha
  • 我只能想到一种方法,但它似乎是过度杀戮,纯粹是一种解决方法。如果一天结束时没有人回答,我会填写我的答案。
  • 你可以做不止一种方法,使用 css 和图像 :-)

标签: html css background transparency


【解决方案1】:

您可以通过使用更多元素和一点创意来让您的输入看起来像是一个剪裁,从而作弊......

Working Example

$('.wrap').draggable();// demo only, used to show off cutout effect. Not necessary 
.wrap {
    position:absolute;
}
.top, .bottom {
    background:grey;
}
input, .top, .bottom {
    width: 100%;
    border: 30px solid grey;
}
input {
    background: transparent;
    color: red;
    border: 29px solid grey;
    outline: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<div class="wrap">
    <div class="top">This may be cheating, but it works...</div>
    <input type="text" placeholder="testing"/>
    <div class="bottom"></div>
</div>
<p>Zombie ipsum reversus ab viral inferno, nam rick grimes malum cerebro. De carne lumbering animata corpora quaeritis. Summus brains sit​​, morbo vel maleficia? De apocalypsi gorger omero undead survivor dictum mauris. Hi mindless mortuis soulless creaturas, imo evil stalking monstra adventus resi dentevil vultus comedat cerebella viventium. Qui animated corpse, cricket bat max brucks terribilem incessu zomby. The voodoo sacerdos flesh eater, suscitat mortuos comedere carnem virus. Zonbi tattered for solum oculi eorum defunctis go lum cerebro. Nescio brains an Undead zombies. Sicut malus putrid voodoo horror. Nigh tofth eliv ingdead. Cum horribilem walking dead resurgere de crazed sepulcris creaturis, zombie sicut de grave feeding iride et serpens. Pestilentia, shaun ofthe dead scythe animated corpses ipsa screams. Pestilentia est plague haec decaying ambulabat mortuos. Sicut zeder apathetic malus voodoo. Aenean a dolor plan et terror soulless vulnerum contagium accedunt, mortui iam vivam unlife. Qui tardius moveri, brid eof reanimator sed in magna copia sint terribiles undeath legionis. Alii missing oculis aliorum sicut serpere crabs nostram. Putridi braindead odores kill and infect, aere implent left four dead. Lucio fulci tremor est dark vivos magna. Expansis creepy arm yof darkness ulnis witchcraft missing carnem armis Kirkman Moore and Adlard caeruleum in locis. Romero morbo Congress amarus in auras. Nihil horum sagittis tincidunt, zombie slack-jawed gelida survival portenta. The unleashed virus est, et iam zombie mortui ambulabunt super terram. Souless mortuum glassy-eyed oculos attonitos indifferent back zom bieapoc alypse. An hoc dead snow braaaiiiins sociopathic incipere Clairvius Narcisse, an ante? Is bello mundi z?</p>

【讨论】:

  • 我喜欢你的回答,它很有创意并且向后兼容。但我也喜欢@Toni Leigh 戴着面具回答。我从未使用过面具,所以我会对其进行测试并为我的设计选择最佳选项。我会回来检查标记最佳答案。谢谢!
  • @Berthy - 我实际上编辑了我的 &lt;svg&gt; 答案 - 你可以在 div 上使用 .png 作为 background-image 而不是 svg,这将支持 100% 的当前全球浏览器
【解决方案2】:

一个完全不同的解决方案是使用&lt;svg&gt;,甚至是.png(尽管这将是一个额外的http请求)。

<div class='container is--transparent'>
    <svg>
        # I am the same size as the background and have transparent parts where required
        # Indeed, I could be a div, with a .png background, if you needed to support older browsers
    </svg>
    <input class='input-one' />
    <input class='input-two' />
</div>

然后用css来设置:

  1. position: absolute; 在后台 div 中的所有内容上
  2. svg 是 top: 0; left: 0; 所以它完全覆盖了容器
  3. 要定位在 SVG 中透明部分上的输入
  4. 输入的背景是透明的

这样输入下的所有内容都将是透明的,并且无论背景是什么都会发光。

对svgs的支持也很好,你必须回到IE8才会有问题。

【讨论】:

  • 我需要将一些宽度/填充/边距更改为 %age 值,以便在移动设备上调整表单大小时输入遵循图像“盲点”的大小。但这很好用!谢谢您的回答 !我会进一步研究面具......很确定它在得到完全支持时会很有用。谢谢!
  • 如果没有经过良好的试验,我将无法确定仅使用 % 的可能性有多大(几乎可以肯定,但我不想肯定地说),但是,您也可以一组根据宽度加载的固定尺寸 - 这将 100% 有效,但可能不适合设计 - 可以尝试的东西,分享一个 jsfiddle :-)
【解决方案3】:

是的,你可以,你可以使用mask

Support is not perfect,但回退是没有透明度,所以它会很好地降级。这也会随着时间的推移而改善。

这将允许元素背后的任何东西发光,平面颜色、纹理、渐变、多个元素、图像甚至视频。

另外,它将样式保留在它所属的 css(和图像)中。

我创建了一个JSFiddle to demonstrate - 真的很简单:

<div class="background">
    <div class='masked'>
    </div>
</div>

标记在那里,背景包含要屏蔽的元素。蒙版本身是由任何透明度(包括完全透明)的黑色区域组成的图像。这里是:

这两个字母框形状是透明的位,是背景烧到前景的地方。底部的栏是 50% 透明的,用于演示如何使用 alpha 值。

css 在这里:

.background {
    background: -webkit-gradient(linear, left top, right bottom, color-stop(0%,#959595), color-stop(46%,#0d0d0d), color-stop(50%,#010101), color-stop(53%,#0a0a0a), color-stop(76%,#4e4e4e), color-stop(87%,#383838), color-stop(100%,#1b1b1b));
    width: 250px;
    height: 250px;
    float:left;
}
.masked {
    background: #fff;
    width: 200px;
    margin: 25px;
    height: 200px;
    background-color: #4f6;
    -webkit-mask-image: url(http://stackoverflowimages.excitedstatelaboratory.com/mask-example-image-2.png);
}

你可以看到一些演示特定的定位和大小的东西,但重要的是-webkit-mask-image,它就像一个背景图像一样可以找到所需的 png。

我们可以break the mask image 使其属性无效,模拟浏览器不理解该属性时会发生什么。

最后一点,您放入插槽中的任何内容都必须将其自己的背景设置为透明,以便通过插槽的内容可见,但 this is easy

【讨论】:

    【解决方案4】:

    看看这个解决方案,https://jsfiddle.net/bnnp2pvj/1/

    var color = $('.container').css('background-color');
    color = color.replace(')', ', 0.2)').replace('rgb', 'rgba');
    $('.background-match').css({"background-color":color});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-15
      • 1970-01-01
      • 2011-09-10
      • 2022-06-14
      • 2015-03-09
      • 2017-09-29
      • 1970-01-01
      相关资源
      最近更新 更多