Responsive triangle divs - html

I need to create a responsive triangle <div>. I was able to create it using css skewed, but it is not responsive, when I change the screen width it gets messed up. Can someone help me? Thank you very much in advance!
Here is what I want:
This is the code I have so far:
.skewed-box-one:before {
background-color: red;
content: '';
height: 100px;
width: 30.05%;
display: block;
visibility: visible;
position: absolute;
top: -40px;
transform: skewY(8deg);
border-top: 3px solid #BBDEFB;
}
.skewed-box-one:after {
background-color: red;
content: '';
height: 130px;
width: 70%;
display: block;
visibility: visible;
position: absolute;
top: -46px;
right: 0;
transform: skewY(-4deg);
border-top: 3px solid #BBDEFB;
}
<div class="skewed-box-one"></div>

You could use CSS clip-path, do note that broswer support is limited. With this tool you can generate the shape you want.
.triangle1 {
clip-path: polygon(50% 0%, 100% 84%, 100% 100%, 0 100%, 0 84%);
background: red;
width: 100%;
height: 100%;
}
.triangle2 {
clip-path: polygon(50% 0%, 0% 100%, 100% 100%);;
background: red;
width: 100%;
height: 100%;
}
.container {
width: 50%;
height: 300px;
margin-bottom: 20px;
}
<div class="container">
<div class="triangle1">1</div>
</div>
<div class="container">
<div class="triangle2">2</div>
</div>

I could solve this problem with #SuperDJ help, usingclip-path. I also find this website that helps to draw shapes:
https://bennettfeely.com/clippy/
Here is the final code i used:
.triangle1 {
position: absolute;
-webkit-clip-path: polygon(21% 96%, 0 54%, 100% 54%);
clip-path: polygon(21% 96%, 0 54%, 100% 54%);
background: #BBDEFB;
width: 100%;
height: 200px;
margin-top: -40px;
}
Thanks very much everybody that tryed to help and a special thanks to #SuperDJ!

Here is another idea more supported than clip-path using background coloration
.box-down {
height:80px;
padding-bottom:50px;
background:
linear-gradient(to bottom right,red 48%, transparent 50%) bottom right/30% 50px,
linear-gradient(to bottom left ,red 48%, transparent 50%) bottom left/70.1% 50px,
red content-box;
background-repeat:no-repeat;
}
.box-up {
height:80px;
padding-top:50px;
background:
linear-gradient(to top right,red 48%, transparent 50%) top right/70% 50px,
linear-gradient(to top left ,red 48%, transparent 50%) top left /30.1% 50px,
red content-box;
background-repeat:no-repeat;
margin-top:20px;
}
<div class="box-down"></div>
<div class="box-up"></div>

Related

How can i make a skewed slanting div box

