This question already has answers here:
Border within border CSS
(3 answers)
Closed 6 years ago.
In my project i need to draw rectangle with right arrow and it should be filled with white color background with black color border. I have tried in many ways but i didn't get right arrow with white background and black color border.
I tried follwoing code:
HTML:
<html>
<head></head>
<body>
<div class="paddingstyleleft right-arrow1">
<div><span><img src="images/referral_out.png"/> Referred To<span>
<div><strong>Dr.Sarah Willam</strong><span class="bandagealign"><span class="bandage">3</span></span></div>
<div class="datestyle"><img src="images/Date.png"/> Jul 24th,2016 | <div class="datestyle1 scheduledstatus"><span class="spanwaiting">Scheduled</span></div></div></div>
</div>
</body>
</html>
CSS:
.right-arrow1 {
display: inline-block;
position: relative;
background: white;
padding: 15px;
height: 100px;
padding:25px;
border-bottom: 1px solid grey;
width: 285px;
border: 1px solid black;
}
.right-arrow1:after {
content: '';
display: block;
position: absolute;
left: 100%;
top: 50%;
margin-top: -10px;
width: 0;
height: 0;
border-top: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 10px solid transparent;
border-left: 10px solid black;
}
you can check this fiddle for all arrows:
https://jsfiddle.net/wLxag8pn/
.arrow-up {
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 5px solid black;
}
.arrow-down {
width: 0;
height: 0;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
border-top: 20px solid #f00;
}
.arrow-right {
width: 0;
height: 0;
border-top: 60px solid transparent;
border-bottom: 60px solid transparent;
border-left: 60px solid green;
}
.arrow-left {
width: 0;
height: 0;
border-top: 10px solid transparent;
border-bottom: 10px solid transparent;
border-right: 10px solid blue;
}
Try this css for arrow with border fiddle link https://jsfiddle.net/m4dv4f7s/
div{
position: relative;
background-color: #008000;
padding: 0px 16px;
height: 40px;
line-height: 40px;
display: inline-block;
color: white;
border: 2px solid black;
border-right: none;
z-index:1;
}
div:before{
content: '';
position: absolute;
left: 100%;
z-index:-1;
width: 28px;
height: 28px;
background-color: #008000;
border-right: 2px solid black;
border-bottom: 2px solid black;
transform: rotate(-45deg) translate(-14px,-7px);
}
Related
HTML:
<div class="rectangle">Some text</div>
CSS:
.rectangle {
width: 300px;
height: 80px;
border: 5px solid red;
}
Is there any way to make div looks like in the photo?
You can use ::after and ::before to achieve the result.
.rectangle {
width: 300px;
height: 80px;
border: 5px solid red;
border-right: none;
position: relative;
}
/* for the triangular shape */
.rectangle::after {
content: "";
position: absolute;
right:-45px;
bottom: 0;
top:-5px;
width: 0;
height: 0;
border-left: 45px solid red;
border-top: 45px solid transparent;
border-bottom: 45px solid transparent;
z-index:1000;
}
/* for hiding the portion except the border
of the triangle shape */
.rectangle::before {
content: "";
position: absolute;
right:-40px;
bottom: 0;
top:0;
width: 0;
height: 0;
border-left: 40px solid white;
border-top: 40px solid transparent;
border-bottom: 40px solid transparent;
z-index:1001;
}
<div class="rectangle">Some text</div>
In case you don't need border like structure then you can avoid ::before portion and set background color to main div.
.rectangle {
width: 300px;
height: 80px;
border: 5px solid red;
border-right: none;
position: relative;
background:red;
}
.rectangle::after {
content: "";
position: absolute;
right:-45px;
bottom: 0;
top:-5px;
width: 0;
height: 0;
border-left: 45px solid red;
border-top: 45px solid transparent;
border-bottom: 45px solid transparent;
}
<div class="rectangle">Some text</div>
For more shapes refer : CSS Tricks
To keep only the border without filling the div, You can try using ::before and ::after.
Something like this:
.rectangle {
width: 200px;
height: 40px;
position: relative;
border-top: 2px solid red;
border-bottom: 2px solid red;
border-left: 2px solid red;
-moz-border-radius: 3px 0 0 3px;
-webkit-border-radius: 3px 0 0 3px;
border-radius: 3px 0 0 3px;
margin-left: 50px;
}
.rectangle::after {
content: "";
position: absolute;
left: 100%;
width: 0;
height: 0;
top: 2px;
border-top: 18px solid transparent;
border-left: 10px solid #fff;
border-bottom: 17px solid transparent;
}
.rectangle::before {
content: "";
position: absolute;
left: 100%;
width: 0;
top: -2px;
height: 0;
border-top: 22px solid transparent;
border-left: 14px solid red;
border-bottom: 22px solid transparent;
}
<div class="rectangle">Some text</div>
Consider rotating a pseudo-element by declaring a transform: rotate() property value, as demonstrated in the code snippet embedded below.
As an alternative to achieving the same behaviour declaring border property rules, this method allows borders to be declared on the element in an intuitive manner using only one pseudo-element.
Rotating an element in this way also gives you the option to fill in the element with a solid colour - allowing you more freedom in customization.
Code Snippet Demonstration:
.rectangle {
width: 300px;
height: 80px;
border: 5px solid red;
/* additional */
border-right: 0;
box-sizing: border-box;
position: relative; /* required */
}
/* Additional */
.rectangle:after {
content: "";
position: absolute;
width: 55px;
height: 55px;
border-right: 5px solid red;
border-top: 5px solid red;
box-sizing: inherit;
right: -28px;
top: 7px;
transform: rotate(45deg);
}
<div class="rectangle">Some text</div>
Check CSS Shapes
#pointer {
width: 200px;
height: 40px;
position: relative;
background: red;
}
#pointer:after {
position: absolute;
left: 0;
bottom: 0;
width: 0;
height: 0;
border-left: 20px solid white;
border-top: 20px solid transparent;
border-bottom: 20px solid transparent;
}
#pointer:before {
content: "";
position: absolute;
right: -20px;
bottom: 0;
width: 0;
height: 0;
border-left: 20px solid red;
border-top: 20px solid transparent;
border-bottom: 20px solid transparent;
}
<div id="pointer">
</div>
you have to use the pseudo class after
.rectangle {
position: relative;
width:200px;
height:40px;
margin-left:40px;
color:#FFFFFF;
background-color:red;
text-align:center;
line-height:40px;
}
.rectangle:after {
content:"";
position: absolute;
left: 100%;
top:0px;
width:0px;
height:0px;
border-top:20px solid transparent;
border-left:40px solid red;
border-bottom:20px solid transparent;
}
<div class="rectangle">Some text</div>
You can do it using :before and :after
.rectangle {
width: 300px;
height: 80px;
border: 5px solid blue;
border-right: none;
position: relative;
}
.rectangle::before {
content: '';
border-top: 5px solid blue;
width: 120px;
position: absolute;
right: -115px;
bottom: 16px;
transform: rotate(-21deg);
}
.rectangle::after {
content: '';
border-top: 5px solid blue;
width: 120px;
position: absolute;
right: -115px;
top: 16px;
transform: rotate(21deg);
}
<div class="rectangle">Some text</div>
How to create border triangle?
The only thing I can think of to make this is to make a triangle
.triangle {
width: 0;
height: 0;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
border-bottom: 20px solid #8e8e8e;
}
But this is a solid triangle, is there a way to make it look like the triangle extends the border
Create an :after or :before element that absolutely positions at the bottom of your div.
.box {
position: relative;
background-color: #F00;
border: 1px solid #000;
width: 50px;
height: 50px;
}
.box:after {
content: "";
width: 16px;
height: 16px;
position: absolute;
background-color: #FFF;
bottom: -8px; /* half of the elements width/height */
left: 50%;
transform: translateX(-50%) rotate(45deg);
border-bottom: 1px solid #000;
border-right: 1px solid #000;
}
<div class="box">
I've made the :after element white so you can see what's happening inside of it.
You need to move triangle element to under sub layout.
I added more triangle for the border design.
.balon {
width: 350px;
height: 120px;
border: 5px solid #2C6DBF;
margin: 50px auto;
position: relative;
border-radius: 8px;
}
.balon::after, .balon::before {
width: 0;
height: 0;
content: '';
border-left: 15px solid transparent;
border-right: 15px solid transparent;
border-top: 21px solid #fff;
position: absolute;
bottom: -19px;
right: 0;
left: 0;
margin: auto;
}
.balon::before {
border-left-width: 20px;
border-right-width: 20px;
border-top-width: 25px;
border-top-color: #2C6DBF;
bottom: -25px;
}
<div class="balon">
</div>
For some reason there's space between my trapezoids.
#trapezoid {
margin-top: 100px;
border-bottom: 100px solid red;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
height: 0;
width: 200px;
transform:rotate(90deg);
float: left;
}
#trapezoid2 {
margin-top: 100px;
border-bottom: 100px solid red;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
height: 0;
width: 200px;
transform:rotate(-90deg);
float: left;
}
<div id="trapezoid2"></div>
<div id="trapezoid"></div>
Is there a way to remove the space without using negative margin?
Instead of making the trapezoid horizontally and then rotating, just make it the way you want it.
#trapezoid {
margin-top:20px;
border-left:100px solid red;
border-top: 20px solid transparent;
border-bottom: 20px solid transparent;
height: 200px;
float: left;
}
#trapezoid2 {
margin-top:20px;
border-right:100px solid red;
border-top: 20px solid transparent;
border-bottom: 20px solid transparent;
height: 200px;
float: left;
}
<div id="trapezoid2"></div>
<div id="trapezoid"></div>
It's because elements still keep it's DOM flow when you do transform:rotate(-90deg);. If you remove it, you will see that two divs actually touches. You can move second element to reduce gap.
#trapezoid {
margin-top: 100px;
border-bottom: 100px solid red;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
height: 0;
width: 200px;
transform: rotate(90deg);
float: left;
/* Added code */
position: relative;
right: 140px;
}
#trapezoid2 {
margin-top: 100px;
border-bottom: 100px solid red;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
height: 0;
width: 200px;
transform: rotate(-90deg);
float: left;
}
<div id="trapezoid2"></div>
<div id="trapezoid"></div>
Do it without the rotating:
#trapezoid {
border-left: 100px solid red;
border-top: 20px solid transparent;
border-bottom: 20px solid transparent;
height: 200px;
width: 0px;
float: left;
}
#trapezoid2 {
border-right: 100px solid red;
border-top: 20px solid transparent;
border-bottom: 20px solid transparent;
height: 200px;
width: 0px;
float: left;
}
http://codepen.io/anon/pen/Wxzdrv
That gap is there because of width and transform: rotate, but you can use translateY to fix it.
#trapezoid {
margin-top: 100px;
border-bottom: 100px solid red;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
height: 0;
width: 200px;
transform: rotate(90deg) translateY(70px);
float: left;
}
#trapezoid2 {
margin-top: 100px;
border-bottom: 100px solid red;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
height: 0;
width: 200px;
transform: rotate(-90deg) translateY(70px);
float: left;
}
<div id="trapezoid2"></div>
<div id="trapezoid"></div>
I was looking at css-tricks to see how to make a triangle with css. In the comments there was a question about how to give the triangle a border.
I thought I had a solution, but the results were unexpected. How come the right half of the inner triangle is missing.
#outer {
display: block;
height: 0;
width: 0;
border-top: 50px solid red;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
}
#inner {
display: inline;
margin: 0;
position: relative;
left: -40px;
top: -6px;
height: 0;
width: 0;
border-top: 40px solid green;
border-bottom: 0 solid transparent;
border-left: 40px solid transparent;
border-right: 40px solid transparent;
}
<div id="outer">
<div id="inner"></div>
</div>
If I change the inner triangle to have display: block or display: inline-block, the right half exists, but it's gone when then inner triangle has display: inline and I don't know why.
Strange, it works on Firefox and IE.
You can make it work on Chrome with
#inner::after {
content: '\200B';
}
#outer {
display: block;
height: 0;
width: 0;
border-top: 50px solid red;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
}
#inner {
display: inline;
margin: 0;
position: relative;
left: -40px;
top: -6px;
height: 0;
width: 0;
border-top: 40px solid green;
border-bottom: 0 solid transparent;
border-left: 40px solid transparent;
border-right: 40px solid transparent;
}
#inner::after {
content: '\200B';
}
<div id="outer">
<div id="inner"> </div>
</div>
I'm trying to create an element for ONE HTML tag that uses multiple pseudo elements/classes (eg. 4 or 5). Here is the end result I am trying to achieve, however I only want one HTML element:
.main {
position: relative;
display: inline-block;
padding: 5px;
background-color: pink;
border: 1px solid red;
}
.b1, .b2, .b3 {
display: inline-block;
}
.down-arrow-border {
position: absolute;
bottom: -15px;
left: 20px;
width: 0;
height: 0;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
border-top: 15px solid red;
}
.down-arrow {
position: absolute;
bottom: -14px;
left: 21px;
width: 0;
height: 0;
border-left: 14px solid transparent;
border-right: 14px solid transparent;
border-top: 14px solid pink;
}
<div class="main">
<div class="b1">$</div>
<div class="b2">234</div>
<div class="b3">GBP</div>
<div class="down-arrow-border"></div>
<div class="down-arrow"></div>
</div>
Original Fiddle
Here is my attempt. I've only been able to use the ::before and ::after pseudo elements. How could I create the triangle element with additional pseudo elements/classes?
.b2 {
display: inline-block;
padding: 5px;
background-color: pink;
border: 1px solid red;
}
.b2::before{
content: '$';
}
.b2::after{
content: ' GBP';
}
<div class="b2">234</div>
Attemped Fiddle
Since there are more than two elements in your original (first fiddle), you cannot get this done with just one single element (because one element can at most have only two pseudo-elements attached to it).
The maximum reduction you can achieve is to do this using two elements like in the below snippet:
.main {
position: relative;
display: inline-block;
padding: 5px;
background-color: pink;
border: 1px solid red;
}
.b2 {
display: inline-block;
}
.b2::before {
content: '$';
}
.b2::after {
content: ' GBP';
}
.main::before {
position: absolute;
content: '';
bottom: -15px;
left: 20px;
width: 0;
height: 0;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
border-top: 15px solid red;
}
.main::after {
position: absolute;
content: '';
bottom: -14px;
left: 21px;
width: 0;
height: 0;
border-left: 14px solid transparent;
border-right: 14px solid transparent;
border-top: 14px solid pink;
}
<div class="main">
<div class="b2">234</div>
</div>
But wait, that would definitely not be recommended solution. As Paulie_D had mentioned in his comments you are not using the pseudo-elements the appropriate way. Pseudo-elements should be used in general for adding extra styles to the element and not content that are critical (like a currency code, its symbol etc). They should be left to the back-end program which is fetching and sending the amount value.
So the solution that I would recommend would be the following:
.b2 {
position: relative;
display: inline-block;
padding: 5px;
background-color: pink;
border: 1px solid red;
}
.b2::before {
position: absolute;
content: '';
bottom: -15px;
left: 20px;
width: 0;
height: 0;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
border-top: 15px solid red;
}
.b2::after {
position: absolute;
content: '';
bottom: -14px;
left: 21px;
width: 0;
height: 0;
border-left: 14px solid transparent;
border-right: 14px solid transparent;
border-top: 14px solid pink;
}
<div class="b2">$234 GBP</div>
If you are 100% intent on using pseudo-elements, I would give the below approach where the backend program sets data-curr-* attributes. But, it is way more easier for the backend to set the amount as a single string.
.main {
position: relative;
display: inline-block;
padding: 5px;
background-color: pink;
border: 1px solid red;
}
.b2 {
display: inline-block;
}
.b2::before {
content: attr(data-curr-symbol);
}
.b2::after {
content: ' ' attr(data-curr-code);
}
.main::before {
position: absolute;
content: '';
bottom: -15px;
left: 20px;
width: 0;
height: 0;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
border-top: 15px solid red;
}
.main::after {
position: absolute;
content: '';
bottom: -14px;
left: 21px;
width: 0;
height: 0;
border-left: 14px solid transparent;
border-right: 14px solid transparent;
border-top: 14px solid pink;
}
<div class="main">
<div class="b2" data-curr-symbol="$" data-curr-code="GBP">234</div>
</div>