简体   繁体   English

CSS中的梯形div

[英]Trapezoid div in CSS

I want sections with content in a trapezoid div but I don't know how to start or what the best way is to achieve my goal: 我希望在梯形div包含内容的部分,但我不知道如何开始或实现目标的最佳方法是:

在此输入图像描述

I had come across this solution but there isn't much info for me to understand CSS3 Transform to Trapezoid 我遇到过这个解决方案,但是我没有太多信息可以理解CSS3 Transform to Trapezoid

HTML HTML

<div class="section">
    <p>content here</p>
</div>

Here is a way to create a trapzoid like div. 这是一种创建像div这样的陷阱的方法。 This uses the ::before and ::after pseudo elements 这使用:: before:: after伪元素

 .example { margin: 20px 0px; position: relative; display: inline-block; width: 200px; height: 200px; background-color: gray; color: white; font-size: 2rem; } .example::before { content: ""; position: absolute; top: -20px; border-top: 20px solid transparent; border-left: 0px solid transparent; border-right: 200px solid gray; border-bottom: 0px solid gray; } .example::after { content: ""; position: absolute; bottom: -20px; border-bottom: 20px solid transparent; border-left: 0px solid transparent; border-right: 200px solid gray; border-top: 0px solid gray; } 
 <div class="example"> <p>Example</p> </div> 

Responsive? 响应? I could just hack my way trough a css solution but I'm going to recommend svg 我可以通过css解决方案破解我的方式,但我会推荐svg

 .trap-container { position: relative; /*Change this to test responsive*/ width: 400px; /*change this to test responsive*/ height: 150px; } .trap-container svg { position: absolute; } .trap-content { display: inline-block; position: relative; top: 10%; height: 80%; width: 100%; bottom: 10%; color: white; } 
 <div class="trap-container"> <svg width="100%" height="100%" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none"> <polygon points="0,10 100,0 100,100 0,90"> </polygon> </svg> <div class="trap-content">Lorem ipsum dollar si amet, Lorem ipsum dollar si amet, Lorem ipsum dollar si amet, Lorem ipsum dollar si amet, Lorem ipsum dollar si amet, Lorem ipsum dollar si amet, Lorem ipsum dollar si amet, Lorem ipsum dollar si amet, Lorem ipsum dollar si amet, Lorem ipsum dollar si amet. </div> </div> 

How does it work? 它是如何工作的? SVG is responsive by design so it will always scale to its container. SVG通过设计响应,因此它将始终扩展到其容器。
Added the preserveAspectRatio="none" so the svg scales in all directions. 添加了preserveAspectRatio =“none”,以便svg在所有方向上缩放。
The properties position:relative; 属性位置:相对; and position:absolute; 位置:绝对; are for letting you put the svg in the background so content can go on top of its shape. 是为了让你把svg放在后台,所以内容可以在其形状之上。
Since the shape is designed with inside a viewBox of 100 100 and the points of the shape range from 90-100 then there is a always a 10% gap at the bottom and top. 由于形状设计在100 100的viewBox内,并且形状的点在90-100之间,因此底部和顶部总是有10%的间隙。

The triangle at the top of the shape will always be 10% of its container basically. 形状顶部的三角形基本上总是其容器的10%。 That's why we set the top:10% and bottom:10% of the .trap-content class. 这就是我们设置顶部的原因:10%和底部:.trap-content类的10%。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM