Rectangle border with specific length - html

I'm trying to create the following using html and css
It's rectangle with with border that has specific length, starting from top center,
the length around the rectangle should specify with percentage,
so the left rectangle should have border with length of 30%, the right should have 45% more or less.
I tried many things like gradient without success

.box {
height: 150px;
width: 150px;
background-color: red;
border-radius: 8px;
position: relative;
overflow: hidden;
}
.box-top {
display: block;
position: absolute;
background-color: blue;
height: 8px;
border-radius: 200px;
top: 0;
right:0;
left: 40%;
bottom: 0;
}
.box-right {
display: block;
position: absolute;
background-color: blue;
width: 8px;
height: 100%;
border-radius: 200px;
top: 0;
right:0;
bottom: 0;
}
.box-bottom {
display: block;
position: absolute;
background-color: blue;
height: 8px;
border-radius: 200px;
right:0;
left: 60%;
bottom: 0;
}
<div class="box">
<div class="box-top"></div>
<div class="box-right"></div>
<div class="box-bottom"></div>
</div>
You can style as much as you like this is just to give to a rough idea how you might approach this. And there can be more than 1 ways to do it.
Like I can see your outline is not inside the box but a few pixels out of it, and you can do that by tweaking the position values.

You can achieve that effect using some before or after pseudo elements.
I've given a simple way to do that below.
.box {
height: 150px;
width: 150px;
background-color: red;
border-radius: 8px;
position: relative;
overflow: hidden;
}
.box::before, .box::after {
content: '';
display: block;
position: absolute;
background-color: transparent;
pointer-events: none;
}
.box::before {
top: 0;
right:0;
left: 40%;
bottom: 0;
border-top: 8px solid blue;
border-right: 8px solid blue;
}
.box::after {
top: 0;
right:0;
left: 60%;
bottom: 0;
border-bottom: 8px solid blue;
}
<div class="box"></div>

<svg viewBox="0 0 500 500">
<style>
#curve {
fill:#0004;
box-shadow:0 0 0 8px #0004;
}
.border{
letter-spacing:-5px;
fill:crimson;
}
</style>
<path id="curve" fill="transparent" d="m100 50,h50,a25 25 0 0 1 25 25,v100,a25 25 0 0 1 -25 25,h-100,a25 25 0 0 1 -25 -25,v-100,a25 25 0 0 1 25 -25" />
<text width="500">
<textPath xlink:href="#curve" class="border">
••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
</textPath>
</text>
</svg>
This isn't possible with only html and css. If you'd make the border with box-shadow: inset 0 0 0 5px, you could use another element on top of it with a conic-gradient with hard color stops to hide the unwanted parts of the box-shadow. The percentage for the color-stops determines how much is shown.
The rounded corners on the line aren't possible like this.

Related

how to get two different shadow color horizontally in CSS?

I am trying to create a box-shadow with two different colours horizontally of a line. Is there any way to do it instead of box-shadow? I am trying to build the inner design of the screen.
.neon-rod
{
position: relative;
height: 0.2vmin;
width: 25vmin;
background: red;
left: 50vmin;
box-shadow:
0.2vmin 0 0 0.1vmin #69e;
}
<div class="neon-rod"></div>
Reference Image
Above is the reference image of the design.
Codepen:- https://codepen.io/himanshu-pal/pen/vYejNZW
I have increased the blur and spread of box-shadow property
.neon-rod
{
position: relative;
height: 5px;
width: 100px;
background: red;
left: 50vmin;
box-shadow:
0 0 8px 4px #69e;
}
<div class="neon-rod"></div>
With :before
.neon-rod {
position: relative;
height: 5px;
width: 100px;
background: #69e;
left: 50vmin;
box-shadow: 0 0 8px 4px #69e;
}
.neon-rod:before {
content: "";
position: absolute;
width: 90%;
background: #0f0;
left: 5px;
right: 5px;
top: 3px;
bottom:3px;
box-shadow: 0 0 3px 2px #0f0;
}
<div class="neon-rod"></div>
More details:
https://developer.mozilla.org/en-US/docs/Web/CSS/box-shadow
You can add more than one box-shadow and use different colors for each. Just separate the shadows with a comma.
.neon-rod
{
position: relative;
height: 1vmin;
width: 25vmin;
background: red;
left: 50vmin;
top: 10vmin;
box-shadow:
0.2vmin 0.3vmin 1.6vmin #69e8,
0.3vmin 0.4vmin 3.2vmin #69ea,
0.3vmin 0.4vmin 4.8vmin #69ec,
0.3vmin 0.4vmin 6.4vmin #69ef,
-0.2vmin -0.3vmin 0.8vmin green;
}
<div class="neon-rod"></div>

Inverse curve on side of button