Hello I am a beginner in CSS and HTML. I would like to create something a div like this for my school project.
I tried the transform:skewed(25deg) but it wont make my box div balance.
As the comments mention, you can achieve that kind of slant by using clip-path. You just need to make sure to match the path on both your inner + outer elements in order for the border to line up correctly.
.outside {
position: relative;
width: 70vmin;
height: 70vmin;
background: red;
-webkit-clip-path: polygon(0 0, 100% 10%, 100% 100%, 0% 100%);
clip-path: polygon(0 0, 100% 10%, 100% 100%, 0% 100%);
}
.inside {
position: absolute;
top: 5px;
left: 5px;
right: 5px;
bottom: 5px;
background: black;
-webkit-clip-path: polygon(0 0, 100% 10%, 100% 100%, 0% 100%);
clip-path: polygon(0 0, 100% 10%, 100% 100%, 0% 100%);
}
<div class="outside">
<div class="inside"></div>
</div>
If you need a transparent background simliar to the picture you ahve you have to use 2 elements for the top and the bottom and cut it away like this:
:root {
--thickness: 10px;
--color: red;
--width: 50vw;
--total-height: 80vh;
--offset-height: 20vh;
}
.clip-path-top {
width: var(--width);
height: var(--offset-height);
background-color: var(--color);
clip-path: polygon(0 0, var(--thickness) 0, 100% calc(100% - var(--thickness)), 100% 100%, calc(100% - var(--thickness)) 100%, var(--thickness) var(--thickness), var(--thickness) 100%, 0 100%);
}
.clip-path-bottom {
width: var(--width);
height: calc(var(--total-height) - var(--offset-height));
background-color: var(--color);
clip-path: polygon(0 0, var(--thickness) 0, var(--thickness) calc(100% - var(--thickness)), calc(100% - var(--thickness)) calc(100% - var(--thickness)), calc(100% - var(--thickness)) 0, 100% 0, 100% 100%, 0 100%);
}
/* for demo-purpose only */
body {
margin: 0;
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: gray;
}
<div class="clip-path-top"></div>
<div class="clip-path-bottom"></div>
Apply the skew to a pseudo element:
.box {
width: 250px;
height: 250px;
border-bottom: 5px solid red;
position: relative;
overflow: hidden;
}
.box:before {
content: "";
position: absolute;
inset: 0;
border: 5px solid red;
border-bottom: none;
transform-origin: left;
transform: skewY(10deg); /* adjust this */
}
<div class="box"></div>

How do you get this shape in HTML? [duplicate]

This question already has answers here:
Responsive CSS Trapezoid Shape
(2 answers)
Closed 2 years ago.
Can someone help me write plain html and css code for the below shape ?
Thanks in advance
HTML/CSS
.shape {
clip-path: polygon(0 19%, 100% 0, 100% 100%, 0 81%);
background-color: red;
width: 200px;
height: 400px;
}
<div class='shape'></div>
And you can make any shapes you want with clip path.
Try this website to make shapes https://bennettfeely.com/clippy/
Transparent Borders
div {
display:inline-block;
box-sizing: border-box;
height: 200px;
border: 40px solid;
border-top-color: #00000000;
border-left: 0;
border-bottom-color: #00000000;
}
<div></div>
Multiple backgrounds
div {
display: inline-block;
box-sizing: border-box;
height: 200px;
width: 50px;
background: linear-gradient(to top left, black 50%, white 51%) 0 0/100% 25% no-repeat, linear-gradient(to bottom left, black 50%, white 51%) 0 100%/100% 25% no-repeat, black;
}
<div></div>
Pseudo Elements
div {
position: relative;
display: inline-block;
box-sizing: border-box;
height: 200px;
width: 50px;
background: black;
}
div:before,div:after {
content: '';
position: absolute;
height: 25%;
width: 100%;
}
div:before {
top: 0;
background: linear-gradient(to top left,black 50%,white 51%) top/100% no-repeat ;
}
div:after {
bottom: 0;
background: linear-gradient(to bottom left,black 50%,white 51%) bottom/100% no-repeat;
}
<div></div>

How can I place a small triangle under a div that keeps showing the background image?

