html - 如何在 DIV 中创建一个洞?

标签 html css html5-canvas

<分区>

假设我有以下 HTML 结构:

<canvas></canvas>
<div class="overLay">
    <button>Click me!</button>
</div>

Canvas 绝对定位为负 z 索引,因此叠加层定位在它上面(我有那么多工作)。问题是我希望覆盖 div 具有白色背景,但按钮具有透明背景并显示 Canvas /主体背景颜色。

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

不起作用,因为它只显示其背后的白色背景。关于如何实现这种效果有什么想法吗?

这个问题可能被标记为可能重复的问题未能说明按钮可以具有诸如 border-radius 之类的属性这一事实,并且没有提供合适的解决方案。

最佳答案

没有办法在 HTML 元素中创建一个洞。透明背景只能应用于不是来自具有非透明背景的父级的元素。

您可以做的是在您的按钮周围创建与 sibling 处于同一级别的 div,认为这有点复杂。给那些 div 一个白色背景,然后你的透明按钮应该可以工作。

我使用表格布局创建了一个示例,其中按钮保留在中间行的中心单元格中。看到按钮显示 Canvas 背景颜色:

canvas {
    width: 200px;
    height: 200px;
    background: purple;
    position: absolute;
    z-index: -1;
}

.overLay {
    width: 200px;
    height: 200px;
    display: table;
}

.overLay button {
    background-color: Transparent;
    white-space: nowrap;
}

.overLay > div {
    display: table-row;
}

.overLay > .middle {
    height: 1px;
}

.overLay > div > div {
    display: table-cell;
    text-align: center;
}

.overLay > .middle > div:nth-child(2) {
    width: 1px;
}

/* Now, set the background on the divs around the button */
.top, .left, .right, .bottom {
    background: white;
}
<canvas></canvas>
<div class="overLay">
    <div class="top">
        <div></div>
        <div></div>
        <div></div>
    </div>
    <div class="middle">
        <div class="left"></div>
        <div>
            <button>Click me!</button>
        </div>
        <div class="right"></div>
    </div>
    <div class="bottom">
        <div></div>
        <div></div>
        <div></div>
    </div>
</div>

关于html - 如何在 DIV 中创建一个洞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30242677/

上一篇:css - Liferay 6.1 强制任何 "edit control"出现在弹出窗口中

下一篇:css - Drupal 7 中的 Twitter Bootstrap 列未在 Safari 中排列

相关文章:

html - 如何使按钮更像 Accordion 按钮?

html - 在 div 中居中元素,Bootstrap

html - IE 和 Div 高度

javascript - FabricJS 没有给我在 IE 中平移图像的 MovementX 和 MovementY,但它在其他浏览器中工作正常

javascript - 从 Vue 动画 Canvas

javascript - 在 Canvas 上绘图时 "Uncaught TypeError: Cannot read property ' getContext ' of undefined"

javascript - 通过 JavaScript 覆盖类

html - 如何删除 html 页面中背景色标题周围的空间?

javascript - 在 (QML/Javascript) Canvas 中绘制模糊的圆圈

javascript - 当满足循环内的条件时,如何仅显示特定控制台