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

html – 如何创建多边形形状div,第1张

概述我想在这里创建像图像上的 HTML元素: 问题是DIV元素具有多边形形状而不是常规矩形,将被放置在其他元素上方,如d出窗口和内部元素,有必要在源中显示矩形形状的图像,但在网络上显示像填充所有包含三角形的空间在左侧. 你认为有没有可能意识到没有准备将图像显示为适当多边形格式的透明PNG?只能通过CSS3转换或使用canvas或SVG? 一种方法可以是将图像分成两个容器,这两个容器的大小是父级的50% 我想在这里创建像图像上的 HTML元素:

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

你认为有没有可能意识到没有准备将图像显示为适当多边形格式的透明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 >  <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>
总结

以上是内存溢出为你收集整理的html – 如何创建多边形形状div全部内容,希望文章能够帮你解决html – 如何创建多边形形状div所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1133391.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存