微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

html – 如何创建多边形形状div

我想在这里创建像图像上的 HTML元素:

问题是DIV元素具有多边形形状而不是常规矩形,将被放置在其他元素上方,如弹出窗口和内部元素,有必要在源中显示矩形形状的图像,但在网络上显示像填充所有包含三角形的空间在左侧.

你认为有没有可能意识到没有准备将图像显示为适当多边形格式的透明PNG?只能通过CSS3转换或使用canvas或SVG?

解决方法

一种方法可以是将图像分成两个容器,这两个容器的大小是父级的50%,分别转换每个容器并将背景定位为看起来像是单个图像.变换可以是倾斜(在答案中使用)或基于透视的旋转.

请注意,由于我们正在转换容器,因此我们必须将反向效果应用于实际图像,以使其看起来正常.

.image {
  position: relative;
  height: 150px;
  width: 450px;
  overflow: hidden;
}
.top-container,.bottom-container {
  position: absolute;
  left: 0px;
  height: 50%;
  width: 100%;
  overflow: hidden;
  backface-visibility: hidden;
}
.top-container {
  top: 0px;
  transform-origin: right bottom;
  transform: skew(-20deg);
}
.bottom-container {
  bottom: 0px;
  transform-origin: right top;
  transform: skew(20deg);
  background-position: 0% 100%;
}
.top-container:after,.bottom-container:after {
  position: absolute;
  content: '';
  height: 100%;
  width: 100%;
  left: -14px;  /* tan(20) * (height/2) / 2 */
  background: url(http://lorempixel.com/450/150);
  background-size: 100% 200%;
}
.top-container:after {
  top: 0px;
  transform: skew(20deg);
}
.bottom-container:after {
  bottom: 0px;
  transform: skew(-20deg);
  background-position: 0% 100%;
}

/* Just for demo */

body {
  background: linear-gradient(90deg,crimson,indianred,purple);
}
.image2 {
  margin-top: 10px;
  height: 150px;
  width: 450px;
  background: url(http://lorempixel.com/450/150);
}
<div class="image">
  <div class='top-container'></div>
  <div class='bottom-container'></div>
</div>


<!-- this is the actual image for comparison -->

<h3>Original Image</h3>
<div class='image2'></div>

我打算建议使用SVG和clipPath,但由于Persijn已经发布了该样本,我在下面添加一个不同版本的多边形.

.vector {
  position: relative;
  height: 150px;
  width: 450px;
}
svg {
  position: absolute;
  top: 0px;
  left: 0px;
  height: 100%;
  width: 100%;
}
polygon {
  fill: url(#image);
}

/* Just for demo */

body {
  background: linear-gradient(90deg,purple);
}
<div class='vector'>
  <svg viewBox='0 0 450 150' preserveAspectRatio='none'>
    <defs>
      <pattern id='image' height='150' width='450' patternUnits='userSpaceOnUse'>
        <image xlink:href='http://lorempixel.com/450/150' height='150' width='450' />
      </pattern>
    </defs>
    <polygon points='15,0 450,150 15,150 0,75' />
  </svg>
</div>

原文地址:https://www.jb51.cc/html/227514.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