I am attempting to display an image, and want it to carry into a little triangle under the <div>. I have seen this question which drags the triangle across the whole page. I would like to display a triangle that only takes up a small space (like the image) Instead of the solid colored background, there's a background image.
div {
width: 100%;
background-image: url("https://www.shopmarriott.com/images/products/v2/xlrg/Marriott-platinum-stitch-bed-bedding-set-mar-101-st-gy_xlrg.jpg");
max-height: 200px;
padding: 75px;
position: relative;
background-size:cover;
padding-bottom:100px;
overflow:hidden;
}
div:after {
margin-left:-30px;
left: 50%;
content:'';
margin-left: -30px;
margin-top: 70px;
position:absolute;
border-left:30px solid #fff;
border-right:30px solid #fff;
border-top:30px solid transparent;
}
h1 {
color: white;
font-size: 36px;
text-align: center;
}
<div>
<h1>Sheets</h1>
</div>
You would have to use clip-path if your using a background image.
div {
background-image: url(https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg);
background-repeat: no-repeat;
background-size: cover;
width: 500px;
height: 500px;
-webkit-clip-path: polygon(0% 0%, 100% 0%, 100% 75%, 66% 75%, 50% 99%, 33% 75%, 0% 75%);
clip-path: polygon(0% 0%, 100% 0%, 100% 75%, 66% 75%, 50% 99%, 33% 75%, 0% 75%);
}
<div></div>
To help you create the size and style you want you can use this online Clippy tool (hint: select the "Message" pre-made shape and play with that)
.container {
position:relative;
background-color:#e15915;
height:320px !important;
width:100% !important;
}
.container:after {
content:'';
position: absolute;
top: 100%;
left: 0;
right: 0;
margin: 0 auto;
width: 0;
height: 0;
border-top: solid 50px #e15915;
border-left: solid 50px transparent;
border-right: solid 50px transparent;
}
http://jsfiddle.net/9AbYc/1/

Divide a rectangle into 2 triangles along diagonal using css

