Reputation: 3103
Hi,
I am aware that it is possible to achieve all kind of shapes with CSS but I havent found an example that allows you to actually put content INSIDE which is READABLE as well. I want to make a div that looks like a trapezoid and put some text in it like this:
#trapezoid {
border-left: 25px solid transparent;
border-radius: 10px;
border-right: 25px solid transparent;
border-top: 100px solid rgba(114, 230, 245, 0.3);
height: 0;
width: 100px;
}
this code draws the trapezoid but I cant put anything inside the trapezoid because everything goes below the actual shape. How should I do it?
Thank you.
Upvotes: 0
Views: 1777
Reputation: 64264
Since all the answers use 2 elements, let's post another with a single element. Uses padding to keep the text inside the trapezoid, and 2 gradients to create the background in trapezoid shape
div {
width: 300px;
padding: 0px 50px;
background-image: linear-gradient(80deg, transparent 40px, lightblue 40px), linear-gradient(-80deg, transparent 40px, lightblue 40px);
background-size: 51% 100%;
background-position: left bottom, right bottom;
background-repeat: no-repeat;
}
<div>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</div>
Another example, with border and shadows, but for this one you need 3d transform on a pseudo element
div {
width: 400px;
margin: 50px;
position: relative;
}
div:after {
content: "";
position: absolute;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
background-color: lightblue;
border: solid 2px blue;
z-index: -1;
transform: perspective(200px) rotateX(-10deg);
transform-origin: center bottom;
border-radius: 10px;
box-shadow: 10px 10px 10px gray;
}
<div>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</div>
Upvotes: 1
Reputation: 1712
Try this Fiddle
Css:
#trapezoid {
border-left: 25px solid transparent;
border-radius: 10px;
border-right: 25px solid transparent;
border-top: 100px solid rgba(114, 230, 245, 0.3);
width: 100px;
position: relative;
}
p{
top: 0;
position: absolute;
left: 0;
margin-top: -90px;
margin-left: -20px;
}
Html:
<div id="trapezoid">
<p>
fdfd
</p>
</div>
Upvotes: 0
Reputation: 3256
Here is a fiddle with it working just using negative margins: https://jsfiddle.net/cuvqstbo/
#trapezoid p {
margin-top:-80px;
}
This is the quickest way. You can also try using positioning, relative on the <div>
and absolute on the <p>
.
Upvotes: 0
Reputation: 10207
Add the text in a span and align that span width position property
HTML
<div id="trapezoid">
<span>some text</span>
</div>
CSS
#trapezoid {
border-left: 25px solid transparent;
border-radius: 10px;
border-right: 25px solid transparent;
border-top: 100px solid rgba(114, 230, 245, 0.3);
height: 0;
width: 100px;
position:relative;
}
#trapezoid span{
position:absolute;
top:10px; // change accordingly
left:10px; // change accordingly
}
You can change the position of span according to your UI.
Upvotes: 0