After hours of trying in CSS (sorry, I'm still a CSS noob) I am asking you for help:
I want a triangle to be the "bottom" of a div while filling the whole screen width, no matter what size the screen is (100%).
When the window resizes I just want the triangle to change it's width so that it still fills the whole screen width (100%) but not it's height.
At the moment the whole thing looks like this (triangle colored black only for demonstration purposes) which judging by the looks is what I want to achieve:
My code looks like this:
.top {
background-color: green;
height: 100px;
width: 100%;
}
.bottom {
background-color: red;
height: 200px;
width: 100%;
}
.triangle {
border-top: 40px solid black;
border-left: 950px solid transparent;
border-right: 950px solid transparent;
width: 0;
height: 0;
top: 107px;
content: "";
display: block;
position: absolute;
overflow: hidden;
left: 0;
right: 0;
margin: auto;
}
<div class="top">
<div class="triangle"></div>
</div>
<div class="bottom"></div>
Code on JSFiddle: http://jsfiddle.net/L8372wcs/
My problems:
Thanks a lot in advance for the help.
Put the “middle” value of the CSS vertical-align property. It will align the vertical the box’s vertical center with the baseline of the parent box plus half the x-height of the parent.
Use the default “auto” value of the CSS margin-left and margin-right properties to set the left and right margins of the <div> element. Now let’s see the result.
Use the CSS position property to set the first <div> element’s position. Add the “absolute” value. Set the CSS top and left properties to define the element’s top and left positions. With the help of the CSS width and height properties put the sizes of your <div> element.
The second <div> has the class with name "column2". The third <div> has an id with name "bottom". Use the border, height, width, and position properties to style the "main" class. The float property defines on which side of the container the elements should be placed. The position property specifies the position of the element in a document.
See http://jsfiddle.net/L8372wcs/1/
CSS (Relevant changes)
.top {
...
position: relative;
}
.triangle {
border-top: 40px solid black;
border-left: 50vw solid transparent;
border-right: 50vw solid transparent;
...
bottom: -40px;
}
The left and right borders are defined with viewport units (since your div is 100% wide). The triangle is responsive (try to resize the viewport)
The triangle position is defined with bottom: -40px;
(instead of top) and its parent has position: relative;
This will ensure that the triangle will be positioned always just below the green element (until the top border of the triangle is 40px
tall)
Result
Another approach would be to use an inline svg with the polygon element.
This way, it's width can be set to 100% and it's height can be controled independently with CSS thx to the preserveAspectRatio
svg attribute.
In the following example, the height of the triangle is fixed to 40px but you can make the height rezise according the width by removing the CSS height property and the preserveAspectRatio
attribute.
.top {
position:relative;
background-color: green;
height: 100px;
width: 100%;
}
.bottom {
background-color: red;
height: 200px;
width: 100%;
}
.triangle {
position:absolute;
top:100%;
width:100%;
height:40px;
}
<div class="top">
<svg viewbox="0 0 100 10" preserveAspectRatio="none" class="triangle" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<polygon points="0 0 100 0 50 10"/>
</svg>
</div>
<div class="bottom"></div>
You can also style the triangle (fill colors, border, opacity...) as derired either with CSS or with attributes in the SVG element. Here is an example with CSS :
.top {
position: relative;
background-color: green;
height: 100px;
width: 100%;
}
.bottom {
background-color: red;
height: 200px;
width: 100%;
}
.triangle {
position: absolute;
top: 100%;
width: 100%;
height: 40px;
fill: gold;
}
<div class="top">
<svg class="triangle" viewbox="0 0 100 10" preserveAspectRatio="none" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<polygon points="0 0 100 0 50 10" />
</svg>
</div>
<div class="bottom"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With