I want to create a Triangle (preferably using a pure CSS method) that has a two colors. The triangle can be filled to a certain height, this has to be done dynamically on a website as the triangle represents the speed of a device. I want to accomplish the following result:
The yellow part of the triangle needs to be adjustable. (I don't mind althering the CSS using jQuery but the use of images is a no-go). I've managed to create a triangle using the 'border-method' and I've have managed to partially fill a square using a background linear gradient but the combination of both is proving to be quite a challenge.
.arrowLeft{
width: 0;
height: 0;
border-style: solid;
border-width: 15px 0 15px 100px;
border-color: transparent transparent transparent #5f5f5f;
float:left;
}
Does anyone have suggestions on how to solve my problem?
You can create two triangles and position then relatively:
.arrowContainer{
width: 0;
height: 0;
border-style: solid;
border-width: 15px 0 15px 100px;
border-color: transparent transparent transparent #5f5f5f;
position: relative;
}
.arrowContainer .arrowLeft {
width: 0;
height: 0;
border-style: solid;
border-width: 11px 0 12px 80px;
border-color: transparent transparent transparent red;
position: absolute;
right: 0;
top: -11px;
}
<div class="arrowContainer">
<div class="arrowLeft"></div>
</div>
You can create the triangular shape using the border and put the gray and orange div behind it using z-index method: https://jsfiddle.net/62yj9wn5/
html:
<div class="triangle">
<div class="vshape">
</div>
<div class="orange">
<div class="gray">
</div>
</div>
</div>
css:
html, body {background-color: black}
.orange {
background-color: orange;
width: 50px;
height: 120px;
position: relative;
margin: -120px 0 0 0px;
z-index: 1;
}
.gray {
background-color: gray;
width: 50px;
// change the hight dynamically
height: 50px;
}
.vshape{
width:0px;
height: 0px;
border-style: solid;
border-width: 120px 25px 0 25px;
border-color: transparent black transparent black;
position: relative;
z-index: 2;
}
You can easily reuse those classes if multiply the object and rotate it the "other" 3 directions.
Related
I am trying to get the line on the left-side in the picture on the right side reversed
see image below:
.line-down {
width: 50px;
height: 60px;
border: solid 5px #9494b8;
border-color: transparent #9494b8 transparent transparent;
border-radius: 100%/100px 340px 0 0;
}
<div class="line-down"></div>
One way to do it is to flip the right-hand shape on its x-axis:
body{display:flex;justify-content:space-around}
.line-down {
width: 50px;
height: 60px;
border: solid 5px #9494b8;
border-color: transparent #9494b8 transparent transparent;
border-radius: 100%/100px 340px 0 0;
}
.line-down.right{
transform: scaleX(-1);
}
<div class="line-down"></div>
<div class="line-down right"></div>
You can switch the rules between the left border and the right border and it will be exactly the same shape.
.line-down.right {
border-color: transparent transparent transparent #9494b8;
border-radius: 100%/340px 100px 0 0;
}
I created a profile dialog body like this:
<div style="background-color: white;
color: black;
border-radius: 2px;
position: absolute;
top: 52px;
right: 10px;
padding: 2px">
<div style="width: 0;
height: 0;
border-style: solid;
border-width: 0 10px 15px 10px;
border-color: transparent transparent #ffffff transparent;
position: relative;
top: -10px;
right: -165px">
</div>
</div>
I want to place child little triangle div always 10-20px far away from right end of parent div. How I can make second little triangle div take its start position on right from the end of parent div?
Just make the parent div's position relative and the child's position absolute
Something like this in your example:
<div style="background-color: black;color: black;border-radius: 2px; position: relative; padding: 2px ; width:1000; height:1000;">
<div style="
position: absolute;
width: 200;
height: 200;
border-style: solid;
border-color: transparent transparent #ffffff transparent;
top: 10px;
right: 16px;
background-color: blue">
</div>
The width of the inner DIV is 20px (left border + right border). Add another 2px for the border of the outer DIV and you have 22px as your zero position for the starting of the triangle.
Therefore, if you want the triangle to start 20px from the right side of the outer DIV, it is 22px + 20px:
right:-42px;
jsFiddle Demo
div{position:relative;}
#one{position:absolute;top:52px;right:200px;padding:2px;background-color:white;color:black;border-radius: 2px; }
#two{width:0;height:0;top:-10px;right:-22px;
border-style:solid;
border-width:0 10px 15px 10px;
border-color: transparent transparent #ffffff transparent;
}
body{background:darkcyan;}
<div id="one"><div id="two"></div></div>
Here is 2 good solutions that will allow the div and its content to flow properly with other page content.
If all you need is that "triangle", use a pseudo element instead
#one {
background-color: #ddd;
color: black;
border-radius: 2px;
position: relative;
padding: 2px;
width:200px;
height:80px;
padding-right: 15px; /* make up for the triangle so inner content does not overlap */
}
#one:after {
content: ' ';
position: absolute;
top: 0;
right: 0;
width: 0;
height: 0;
border-style: solid;
border-width: 0 10px 15px 10px;
border-color: transparent transparent #f00 transparent;
}
<div id="one">
</div>
More content out side the `one` div
This one use float, also have it stay either at top/right or bottom/right.
#one {
background-color: #ddd;
color: black;
border-radius: 2px;
top: 52px;
right: 10px;
padding: 2px;
clear: both;
}
.inner {
height: 30px;
background: green;
}
#two {
float: right;
width: 0;
height: 0;
border-style: solid;
border-width: 0 10px 15px 10px;
border-color: transparent transparent #f00 transparent;
}
<div id="one">
<div id="two"></div>
Now, anything you write here, whether it is text, or another element
<div class="inner"></div>
the "two" div will always stay at the right top<br><br>
</div>
More content out side the `one` div
<br>
<br>
<div id="one">
Now, anything you write here, whether it is text, or another element
<div class="inner"></div>
the "two" div will always stay at the right top<br><br>
This one has it stay at the right bottom
<div id="two"></div>
</div>
More content out side the `one` div
As I can't upload images due to reputation points here goes me trying to explain it.
I have 2 div's than need to be separated by two slanted lines coming down towards each other forming a point in the middle. The jsfiddle will show you what I mean.
I tried this solution: http://jsfiddle.net/RZ4b8/7/
However I don't know how to make it responsive as the values are set for the border-left-width property and only works for specific browser widths. Below is the code;
.top-border { width: 100%; position: relative; left: 0; }
.top-border-left { border-top-width: 30px; border-right-width: 0; border-bottom-width: 0; border-left-width: 800px; border-color: transparent transparent transparent #ffffff; float: left; }
.top-border-left { border-style: solid solid inset solid; width: 0; height: 0; }
.top-border-right { border-top-width: 0; border-right-width: 0; border-bottom-width: 30p x; border-left-width: 800px; border-color: transparent transparent #ffffff transparent; float: right; }
.top-border-right { border-style: inset solid solid solid; width: 0; height: 0; }
jQuery/CSS Solution for a Flexible Triangular Box
In the simplest case, start with this HTML:
<div class="div1">text 1</div>
<div class="top-border">
<div class="inner"></div>
</div>
<div class="div2">text 2</div>
and apply this CSS:
.div1 {
background: purple;
height: 50px;
}
.div2 {
background: red;
height: 50px;
}
.top-border {
overflow: hidden;
}
.top-border .inner {
width: 0;
height: 0;
border-top: 50px solid lightgray; /* controls the height of the triangle */
border-left: 100px solid transparent; /* these default values are reset
by the jQuery script */
border-right: 100px solid transparent;
border-bottom: none;
margin: 0 auto;
}
and use the following jQuery script to set the correct border widths:
function setTriangleWidth() {
var baseWidth = $(".top-border").width() / 2.0;
$(".top-border .inner").css({
"border-left-width": baseWidth + "px",
"border-right-width": baseWidth + "px"
});
}
$(window).resize(function(){setTriangleWidth();});
$(document).ready(setTriangleWidth())
Demo fiddle at: http://jsfiddle.net/audetwebdesign/Gs2pc/
This demonstrates the basic concept. If you want the triangle to overlap the red text box, then you can use absolute positioning or a negative margin, for example:
.div2.ex2 {
margin-top: -50px;
position: relative;
z-index: -1;
padding-top: 50px;
}
I want to create an upward and downward facing arrow with css like the following: http://apps.eky.hk/css-triangle-generator/
However, instead of a solid color, I want to set it up so the inside is white and there is just a border around the triangle. (So the triangle would be multi-colored, one color on the inside and a different colored border).
Is this possible, and if so, how can it be done?
To create triangles with only CSS we use a zero width/height element with borders:
.arrow-up {
width : 0;
height : 0;
border-left : 50px solid transparent;
border-right : 50px solid transparent;
border-bottom : 50px solid black;
}
Since we are using borders to create the arrow, we can't just give it a border, but we can overlay one arrow on top of a slightly larger arrow to make the appearance of a border:
HTML --
<div class="top"></div>
<div class="bottom"></div>
CSS --
.top {
position : absolute;
top : 6px;
left : 10px;
width : 0;
height : 0;
z-index : 100;
border-left : 50px solid transparent;
border-right : 50px solid transparent;
border-bottom : 50px solid black;
}
.bottom {
position : absolute;
width : 0;
height : 0;
z-index : 99;
border-left : 60px solid transparent;
border-right : 60px solid transparent;
border-bottom : 60px solid red;
}
Here is a demo: http://jsfiddle.net/jasper/qnmpb/1/
Update
You can then put both of the triangle DIV elements inside a container and move that container however you want:
HTML --
<div id="container">
<div class="top"></div>
<div class="bottom"></div>
</div>
CSS --
#container {
position : relative;
top : 25px;
left : 25px;
}
Here is a demo: http://jsfiddle.net/jasper/qnmpb/3/
EDIT (2014):
I just came back to this answer and noticed that separate HTML elements are not necessary to create your double-triangle. You can use pseudo-elements, :before and :after. I.e. replace the .top selector with something like .my-element-that-needs-a-triangle:before and the .bottom selector with something like .my-element-that-needs-a-triangle:after.
I think you could get a good idea of what to do by checking out this tutorial on pure css thought bubbles. It's doing what you're looking for.
http://www.sitepoint.com/pure-css3-speech-bubbles/
Depending on how you're using it, you can make a triangle, with a border and even box shadow, without the triangle border hack, using CSS transform: rotate(). See my answer here: https://stackoverflow.com/a/8867645/918414
If you want to create a triangle with borders (or box shadow look-alike) in pure CSS, you should use pseudo-elements :before and :after.
In my example, I added display:inline-block; to the element .arrow-dropdown to be able to create some kind of dropdown menu with a drop shadow. It is followed by .arrow-only which is a a basic triangle with a red border.
body {
margin: 10px;
background: #1670c4;
}
.text {
display: inline-block;
font-size: 15px;
color: #fff;
text-shadow: 1px 1px 2px rgba(0,0,0,0.4);
cursor: default;
}
.arrow-dropdown {
display: inline-block;
position: relative;
vertical-align: middle;
margin: 1px 0 0 8px;
width: 8px;
height: 7px;
}
.arrow-dropdown:after {
content: '';
position: absolute;
border-style: solid;
border-width: 7px 4px 0;
border-color: #fff transparent transparent transparent;
display: block;
width: 0;
z-index: 1;
}
.arrow-dropdown:before {
content: '';
position: absolute;
border-style: solid;
border-width: 8px 5px 0;
border-color: rgba(0,0,0,0.3) transparent transparent transparent;
display: block;
width: 0;
z-index: 0;
}
.arrow-only {
position: relative;
vertical-align: middle;
margin: 10px 0 0 8px;
width: 8px;
height: 7px;
}
.arrow-only:after {
content: '';
position: absolute;
border-style: solid;
border-width: 12px 9px 0;
border-color: #fff transparent transparent transparent;
display: block;
width: 0;
z-index: 1;
}
.arrow-only:before {
content: '';
position: absolute;
border-style: solid;
border-width: 15px 11px 0;
border-color: #f00 transparent transparent transparent;
display: block;
width: 0;
z-index: 0;
margin:-1px 0 0 -2px;
}
<div class="text">
Dropdown text
</div><div class="arrow-dropdown"></div>
<div class="arrow-only"></div>
I have a div with different colors for both the border-bottom and border-right properties.
So they are separated via a line leaving the box in a 45 degree angle.
How can I make the bottom-border shorter so that the right border goes all the way to the bottom of the element which would yield a 90 degree angle separator-line?
You can do this with box-shadow.
Demo:
Output:
CSS:
#borders {
border-bottom: 20px solid black;
box-shadow: 20px 0 0 0 red;
height: 150px;
margin: 30px;
width: 150px;
}
HTML:
<div id="borders"></div>
I solved this issue using border-width. You simply reduce the width of the border at the edges you don't want to see.
If we don't want the border on the upper edge, we can put border-width to 0.
border-width: 0px 5px 5px 5px;
border-color:#ddd #000 #000 #000;
Sad fact: Border corners are mitered. Always. (It's only visible if using different colors.)
In order to simulate a butt joint, you can stack two divs to get a simulated result:
div {
position: absolute;
left: 0;
top: 0;
height: 100px;
width: 100px;
}
<div style="border-left: 2px solid #ff0000; border-bottom: 2px solid #ff0000;">
</div>
<div style="border-right: 2px solid #00ff00; border-top: 2px solid #00ff00;">
</div>
Stack more or control the top and bottom differently for better control over the appearance of the joint.
For the top border and the bottom border, you can use box-shadow:
.box {
border: 10px solid #ddd;
border-top: 0;
border-bottom: 0;
box-shadow: 0 10px 0 #D03FBE, 0px -10px 0 #D03FBE;
float: left;
width: 100px;
height: 100px;
}
<div class="box"></div>
What you are seeing is that borders on different sides will split diagonally around the corner:
.border {
border: 10px solid;
border-top-color: forestgreen;
border-right-color: gold;
border-bottom-color: steelblue;
border-left-color: firebrick;
width: 40px;
height: 40px;
}
<div class="border"></div>
This is a behavior many use to create CSS triangles
To overcome this I can find 2 solutions: borders on a wrapper element, or linear gradients:
Option 1: Wrapper elements
.wrapper {
border-bottom: 10px solid steelblue;
height: 40px;
width: 50px;
}
.border {
border-right:10px solid gold;
height: 40px;
width: 40px;
}
<div class="wrapper">
<div class="border"></div>
</div>
Note how the wrapper element has height of 5px more then the child. This is essential for the borders to align.
Option 2: Linear Gradients
.border {
border-bottom: 10px solid;
border-right: 10px solid;
border-image: linear-gradient(to top, steelblue, steelblue 10px, gold 5px, gold) 10;
height: 40px;
width: 40px;
}
<div class="border"></div>
If you're looking for square ends on your borders, you can set two of the borders to 0px and then run a dummy animation like so :
#keyframes widthSet {
to{
border-right-width: 10px; //or top and bottom, your choice
border-left-width: 10px;
}
}
with animation-fill-mode: forwards;
You can't.
For 90˚ angles you could just use colored divs.
You could get a similar effect for arbitrary angles by using skew transitions and absolute positioning, but it will be hard (if not impossible) to get it to look the same in older browsers (IE8 and lower will particular be a problem).