【问题标题】:Element with cut corner and semi-opaque background具有切角和半透明背景的元素
【发布时间】:2015-05-18 14:22:14
【问题描述】:

我需要创建一个带有切角和边框的流体形状。形状需要能够位于未知背景上。这本身不是问题,但我还需要这个元素的背景是半透明的。

这是我目前所拥有的......

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

body {
    background: pink;
}

.sidebar-widget {
    border: 1px solid #44AAAB;
    border-right: none;
    border-bottom: none;
    position: relative;
    padding: 15px 0 0 15px;
    margin-bottom: 20px;
}

.sidebar-widget .inner {
    position: relative;
    padding: 15px 0 0 15px;
    left: -15px;
    top: -15px;
    background: #f2f2f2;
}

.sidebar-widget .inner:before {
    content: "";
    width: 100%;
    height: 15px;
    background: #f2f2f2;
    border: 1px solid #44AAAB;
    border-right: none;
    border-top: none;
    position: absolute;
    left: -1px;
    bottom: -16px;
}

.sidebar-widget .inner .content:after {
    content: "";
    width: 15px;
    height: 100%;
    background: #f2f2f2;
    border: 1px solid #44AAAB;
    border-left: none;
    border-bottom: none;
    position: absolute;
    right: -16px;
    top: -1px;
}

.corner {
    width: 22px;
    height: 22px;
    border-right: 1px solid #44AAAB;
    background: #f2f2f2;
    position: absolute;
    right: 4px;
    bottom: 4px;
    transform: rotate(45deg);
    z-index: 1;
}

.sidebar-widget.trans-bg .inner,
.sidebar-widget.trans-bg .inner:before,
.sidebar-widget.trans-bg .inner .content:after,
.trans-bg .corner {
    background: rgba(0,0,0,0.5);
}
<div class="sidebar-widget">
    <div class="corner"></div>
    <div class="inner">
        <div class="content">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam tellus felis, faucibus id velit eget, auctor tristique ex. Pellentesque id dolor risus. Donec tincidunt, nisl id laoreet tristique, ligula magna placerat mi, id congue magna diam ut sem. Aenean ornare eros nec sapien porta, laoreet venenatis est lobortis.
        </div>
    </div>
</div>

<div class="sidebar-widget trans-bg">
    <div class="corner"></div>
    <div class="inner">
        <div class="content">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam tellus felis, faucibus id velit eget, auctor tristique ex. Pellentesque id dolor risus. Donec tincidunt, nisl id laoreet tristique, ligula magna placerat mi, id congue magna diam ut sem. Aenean ornare eros nec sapien porta, laoreet venenatis est lobortis.
        </div>
    </div>
</div>

当元素具有纯色背景时,此方法有效,但正如您所见,用于创建截角的方形元素在使用半透明背景时清晰可见。谁能想到解决这个问题的方法?

JSFiddle version

【问题讨论】:

  • 我会说,可能最容易使用border-image 来实现这种效果。
  • 这里有几种实现布局的方法:Div with cut out edges, border and transparent background
  • 嗨@web-tiki 不知道我是如何设法错过这个问题的。我尝试了您的解决方案,但不幸的是它依赖于我没有的固定高度。边框宽度的渲染也存在一些我无法忍受的问题
  • @uʍopǝpısdn 我建议在链接问题中使用this approach
  • 该解决方案也不适用于流体高度元素

标签: html css transparency css-shapes


【解决方案1】:

对于任何对此感兴趣的人来说,我最终是如何做到的。

overflow: hidden;.corner包裹在另一个元素中

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

body {
    background: pink;
}

.sidebar-widget {
    border: 1px solid #44AAAB;
    border-right: none;
    border-bottom: none;
    position: relative;
    padding: 15px 0 0 15px;
    margin-bottom: 20px;
}

.sidebar-widget .inner {
    position: relative;
    padding: 15px 0 0 15px;
    left: -15px;
    top: -15px;
    background: #f2f2f2;
}