so i have the following design for some "button tabs".
One side is curved, so border radius would not really be possible.
But is this type of curve even possible ?
or am i doomed to use some sort of image?
mostly looking for tips on how this might be accomplished, or somewhere i can look for a solution, since my previous tries to find a solution has yet to yield a result.
Html
<div class="tab-row">
<button>All Products<div class="tab-row__counter">20</div></button>
<button>Hardware<div class="tab-row__counter">20</div></button>
<button>Virtual<div class="tab-row__counter">20</div></button>
<button>Bundles<div class="tab-row__counter">20</div></button>
</div>
Css
.tab-row{
button{
background-color:$element-bg;
border:0;
color:$white;
width:300px;
height:90px;
margin-right:20px;
margin-top:40px;
border-radius: 5px 100px 0 0;
&:first-child{
margin-left:40px;
}
.tab-row__counter{
}
}
}
This is what i ended up with as a result,
https://codepen.io/andrelange91/pen/YzPqJXO
not exactly curved but close enough
You can try the curves by using the border-radius, transform, and transform-origin properties like,
/**
* Slanted tabs with CSS 3D transforms
* See http://lea.verou.me/2013/10/slanted-tabs-with-css-3d-transforms/
*/
body { padding: 50px;background:#20273d }
nav {
position: relative;
z-index: 1;
white-space: nowrap;
}
nav a {
position: relative;
display: inline-block;
padding: 1.5em 2em 1em 1em;
color:#9a9a9a;
text-decoration: none;
margin: 0 -7px;
}
nav a::before {
content: ''; /* To generate the box */
position: absolute;
top: 0; right: 0; bottom: .5em; left: 0;
z-index: -1;
border-radius: 10px 10px 0 0;
background: #434f78;
box-shadow: 0 2px hsla(0,0%,100%,.5) inset;
transform: perspective(5px) rotateX(2deg);
transform-origin: bottom left;
}
nav a.selected {
z-index: 2;
color:#FFF;
}
<nav class="left">
All Products
Hardware
Virtual
</nav>
You can use radial gradient also,
body { padding: 50px;background:#20273d }
nav {
position: relative;
z-index: 1;
white-space: nowrap;
}
nav a {
position: relative;
display: inline-block;
padding: 1em 5em 1.2em 1em;
color:#9a9a9a;
text-decoration: none;
margin: 0 -20px;
border: 0px none;
}
nav a::before {
content: ''; /* To generate the box */
position: absolute;
top: 0;
right: 0;
bottom: .5em;
left: 0;
z-index: -1;
background: radial-gradient(circle at top right,transparent 5.8vw, #434f78 6.8vw);
transform: perspective(10px) rotateX(1deg);
transform-origin: bottom left;
border: 0px none;
}
nav a.selected {
z-index: 2;
color:#FFF;
}
<nav class="left">
All Products
Hardware
Virtual
</nav>
Whilst this does not replicate the exact shape you're after, this does provide an example of the method I described in the comments in how to approach it. You will just need to edit the values in ::before and ::after to get it to your desired shape.
.curve {
background: blue;
width: 50px;
height: 75px;
position: relative;
}
.curve:before {
content: '';
background-image: radial-gradient(circle at 100% 100%, rgba(204, 0, 0, 0) 100px, blue 100px);
position: absolute;
top: 0;
left: 100%;
width: 100px;
height: 75px;
}
.curve:after {
content: '';
position: absolute;
width: 50px;
height: 75px;
background: blue;
border-radius: 0 0 100% 0 / 0 0 100% 0;
top: 100%;
left: 0;
}
.container {
display: flex;
align-items: flex-start;
justify-content: center;
}
.tab {
height: 150px;
width: 300px;
background: red
}
<div class="container">
<div class="tab"></div>
<div class="curve"></div>
</div>
Also take a look at Creating s-shaped curve using css

How to inscribe the following shape with CSS inside div?

FIDDLE
HTML
<div id="DiamondCenter">
<div id="triangle-topleft"></div>
</div>
CSS
#DiamondCenter {
position:fixed;
top:2%;
left:48%;
background: #24201a;
height:40px;
width:40px;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-o-transform: rotate(45deg);
transform: rotate(45deg);
z-index:20 !important;
}
#triangle-topleft {
width: 0;
height: 0;
border-top: 40px solid gray;
border-right: 40px solid transparent;
}
Using SVG:
While using SVG, you could make use of path and polyline elements to draw the required shape. As indicated by Paulie_D in comments, SVG is the better choice for such complex shapes instead of CSS (though this can be achieved with CSS also).
The approach is very simple and is as follows:
One path element for the top polygon which is drawn by joining the points at coordinates (0,50), (50,0), (100,50) and (50,70).
Another path element for the bottom polygon which is drawn by joining the points at (0,50), (50,70) and (100,50).
One polyline element for the orange border which is nothing but a line connecting (0,50), (50,70) and (100,50).
svg {
height: 100px;
width: 100px;
}
path#top {
fill: gray;
}
path#bottom {
fill: black;
}
polyline#border {
stroke: orange;
stroke-width: 2;
fill: none;
}
<svg viewBox="0 0 100 100">
<path id="top" d="M0,50 L50,0 100,50 50,70z" />
<path id="bottom" d="M0,50 L50,100 100,50 50,70z" />
<polyline id="border" points="0,50 50,70 100,50" />
</svg>
Using CSS:
You can achieve the shape provided in question by using 2 pseudo-elements which are both rotated and skewed. The dimension of each pseudo-element is calculated using Pythagoras theorem.
The shape produced using this method is responsive and can adapt to changes in dimension. Hover the shape within the snippet to see how it adapts.
*,
*:after,
*:before {
box-sizing: border-box;
}
#DiamondCenter {
position: fixed;
top: 2%;
left: 48%;
background: #24201a;
height: 40px;
width: 40px;
transform: rotate(45deg);
z-index: 20 !important;
overflow: hidden;
}
#DiamondCenter:after {
position: absolute;
content: '';
bottom: 0px;
left: -1px; /* half the width of border-left */
height: calc(100% / 1.414);
width: calc(100% / 1.414);
background: black;
border-left: 2px solid orange;
transform: rotate(40deg) skewX(-20deg);
transform-origin: bottom left;
}
#DiamondCenter:before {
position: absolute;
content: '';
top: -1px; /* half the width of border-top */
right: 0px;
height: calc(100% / 1.414);
width: calc(100% / 1.414);
background: black;
border-top: 2px solid orange;
transform: rotate(-40deg) skewY(-20deg);
transform-origin: top right;
}
/* Just for demo */
#DiamondCenter{
transition: all 1s;
}
#DiamondCenter:hover{
top: 5%;
height: 80px;
width: 80px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div id="DiamondCenter"></div>
In the below snippet, I have set different background colors for the pseudo-elements to illustrate how the shape is achieved.
*,
*:after,
*:before {
box-sizing: border-box;
}
#DiamondCenter {
position: fixed;
top: 2%;
left: 48%;
background: #24201a;
height: 40px;
width: 40px;
transform: rotate(45deg);
z-index: 20 !important;
overflow: hidden;
}
#DiamondCenter:after {
position: absolute;
content: '';
bottom: 0px;
left: -1px;
height: calc(100% / 1.414);
width: calc(100% / 1.414);
background: seagreen;
border-left: 2px solid orange;
transform: rotate(40deg) skewX(-20deg);
transform-origin: bottom left;
}
#DiamondCenter:before {
position: absolute;
content: '';
top: -1px;
right: 0px;
height: calc(100% / 1.414);
width: calc(100% / 1.414);
background: skyblue;
border-top: 2px solid orange;
transform: rotate(-40deg) skewY(-20deg);
transform-origin: top right;
}
/* Just for demo */
#DiamondCenter{
transition: all 1s;
}
#DiamondCenter:hover{
top: 5%;
height: 80px;
width: 80px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div id="DiamondCenter"></div>
This is quite a complicated shape to complete in CSS, but as shown by others it is possible.
A good alternative though would be to use SVG. Its a vector graphic so it scales brilliantly for responsiveness and is very well supported (CanIUse)
<svg width="50%" height="50%" viewBox="0 0 10 10">
<path d="M5,1
L9,5
L5,9
L1,5z" fill="grey" />
<path d="M1,5
L5,6
L9,5
L5,9z" stroke="orange" stroke-width=".1" stroke-dasharray="0,0,8.23,15" />
</svg>
You can create diamond shape using this css , suppose this is div where you want above shape id should be same as css (cut-diamond)
#cut-diamond {
border-style: solid;
border-color: transparent transparent red transparent;
border-width: 0 25px 25px 25px;
height: 0;
width: 50px;
position: relative;
margin: 20px 0 50px 0;
}
#cut-diamond:after {
content: "";
position: absolute;
top: 25px;
left: -25px;
width: 0;
height: 0;
border-style: solid;
border-color: red transparent transparent transparent;
border-width: 70px 50px 0 50px;
}
JSFiddle Demo

Make rectangle with one side curved

Is it possible to make a div same as this shape, if yes could you please share the code
I would use SVG: http://codepen.io/anon/pen/JdMVXY
<svg>
<path d="M260 150, 0 150, 0 0, 300 0 Q260 75, 260 150"
stroke="transparent" fill="#bd9" />
</svg>
When you have defined the correct aspect ratio of the box, you can also scale the SVG element with a simple CSS transformation (as shown in the example)
Result
This is possible within CSS using a single element with pseudo-elements with border-radius and background-shadow to create the curve.
div {
height: 100px;
width: 300px;
position: relative;
overflow: hidden;
}
div:before {
content: '';
position: absolute;
top: -150%;
left: 50%;
width: 200%;
padding-bottom: 200%;
border-radius: 100%;
background: none;
-webkit-box-shadow: 0px -10px 5px 300px #F15723;
box-shadow: 0px -10px 5px 300px #F15723;
z-index: -1;
}
<div></div>
Try this to make 'div' element:
<div id="test">
<div class="oposite-radius"></div>
<style>
#test {
position: relative;
margin: 30px;
width: 200px;
height: 100px;
border: 1px solid #333;
}
.oposite-radius {
position: absolute;
height: 100px;
width: 20px;
border: 1px solid #333;
background-color: #fff;
left: 180px;
border-radius: 100% 0 0 0;
border-width: 1px 0 0 1px;
}
</style>

border-image remove the background color and center of each side

I want to remove the background color of border-image and position the border-image to center of each side of my div. Any idea how I can do this?
Here is my JSFiddle.net
HTML:
<div>WELCOME</div>
CSS:
div {
background-color: #99FF00;
text-align:center;
font-family: arial;
color: #454545;
font-size: 20px;
width: 200px;
height: 100px;
line-height:100px;
margin: 50px 50px;
outline: 4px solid #000000;
border: 30px solid #FF0000;
-webkit-border-image: url(http://i58.tinypic.com/2chuwrd_th.png) 30 30 30 30;
-o-border-image: url(http://i58.tinypic.com/2chuwrd_th.png) 30 30 30 30;
border-image: url(http://i58.tinypic.com/2chuwrd_th.png) 30 30 30 30;
}
I want to achieve this:
Any help would be very much appreciated. Thanks!
Here is a working fiddle: http://jsfiddle.net/chajadan/f1pnws6v/8/
The following lines were getting in the way of the border-color change, if you remove them you'll see the border as you wanted:
-webkit-border-image: url(http://i58.tinypic.com/2chuwrd_th.png) 30 30 30 30;
-o-border-image: url(http://i58.tinypic.com/2chuwrd_th.png) 30 30 30 30;
border-image: url(http://i58.tinypic.com/2chuwrd_th.png) 30 30 30 30;
Then I refactored the code to display the same images with divs. I used this reference to vertically align the side images: How to vertically align an image inside div
There is quite possible extraneous css and/or elements present. I didn't clean it down.
I've played around a bit and come up with this FIDDLE.
If you look at the border-images definitions, the key is that the images are 'corner' images, and you can just repeat them over the middle section.
So in the fiddle, I just put the green text in the middle and absolutely positioned some ASCII diamonds. - not very elegant.
CSS
.holder {
outline: 6px solid gray;
border: 1px solid gray;
width: 200px;
height: 100px;
position: relative;
}
.diamond1 {
font-size: 40px;
position: absolute;
top: -12px;
left: 50%;
}
.diamond2 {
font-size: 40px;
position: absolute;
bottom: -10px;
left: 50%;
}
.diamond3 {
font-size: 40px;
position: absolute;
top: 20px;
left: -1px;
}
.diamond4 {
font-size: 40px;
position: absolute;
top: 20px;
right: -1px;
}
.textspan {
display: block;
border: 1px solid green;
background-color: green;
margin: 25px auto;
color: white;
height: 48px;
width: 160px;
text-align: center;
line-height: 48px;
}
You can try placing a div inside another div. The inner div contains the green-colored background. You can also use table
Instead of having the images as border-images, why not have them background-images on some divs that are absolutely positioned?
Below is the CSS explaining the divs with background images of diamonds that are absolutely positioned.
div.greenBox {
background-color: #fff;
color: #454545;
font-family: arial;
font-size: 20px;
height: 160px;
line-height: 100px;
margin: 50px;
outline: 4px solid #000000;
position: relative;
text-align: center;
width: 280px;
}
div.whiteBox {
background-color: #99ff00;
display: block;
height: 90px;
position: absolute;
right: 38px;
top: 30px;
width: 200px;
}
div.diamond1, div.diamond2, div.diamond3, div.diamond4 {
background:url('http://i58.tinypic.com/2chuwrd_th.png') no-repeat;
display:block;
width:30px;
height:30px;
}
.diamond1 {
position: absolute;
right: 46%;
top: 0;
}
.diamond2 {
position: absolute;
top: 40%;
}
.diamond3 {
bottom: 0;
position: absolute;
right: 46%;
}
.diamond4 {
position: absolute;
right: 0;
top: 44%;
}
and here is the html markup
<div class="greenBox">
<div class="diamond1"></div><div class="diamond2"></div>
<div class="whiteBox">
WELCOME
</div>
<div class="diamond3"></div><div class="diamond4"></div>
</div>