【问题标题】:Is it possible to mask or clip an image's shape using CSS?是否可以使用 CSS 屏蔽或剪切图像的形状?
【发布时间】:2015-01-02 17:33:34
【问题描述】:

我正在尝试在 CSS 中创建此设计。有可能吗?

这是我的代码:

.triangle{
    border-radius: 50%;
    width: 120px;
    height: 120px;
}
.triangle img {
    width: 120px;
    height: 120px;
}
.triangle::after{
    right: 150px;
    top: 50%;
    border: solid transparent;
    content:"";
    height:  0px;
    width:  0px;
    position: absolute;
    pointer-events: none;
    border-color: white;
    border-left-color: white;/*white is the color of the body*/
    border-width: 60px;
    margin-top: -60px
}
<div class="triangle">
    <img src="http://deskode.com/images/placeholder/team/07.jpg">
</div>

三角形已形成,但与图像的方式不同。

jsFiddle

【问题讨论】:

  • 我现在无法测试,但.triangle 上的overflow: hidden 应该会有所帮助。
  • 我会看看CSS Clipping Masks

标签: html css css-shapes


【解决方案1】:

这可以仅使用 CSS 来实现。伪元素:before:after 用于制作三角形(相对于容器定位),而border-radius 创建圆角。

.triangle {
    border-radius: 0 60px 60px 0;
    height: 120px;
    overflow: hidden;    
    position: relative;
    width: 120px;
}
.triangle:before, .triangle:after {
    content: "";
    height: 0;
    left: 0;
    position: absolute;
    width: 0;
}
.triangle:before {
    border-right: 60px solid transparent;
    border-top: 60px solid #FFFFFF;
    top: 0;
}
.triangle:after {
    border-bottom: 60px solid #FFFFFF;
    border-right: 60px solid transparent;
    bottom: 0;
}
<div class="triangle">
    <img alt="" src="http://placehold.it/120x120" />
</div>

JS 小提琴: http://jsfiddle.net/rw7q2te2/1/

【讨论】:

  • 我为你的聪明才智喝彩,但是三角形使用边框意味着当放置在不同颜色的背景上时,颜色不会显示出来。
  • @ChrisSpittle 没错,三角形的border-color 需要设置为与包含.triangle 的元素相同的background-color 才能工作。在这种情况下,我认为这不是问题(除非 grijalvaromero 想纠正我!)因为 OP 没有明确说明这需要适用于不同的背景颜色,并且代码中的 border-left-color: white;/*white is the color of the body*/ 表明这将在白色背景上使用。
  • border-color: inherit 可以用作解决不同背景颜色问题的黑客解决方法吗?我想你必须将容器的边框颜色设置为与背景相同。
  • @Svish 这会起作用,尽管您注意到您仍然会在不同的地方声明边框颜色。
【解决方案2】:

只有一个类。

http://jsfiddle.net/koh36dsz/1/

.wedge {
  width: 0px;
  height: 0px;
  border-right: 60px solid transparent;
  border-top: 60px solid red;
  border-left: 0px solid red;
  border-bottom: 60px solid red;

}

<div class='wedge'></div>

【讨论】:

  • :o 最后非常感谢你,我将继续使用这个简单的代码:D
【解决方案3】:

虽然Hidden Hobbes 已经提供了一个很好的答案,但还有另一种方法是旋转 div 和图像。但是,这需要一个相当大的图像,该图像将被剪裁。基本代码是

    .triangle{
        border-radius: 50%;
        width: 120px;
        height: 120px;
        margin:0 auto;
        transform: rotate(-45deg);
        position: relative;
        overflow:hidden;
        border-radius: 0 50% 50% 50%;
    }
    .triangle img{
        width: 200%;
        height: 200%;   
        transform: rotate(45deg);
        position: relative;
        left: -60px;
        top: -30px;
    }

DEMO

【讨论】:

  • 这是@HiddenHobbes,就像卡尔文和霍布斯一样,而不是加拉德瑞尔和霍比特人^^
  • +1 此方法的优点是它可以用于具有不同背景颜色/图像的父元素。哦,我绝对不是霍比特人! :P
  • 再次,我真的很抱歉,我爱 Calvin&Hobbes,不应该忽略我的注意力。
  • @Paul,没问题,别出汗! :)
【解决方案4】:

这是一个基于@HiddenHobbes 回答和@misterManSam 评论的版本,其中容器是完全透明的。

http://jsfiddle.net/jkz8bkb8/1/

body {
    background: #f00;
}
.triangle {
    overflow: hidden;
    position: relative;
    width: 90px;
    height: 90px;
    margin: 15px;
    transform: rotate(45deg);
}
.triangle img {
    border-radius: 50%;
    position: absolute;
    right: 0;
    top: 0;
    width: 120px;
    height: 120px;
    transform: rotate(-45deg);
}
<div class="triangle">
    <img alt="" src="http://placehold.it/120x120" />
</div>

【讨论】:

【解决方案5】:

正如 Austin Brunkhorst 在 cmets 中指出的那样,可以使用 SVG clipping。我已经整理了一个快速的Fiddle 展示了它是如何完成的,我将在下面添加 HTML:

<svg width="120px" height="100px" version="1.1" xmlns="http://www.w3.org/2000/svg">
    <defs>
        <clipPath id="mask">
            <path d="M230 80 A 45 45, 0, 1, 0, 275 125 L 275 80 Z" fill="red"  transform="rotate(-135 133 120)" />
        </clipPath>
    </defs>
    <image xlink:href="http://placehold.it/120x100" x="0" y="0" height="100px" width="120px"  clip-path="url(#mask)" />
</svg>

值得指出的是,我不是专家,如果您能弄清楚如何正确调整 d 值,我敢打赌 transform 属性可以从 path 元素中删除。

【讨论】:

  • 这实际上是最简单的答案 XD。应该使用更多的标记来格式化文本和答案。 +1
猜你喜欢
  • 2022-01-23
  • 1970-01-01
  • 2018-09-17
  • 2013-07-23
  • 1970-01-01
  • 2017-05-13
  • 1970-01-01
  • 2011-10-20
相关资源
最近更新 更多