CSS shape semicircle alignment problem with border of rectangle - html

I want to create a shape using CSS. The only problem I am facing is the alignment of semicircle with the border of rectangle which is not working out properly.
I am attaching the image of what I have done till now. Can anybody help me out to fix these alignment problem. Thank you.
body {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: white;
}
#talkbubble {
width: 120px;
height: 80px;
border: 4px solid #4C4C4C;
position: relative;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
}
#talkbubble:before {
content: "";
position: absolute;
top: 42%;
left: -11.6px;
width: 30px;
height: 15px;
border: 4px solid #4C4C4C;
border-bottom: none;
border-top-left-radius: 30px;
border-top-right-radius: 30px;
background-color: white;
transform: rotate(90deg);
box-sizing: border-box;
}
#talkbubble:after {
content: "";
position: absolute;
top: 42%;
right: -12px;
width: 30px;
height: 15px;
border: 4px solid #4C4C4C;
border-bottom: none;
border-top-left-radius: 30px;
border-top-right-radius: 30px;
background-color: white;
transform: rotate(270deg);
box-sizing: border-box;
}
<div id="talkbubble"></div>

I would do it differently with one element:
.ticket {
width:300px;
height:200px;
border-radius:20px;
border:8px solid;
background:
radial-gradient(circle 38px at right,#000 95%,#0000),
radial-gradient(circle 38px at left ,#000 95%,#0000);
background-origin:border-box;
-webkit-mask:
radial-gradient(circle 30px at right,#0000 95%,#000) right,
radial-gradient(circle 30px at left ,#0000 95%,#000) left;
-webkit-mask-size:51% 100%;
-webkit-mask-repeat:no-repeat;
}
<div class="ticket"></div>

You could add overflow hidden in case, and a full circle?
.ticket-outer {
overflow: hidden;
height: 150px;
width: 300px;
margin: 50px auto;
}
.ticket {
border: 5px solid #000;
border-radius: 20px;
height: 150px;
width: 300px;
position: relative;
box-sizing: border-box;
}
.ticket::before,
.ticket::after {
content: '';
width: 50px;
height: 50px;
border: 5px solid #000;
background: #fff;
border-radius: 50%;
position: absolute;
top: 50%;
left: -30px;
transform: translateY(-50%);
z-index: 2;
}
.ticket::after {
left: auto;
right: -30px;
}
<div class="ticket-outer">
<div class="ticket"></div>
</div>

Related

How can add two rounds shapes in a rectangle? [duplicate]

I want to create a shape using CSS. The only problem I am facing is the alignment of semicircle with the border of rectangle which is not working out properly.
I am attaching the image of what I have done till now. Can anybody help me out to fix these alignment problem. Thank you.
body {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: white;
}
#talkbubble {
width: 120px;
height: 80px;
border: 4px solid #4C4C4C;
position: relative;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
}
#talkbubble:before {
content: "";
position: absolute;
top: 42%;
left: -11.6px;
width: 30px;
height: 15px;
border: 4px solid #4C4C4C;
border-bottom: none;
border-top-left-radius: 30px;
border-top-right-radius: 30px;
background-color: white;
transform: rotate(90deg);
box-sizing: border-box;
}
#talkbubble:after {
content: "";
position: absolute;
top: 42%;
right: -12px;
width: 30px;
height: 15px;
border: 4px solid #4C4C4C;
border-bottom: none;
border-top-left-radius: 30px;
border-top-right-radius: 30px;
background-color: white;
transform: rotate(270deg);
box-sizing: border-box;
}
<div id="talkbubble"></div>
I would do it differently with one element:
.ticket {
width:300px;
height:200px;
border-radius:20px;
border:8px solid;
background:
radial-gradient(circle 38px at right,#000 95%,#0000),
radial-gradient(circle 38px at left ,#000 95%,#0000);
background-origin:border-box;
-webkit-mask:
radial-gradient(circle 30px at right,#0000 95%,#000) right,
radial-gradient(circle 30px at left ,#0000 95%,#000) left;
-webkit-mask-size:51% 100%;
-webkit-mask-repeat:no-repeat;
}
<div class="ticket"></div>
You could add overflow hidden in case, and a full circle?
.ticket-outer {
overflow: hidden;
height: 150px;
width: 300px;
margin: 50px auto;
}
.ticket {
border: 5px solid #000;
border-radius: 20px;
height: 150px;
width: 300px;
position: relative;
box-sizing: border-box;
}
.ticket::before,
.ticket::after {
content: '';
width: 50px;
height: 50px;
border: 5px solid #000;
background: #fff;
border-radius: 50%;
position: absolute;
top: 50%;
left: -30px;
transform: translateY(-50%);
z-index: 2;
}
.ticket::after {
left: auto;
right: -30px;
}
<div class="ticket-outer">
<div class="ticket"></div>
</div>

How to create a div with pointy edges?

can you help me to make like this div:
My Code:
body{
display: flex;
justify-content: center;
}
#talkbubble {
width: 160px;
height: 80px;
background: #bc0a14;
position: relative;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
}
#talkbubble:before {
content: "";
position: absolute;
right: 100%;
top: 26px;
width: 0;
height: 0;
border-top: 13px solid transparent;
border-right: 26px solid #bc0a14;
border-bottom: 13px solid transparent;
}
#talkbubble:after {
content: "";
position: absolute;
left: 100%;
top: 26px;
width: 0;
height: 0;
border-top: 13px solid transparent;
border-left: 26px solid #bc0a14;
border-bottom: 13px solid transparent;
}
<div id="talkbubble"></div>
I want to create this div with the same style in the image
here is an idea with pseudo element and radial-gradient. I used CSS variable to easily adjust the shape but it's not mandatory
.box {
width: 100px;
height: 50px;
margin:0 var(--w,20px);
display:inline-block;
border-radius: 15px;
background: var(--c,red);
position: relative;
}
.box:before,
.box:after {
content: "";
position: absolute;
top: 0;
bottom: 0;
width: var(--w,20px);
right:calc(100% - 2px);
background:
radial-gradient(107% 100% at top left,transparent 96%,var(--c,red) 100%) top,
radial-gradient(107% 100% at bottom left,transparent 96%,var(--c,red) 100%) bottom;
background-size:100% 50.1%;
background-repeat:no-repeat;
}
.box:after {
left:calc(100% - 2px);
right:auto;
transform:scaleX(-1);
}
<div class="box"></div>
<div class="box" style="--c:blue;--w:30px;"></div>
<div class="box" style="--c:purple;--w:10px;height:60px"></div>
please try this code:
body{
display: flex;
justify-content: center;
}
#talkbubble {
width: 180px;
height: 54px;
background: #bc0a14;
position: relative;
-moz-border-radius: 20px;
-webkit-border-radius: 20px;
border-radius: 24px;
}
#talkbubble:before {
content: "";
position: absolute;
right: 99%;
top: 17px;
width: 0px;
height: 1px;
border-top: 9px solid transparent;
border-right: 10px solid #bc0a14;
border-bottom: 9px solid transparent;
}
#talkbubble:after {
content: "";
position: absolute;
left: 99%;
top: 17px;
width: 0;
height: 1px;
border-top: 9px solid transparent;
border-left: 10px solid #bc0a14;
border-bottom: 9px solid transparent;
}
<div id="talkbubble"></div>

How can we make this shape using CSS

How can we make this shape using CSS?
I'm able to write the below code using CSS but the shape generated output is a bit off. Can we do that using CSS?
.btn-arrow {
width: 15px;
height: 24px;
border: 2px solid red;
border-top-right-radius: 40px;
border-bottom-right-radius: 40px;
border-left: 0;
display: inline-block;
position: relative;
}
.btn-arrow:after,
.btn-arrow:before {
right: 100%;
top: 50%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
}
.btn-arrow:after {
border-right-color: white;
border-width: 12px;
margin-top: -12px;
}
.btn-arrow:before {
border-right-color: red;
border-width: 14px;
margin-top: -14px;
}
body {
padding: 20px;
}
<div class="btn-arrow"></div>
With CSS you can achieve that.
Just create ::after and ::before pseudoelements and the main box rotate 45 degrees. You can adjust the degrees on the linear-gradient part instead of "to right" sentence.
This trick is necessary because border-image and border-radius can't live both on the same element.
You can see more about this:
Possible to use border-radius together with a border-image which has a gradient?
https://css-tricks.com/examples/GradientBorder/
.shape {
position:relative;
width: 100px;
border-radius: 100% 100% 100% 0;
height: 100px;
transform: rotate(45deg);
margin: 0 auto;
background: white;
background-clip: padding-box;
}
.shape::after {
position: absolute;
top: -8px;
bottom: -8px;
left: -8px;
right: -8px;
background: linear-gradient(to right, #fe3870, #fc5d3e);
content: '';
z-index: -1;
border-radius: 100% 100% 100% 0;
}
.shape::before {
position: absolute;
top: 8px;
bottom: 8px;
left: 8px;
right: 8px;
background: white;
content: '';
z-index: 1;
border-radius: 100% 100% 100% 0;
}
<div class="shape">
</div>
One of many possible solutions in just CSS:
This solution only requires one pseudo element.
.btn-arrow {
width: 44px;
height: 44px;
border-top-right-radius: 40px;
border-top-left-radius: 40px;
border-bottom-right-radius: 40px;
background: -webkit-linear-gradient(left, rgba(232,51,105,1) 0%,rgba(235,94,67,1) 100%); /* Chrome10-25,Safari5.1-6 */
transform:rotate(45deg);
position: relative;
}
.btn-arrow::after {
display: block;
width: 30px;
height: 30px;
border-top-right-radius: 40px;
border-top-left-radius: 40px;
border-bottom-right-radius: 40px;
background: white;
position: absolute;
content: '';
top: 7px;
left: 7px;
}
body {
padding: 20px;
}
<div class="btn-arrow"></div>
Adjust the CSS to look like this
.btn-arrow {
width: 30px;
height: 30px;
border: 2px solid red;
border-radius: 100%;
border-left: 0;
display: inline-block;
position: relative;
}
.btn-arrow:after,
.btn-arrow:before {
right: calc(100% - 6px);
top: 50%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
}
.btn-arrow:after {
border-right-color: white;
border-width: 12px;
margin-top: -12px;
}
.btn-arrow:before {
border-right-color: red;
border-width: 14px;
margin-top: -14px;
}
body {
padding: 20px;
}

how to add border in an arrow before a div?

I have this box with an arrow:
#div1 {
position: fixed;
width: 140px;
min-height: 100px;
max-height: 400px;
background: #fff;
color: #000;
border: 1px solid #ccc;
//border-top:none;
z-index: 3000;
}
#div1:before {
content: "";
vertical-align: middle;
margin: auto;
position: absolute;
left: 0;
right: 0;
bottom: 100%;
width: 0;
height: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 10px solid #fff;
}
<div id=div1></div>
I want the arrow to have the same border as #div1 (1px solid #ccc) but it is white.
Any ideas how can I add a border color in the arrow?
JSFiddle
#div1 {
position: relative;
width: 140px;
min-height:100px;
max-height:400px;
background: #fff;
color: #000;
border:1px solid #ccc;
z-index:3000;
margin: 3rem;
}
#div1:before {
content: "";
vertical-align: middle;
margin: auto;
position: absolute;
display: block;
left: 0;
right: 0;
bottom: calc(100% - 6px);
width: 12px;
height: 12px;
transform: rotate(45deg);
border: 1px solid;
border-color: #ccc transparent transparent #ccc;
background-color: white;
}
<div id=div1></div>
I've modified your code a bit, but I think I've got your desired output
FIDDLE
#div1 {
position: fixed;
width: 140px;
min-height: 100px;
max-height: 400px;
background: #fff;
color: #000;
border: 1px solid #ccc;
}
#div1:before {
content: '';
vertical-align: middle;
margin: auto;
position: absolute;
left: 0;
right: 0;
bottom: 93%;
width: 15px;
height: 15px;
background: #fff;
border-top: 1px solid #ccc;
border-right: 1px solid #ccc;
transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
-webkit-transform: rotate(-45deg);
}
<br>
<div id="div1"></div>

Creating angled shape using CSS

Is it possible to create a shape like this using the CSS border?
I saw some other stack overflow posts regarding making some border modifications, but nothing specifically like this. Can someone point me in the right direction?
Thanks
Based on https://css-tricks.com/examples/ShapesOfCSS/:
#base {
background: red;
display: inline-block;
height: 30px;
margin-left: 20px;
margin-top: 55px;
position: relative;
width: 200px;
text-align: center;
}
#base:before {
border-bottom: 15px solid red;
border-left: 100px solid transparent;
border-right: 100px solid transparent;
content: "";
height: 0;
left: 0;
position: absolute;
top: -15px;
width: 0;
}
<div id="base"><span>BACK TO TOP</span></div>
Just modify the width and height for your needs, it is really easy.
You can create this shape using css :before and :after selectors:
#back {
background: #fff;
border:1px solid #333;
display: inline-block;
height: 20px;
margin-left: 20px;
margin-top: 55px;
position: relative;
width: 120px;
text-align: center;
}
#back:before {
border-bottom: 15px solid #fff;
border-left: 60px solid transparent;
border-right: 60px solid transparent;
content: "";
height: 0;
left: 0;
position: absolute;
top: -15px;
width: 0;
z-index:2;
}
#back:after {
border-bottom: 15px solid #333;
border-left: 60px solid transparent;
border-right: 60px solid transparent;
content: "";
height: 0;
left: 0;
position: absolute;
top: -16px;
width: 0 ;
z-index:1;
}
<div id="back"><span>Back to Top</span></div>
Fully adaptive and transparent...
* {
margin: 0;
padding: 0;
}
body {
position: relative;
width: 100vw;
height: 100vh;
background-image: linear-gradient(rgba(0, 0, 0, .7) 0, rgba(0, 0, 0, .7) 100%), url('http://beerhold.it/1024/600');
background-repeat: no-repeat;
background-size: cover;
}
.border-arrow-top {
display: inline-block;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
color: white;
text-align: center;
font-size: 6vh;
text-transform: uppercase;
padding: 0 10vw;
padding-bottom: 2vh;
border: 3px solid white;
border-top: none;
position: relative;
}
.border-arrow-top:before,
.border-arrow-top:after {
content: '';
position: absolute;
top: 0%;
border-top: 3px solid white;
width: 50%;
}
.border-arrow-top:before {
left: 0;
transform-origin: -3px -50%;
/* x-coord: -[size of border] */
transform: skewy(-10deg);
}
.border-arrow-top:after {
right: 0;
transform-origin: calc(100% + 3px) -50%;
/* x-coord: 100% + size of border */
transform: skewy(10deg);
}
<div class="border-arrow-top">
Back to Top
</div>
I had written a tutorial for the same, arrow heads and triangles with CSS which can be read here: http://time2hack.com/2014/10/triangles-and-arrow-heads-css.html.
The trick works on the basis of borders and their colors. The direction in which arrow has to point; border of that side can be 0 and rest of the sides will create the arrow head.
The main role will be of opposite side border; if arrow has to point to top, border-bottom will create the arrow and rest can be transparent and if arrow has to point to bottom, the border-top will be of some color and other will be transparent. Similar is for arrow pointing left and right.
The transparent color will work fine in all browser except IE8 and below; for this you can set the color to the matching background, so that it is not visible.
By customizing the fiddle http://jsfiddle.net/95Xq8/ The given below is the output
Check the fiddle
.arrow-wrap{ width:125px; margin:auto; padding:100px 0;}
.arrow-button {
width: 125px;
height: 50px;
line-height: 50px;
position: relative;
background: #f00;
text-align: center; text-decoration:none; color:#000; display:block;
color:#fff;
}
.arrow-tip {
display: block;
width: 101px;
height: 115px;
margin: 0;
-webkit-transform: rotate(45deg) skew(-18deg,-23deg);
}
.arrow-tip-container {
display: block;
width: 125px;
height: 40px;
position: absolute;
top: -40px;
left: 0px;
overflow: hidden;
}
.arrow-tip-grad {
display: block;
width: 100%;
height: 100%;
background: red;
}
<div class="arrow-wrap">
<a href="#" class="arrow-button">Back to top
<span class="arrow-tip-container">
<span class="arrow-tip">
<span class="arrow-tip-grad"></span>
</span>
</span>
</a>
</div>