I want to make a div into 2 triangles (as shown in below, no problem if 1 is background of parent) upper one with one color and lower one with another. I dont mind how it is implemented but i want to do it in css (not javascript). I tried with css rotation, (code below), but its not responsive. In smaller or wider screen it is distorted . Any way to implement this in css?
body {
background: #eee;
}
.darker {
position: fixed;
top: -94%;
left: -10%;
width: 150%;
height: 150%;
background: #dd4f39;
-webkit-transform: rotate(30deg);
transform: rotate(30deg);
}
<div class="darker"> </div>
I found an interesting way to do this from here, which uses clip-path
.Answering my own question so that everyone can use it.
html,
body {
margin: 0;
}
body {
background: #eee;
}
.box {
width: 100vw;
height: 100vh;
background-color: #dd4f39;
clip-path: polygon(0 0, 100% 0, 100% 100%);
}
<div class="box"></div>
This is one way of doing it. But this use case is strictly with respect to vw. Just make sure to give the same value to these elements
div and it's pseudo element should have same width and border-left respectively.
div and it's pseudo element should have same height and border-top respectively.
html, body {
margin: 0;
}
.box {
width: 100vw;
height: 100vh;
background-color: white;
}
.box::after {
content: ' ';
border-top: 100vh solid #dd4f39;
border-left: 100vw solid transparent;
width: 0;
position: absolute;
}
<div class="box"></div>
JS fiddle
https://jsfiddle.net/kqsrmrss/2/
You can do that with a skewed pseudo element. The main trick is to keep the aspect ratio the same or else the sloped angle will fail
Fiddle demo
Stack snippet Note 1
body {
background: #eee;
}
.darker {
position: absolute;
top: 0;
left: 0;
width: 100%;
padding-top: 50%;
background: #dd4f39;
overflow: hidden;
}
.darker::after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: gray;
transform: skewY(26.5deg);
transform-origin: left top;
}
<div class="darker"></div>
Optionally, you can add media query to control the angle at different screen sizes
Fiddle demo 2
With a tiny script running when window resize's, you can control the angle and make it fully responsive both horizontally and vertically.
Note 1 Based on a comment, the Stack snippet might not work properly, and if, try the fiddle demos.
Please Use this code snippet.
div {
width: 100%;
height: 100px;
}
.diagonalRising {
border: 1pt solid black;
background: linear-gradient(to right bottom, #eeeeee 0%, #eeeeee 49.9%, #eeeeee 50%, #000000 51%, #dd4f39 51.1%, #dd4f39 100%);
}
.diagonalFalling {
background: linear-gradient(to right top, #eeeeee 0%, #eeeeee 49.9%, #000000 50%, #000000 51%, #dd4f39 51.1%, #dd4f39 100%);
}
.diagonalCross {
position: relative;
background: linear-gradient(to right bottom, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0) 49.9%, rgba(0, 0, 0, 1) 50%, rgba(0, 0, 0, 1) 51%, rgba(0, 0, 0, 0) 51.1%, rgba(0, 0, 0, 0) 100%);
}
.diagonalCross:after {
content: "";
display: block;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: -1;
background: linear-gradient(to right top, #ffffff 0%, #ffffff 49.9%, #000000 50%, #000000 51%, #ffffff 51.1%, #ffffff 100%);
}
<div class="diagonalRising"></div>
<div class="diagonalFalling"></div>
<div class="diagonalCross"></div>
Try this,
.box::after {
background: #E52A35
content: '';
position: absolute;
width: 100%;
height: 100vh;
background-color: #dd4f39;
clip-path: polygon(52% 13%, 104% -1%, -1% 0%);
}

Creating responsive eye focus icon

I've been trying to make responsive colored eye focus icon, but so far all I've tried has been unsuccessful.
I was trying to somewhat replicate the colors of a real eye.
I used border, box shadow, to get the colors, but that part is not scaling. Tried with outline too, but failed as well, that one wasn't even round.
The height of the div is currently static, but I would like it to be responsive. So the whole eye scales properly across different sizes.
Here's my code:
<div class="paragraph eye-focus">
<div class="eye1" width="80%">
<div class="eye2"></div>
</div>
</div>
.eye1 {
height: 200px;
height: calc(attr(width) / 2.5);
width: 75%;
background-color: white;
border-radius: 50%;
position: relative;
margin: auto;
}
.eye2 {
background-color: black;
width: 8%;
height: 12%;
border-radius: 50%;
border: 0.5em solid #a50;
box-shadow: 0 0 0 1.5em #080;
position: absolute;
top: 40%;
left: 45%;
}
.eye-focus {
position: relative;
}
jsfiddle if you'd prefer https://jsfiddle.net/xcxdp92q/
I'd like to put my solution out there.
You can use background radial-gradient to create the eye in a single element.
When adding padding in %, it is based on the width of the element. Use that to your advantage to make it responsive. If padding equals width, the element will be a square.
.eye-focus {
box-sizing: content-box;
height: 0;
width: 75%;
padding: 30% 0 0 0;
margin: 0 auto;
border-radius: 50%;
background-color: #fff;
background-image: radial-gradient(circle, #000 8%, #a50 8%, #0b0 17%, #080 33%, transparent 33%);
}
<div class="paragraph">
<div class="eye-focus"></div>
</div>
jsfiddle
If you're only supporting browsers that support gradients (and current browsers most do) then you can just use one div and do all the colors in a radial gradient. I used vw to size it like Suthan Bala suggested in their comment.
body {
background: #EEE;
}
.eye {
border-radius: 50%;
background: -moz-radial-gradient(center, ellipse cover, #000000 17%, #aa5500 18%, #008800 40%, #ffffff 41%);
background: -webkit-radial-gradient(center, ellipse cover, #000000 17%, #aa5500 18%, #008800 40%, #ffffff 41%);
background: radial-gradient(ellipse at center, #000000 17%, #aa5500 18%, #008800 40%, #ffffff 41%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#000000', endColorstr='#ffffff', GradientType=1);
width: 35vw;
height: 35vw;
}
<div class="eye">
</div>
I used the Color Gradient Generator by Colorzilla.
Try using this CSS:
.eye1 {
height: 4vw;
width: 4vw;
background-color: white;
border-radius: 50%;
position: relative;
margin: auto;
}
.eye2 {
background-color: black;
width: 6vw;
height: 6vw;
border-radius: 50%;
border: 1em solid #a50;
box-shadow: 0 0 0 3vw #080;
position: relative;
top: 8vw;
left: 43%;
}
.eye-focus {
position: relative;
}
I've been using vw a lot lately (for a year now). Very handy!