How to make a pure css triangle which has a white center - html

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>

Related

How to design disjoint triangular corner on top side of a div

How can I design this triangular corner marked with yellow color on top side of the inside div?
Make a triangle then position that by position: absolute. more explanation in code:
.box {
width: 200px;
height: 200px;
background-color: white;
border: 12px solid #aaa;
}
.triangle-left {
width: 0; /* this five code lines for making a triangle */
height: 0;
border-top: 8px solid transparent;
border-right: 8px solid white;
border-bottom: 8px solid transparent;
position: absolute; /* this three code line for positioning of triangle */
top: 60px;
left: 10px;
}
<div class="box"></div>
<div class="triangle-left"></div>
See this for how making a triangle: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_shapes_triangle-left
try this one!
anything you want is here,
but first try to make a container then copy the code! https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_shapes_triangle-left

CSS Triangle with partial fill

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.

How to pixel-perfect mockup this border?

I'm trying to mockup this design:
But, I can't render the red border correctly. I tried with the obvious solution:
border: 1px solid #939393;
border-left: 4px solid red;
But It's affected by the top and bottom borders, leaving the red stripe with diagonal corners, as you can see in this fiddle:
http://jsfiddle.net/anp0e03k/
Is there any way correct way to fix this?
The only thing that I can think is to add a div inside with red background and negative margins on top and bottom, but it seems to be an overkill and would love to find something that doesn't ruins the html semantic.
Apply the left border to a :before pseudo element of the div and remove the divs left border.
Compatibility: All modern browsers and IE8 +
Give the :before
height: 100% to span the entire height of your div
margin-top: -1px to overlap the top border
padding-bottom: 2px to overlap the bottom border
Then use either
position: absolute on the :before with position: relative on the div like this example:
body {
background-color: #c2c2c2;
}
div {
margin: 50px;
background-color: #FFF;
border: 1px solid #939393;
height: 50px;
width: 200px;
border-left: none;
position: relative;
}
div:before {
content: '';
display: block;
border-left: 4px solid red;
height: 100%;
margin-top: -1px;
padding-bottom: 2px;
position: absolute;
top: 0;
left: 0;
}
<div>
</div>
or
display: inline-block for the :before like this example:
Note: You will probably want to use vertical-align: top / middle / bottom for the :before. This example uses the value top.
body {
background-color: #c2c2c2;
}
div {
margin: 50px;
background-color: #FFF;
border: 1px solid #939393;
height: 50px;
width: 200px;
border-left: none;
}
div:before {
content: '';
display: inline-block;
border-left: 4px solid red;
height: 100%;
margin-top: -1px;
padding-bottom: 2px;
vertical-align: top;
}
<div>
There is text in this
</div>
Final result

CSS triangle with background image [duplicate]

This question already has answers here:
How do CSS triangles work?
(23 answers)
Creating a transparent arrow above image in CSS3
(2 answers)
Closed 9 years ago.
Respected stackoverflowers,
How do i create a triangle element with the background pattern?
For example i need the div like this :
But my state is like this :
All examples with triangle elements use borders which cant have an img in that ....
This is my subsection class which needs the coolarrow:
<div class="subsection"><span>Ryan Gosling, Mr Landlord</span></div>
.subsection {
.box-shadow (0, -1px, 1px, 0, rgba(0, 0, 0, 0.3));
background: url('/assets/pattern-lorem.png'); // The inner part of the slider have the pattern
display: block;
clear: both;
float: left;
width: 100%;
padding-top: 15px;
padding-bottom: 15px;
display: none;
}
.subsection {
position:relative;
}
.subsection:before {
content:'';
position:absolute;
top:0;
left:0;
height:20px;
width:0;
border-left:20px solid white;
border-bottom:16px solid transparent;
}
.subsection:after {
content:'';
position:absolute;
top:36px;
left:0;
bottom:0;
width:0;
border-left:20px solid white;
border-top:16px solid transparent;
}
And im getting :
Which is fine ...how can i bring the arrow on the top in the required form ? ... and overlaying the cases div ? ...
Thanks.
If you don't care for cross browser compatibility, you can use a pseudo-element that you rotate by 45 degrees and attach the styles to it. The only thing you need additionally would be the background, rotated (back) by 45deg to attach to the pseudo element:
div.coolarrow:before {
content: '';
position: absolute;
z-index: -1;
top: -24.7px;
left: 10px;
background-color: #bada55;
width: 50px;
height: 50px;
background: url(url/to/your/45deg/rotated/background.gif);
box-shadow: inset 0 0 10px #000000;
transform: rotate(45deg);
}
Here's a short fiddle to illustrate (without background):
Fiddle
To work this out for other cases but 90degree arrows, you need to skew the rect additionaly. And I don't really know what then happens with the background image...
Put the image as a background for a div, and just put negative values for the margin to make it overlay on the bar. Example (although estimated, in no way do I claim this to work) would be margin-left: -20px; margin-top: -20px; and have it after the line.
Alternatively go with #Py's answer, and you can use this CSS for the arrow, and do the same negative margins to make it line up.
#triangle-up { width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px solid transparent; border-bottom: 100px solid red; margin-left: -20px; margin-top: -20px; }
go on http://apps.eky.hk/css-triangle-generator/ and generate it :D
OR
#triangle {
width: 0;
height: 0;
border-bottom: 120px solid green;
border-left: 60px solid transparent;
border-right: 60px solid transparent;
}

Boxes with folded corner and box shadow over an unpredictable background

I'm trying to do something like this:
The boxes have shadows and the background of the corners must be transparent because they are over an image (unpredictable background).
After searching Google, I found solutions using pseudo selectors :before and :after as well as solutions using extra markup, but all of them use a fixed colour background. These were my results:
I'm trying to use box-shadows and only a small image for the corner, instead of a large complete background.
How I can do this?
Use both the pseudo-elements, one for the upper box, the other for the white triangle:
h1 {
background: #F0B032;
box-shadow: 1px 1px 4px #362708;
line-height: 30px;
position: relative;
}
h1:before, h1:after {
content: '';
position: absolute;
left: 100%;
}
​h1:before {
background: #F0B032;
box-shadow: 1px 1px 2px #362708;
width: 15px;
height: 16px;
top: 0;
}
h1:after {
border: 7px solid transparent;
border-left-color: #fff;
border-top-color: #fff;
bottom: 0;
}
​Here's the fiddle: http://jsfiddle.net/Kjp6v/
This does not add a shadow under the fold, but looks realistic enough.