.sidebar-widget .inner:before {
    content: "";
    width: 100%;
    height: 15px;
    background: #f2f2f2;
    border: 1px solid #44AAAB;
    border-right: none;
    border-top: none;
    position: absolute;
    left: -1px;
    bottom: -16px;
}

.sidebar-widget .inner .content:after {
    content: "";
    width: 15px;
    height: 100%;
    background: #f2f2f2;
    border: 1px solid #44AAAB;
    border-left: none;
    border-bottom: none;
    position: absolute;
    right: -16px;
    top: -1px;
}

.corner-mask {
    width: 15px;
    height: 15px;
    position: absolute;
    right: 0;
    bottom: 0;
    overflow: hidden;
}

.corner {
    width: 22px;
    height: 22px;
    border-right: 1px solid #44AAAB;
    background: #f2f2f2;
    position: absolute;
    right: 4px;
    bottom: 4px;
    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    transform: rotate(45deg);
    z-index: 1;
}

.sidebar-widget.trans-bg .inner,
.sidebar-widget.trans-bg .inner:before,
.sidebar-widget.trans-bg .inner .content:after,
.trans-bg .corner {
    background: rgba(0,0,0,0.5);
}
<div class="sidebar-widget">
    <div class="corner-mask">
        <div class="corner"></div>
    </div>
    <div class="inner">
        <div class="content">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam tellus felis, faucibus id velit eget, auctor tristique ex. Pellentesque id dolor risus. Donec tincidunt, nisl id laoreet tristique, ligula magna placerat mi, id congue magna diam ut sem. Aenean ornare eros nec sapien porta, laoreet venenatis est lobortis.
        </div>
    </div>
</div>

<div class="sidebar-widget trans-bg">
    <div class="corner-mask">
        <div class="corner"></div>
    </div>
    <div class="inner">
        <div class="content">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam tellus felis, faucibus id velit eget, auctor tristique ex. Pellentesque id dolor risus. Donec tincidunt, nisl id laoreet tristique, ligula magna placerat mi, id congue magna diam ut sem. Aenean ornare eros nec sapien porta, laoreet venenatis est lobortis.
        </div>
    </div>
</div>

JSFiddle version

【讨论】:

    【解决方案2】:

    您可以在.corner 中使用另一个元素,请参见下面的示例:

    <style>
        .corner, .corner_inner { /* added .corner_inner to your styles */
            width: 22px; 
            height: 22px; 
            border-right: 1px solid #44AAAB; 
            background: #f2f2f2; 
            position: absolute; 
            right: 4px; 
            bottom: 4px; 
            transform: rotate(45deg); 
            z-index: 1;
        }
    
        .trans-bg .corner {background: transparent} /* overwrite the background above */
    
        .trans-bg .corner_inner { /* set background and position for inner element */
            width: 11px; 
            height: 11px; 
            right: -1px; 
            bottom: 0; 
            background: rgba(0,0,0,0.5);
        }
    </style>
    
    <div class="sidebar-widget trans-bg">
        <div class="corner">
            <div class="corner_inner"></div>
        </div>
        ...
    </div>
    

    http://jsfiddle.net/6Lyph6zj/3/

    我已经编辑了你的样式,一些属性被覆盖并且可以被删除。我的意思是您可以更好地理解我所做的更改。

    【讨论】:

    • 嗨@panther。你的小提琴是这样显示的……s12.postimg.org/iu2le8nrh/cut_corner_copy.png
    • @uʍopǝpısdn:嗯,奇怪。在 Safari 中,它似乎与您的代码完全相同。我无法对其进行更多测试,但是内部元素(带背景)和将透明背景设置为.corner 的方式应该是正确的。
    猜你喜欢
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 2015-11-12
    • 2020-01-12
    • 1970-01-01
    • 2020-10-30
    • 2011-12-15
    相关资源
    最近更新 更多