CSS leaner gradient effect [closed] - css

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to get the following style in my div border in both sides:
1. if the height of the div is X.
2. I want 4px from to top to start some gradient color.
3. i want 4px from the bottom to start some gradient color.(same color)
so it will look like this:
|4px transparent
|start white to black gradient
|
|End the black here
|start black to white gradient (the other direction)
|
|
|end the white here
|4px transparet

All that you needed to do was create the gradient and assign it to the border-image property. This is well supported in modern browsers and can be used unless you wish to support IE10-.
div {
height: 100px;
width: 200px;
border-style: solid;
border-width:0px 4px;
border-image: linear-gradient(to bottom, transparent 4px, red 4px, orange 50%, red calc(100% - 4px), transparent calc(100% - 4px));
border-image-slice: 1;
border-image-repeat: stretch;
background: beige;
}
<div>Some div</div>
Here's how to interpret the linear-gradient that was used in the above snippet:
linear-gradient(to bottom, /* the direction of the gradient */
transparent 4px, /* make it transparent till 4px from start */
red 4px, /* start with the required color exactly at 4px */
orange 50%, /* make it change from red to orange at 50% - half distance */
red calc(100% - 4px), /* change it back from orange to red at 4px before 100% */
transparent calc(100% - 4px) /* leave the last 4px as transparent again */
);

You can achieve this with some styling tricks
Firstly, instead of borders we use positioned pseudo-elements (left/right since you indicated height as the determining factor).
We use 4px top and bottom padding to create the transparent sections (and background-clip so the gradient background doesn't extend into the padding).
Then it's a simple linear gradient.
*,
::before,
::after {
box-sizing: border-box;
}
body {
background: pink;
}
div {
height: 90vh;
width: 60%;
margin: 5vh auto;
background: #c0ffee;
position: relative;
}
div::before,
div::after {
content: '';
position: absolute;
top: 0;
height: 100%;
padding: 4px 0;
width: 2px;
background-clip: content-box;
background-image: linear-gradient(to bottom, white, black 50%, white);
}
div::before {
left: -2px;
}
div::after {
right: -2px;
}
<div>
</div>
More complex gradient using calc and no additional clipping / padding
div::before,
div::after {
content: '';
position: absolute;
top: 0;
height: 100%;
width: 2px;
background-clip: content-box;
background-image: linear-gradient(to bottom,
transparent,
transparent 4px,
white 4px,
black 50%,
white calc(100% - 4px),
transparent calc(100% - 4px),
transparent);
}
*,
::before,
::after {
box-sizing: border-box;
}
body {
background: pink;
}
div {
height: 90vh;
width: 60%;
margin: 5vh auto;
background: #c0ffee;
position: relative;
}
div::before,
div::after {
content: '';
position: absolute;
top: 0;
height: 100%;
width: 2px;
background-clip: content-box;
background-image: linear-gradient(to bottom,
transparent,
transparent 4px,
white 4px,
black 50%,
white calc(100% - 4px),
transparent calc(100% - 4px),
transparent);
}}
div::before {
left: -2px;
}
div::after {
right: -2px;
}
<div>
</div>

Related

How to create line with curve in the middle in css

I need to create a line in pure CSS with a dimple in the middle. Is it possible? If so, how might I do this?
The CSS rules that I'm familiar to make the entire div to semicircular or change element border.
For example: border-radius, or perspective or border-top-radius...
Here's my take using absolutely-positioned pseudo content and a relative container. I create an oval shape in the ::after content and hide the top half of it using overflow: hidden.
.thing {
width: 400px;
height: 200px;
position: relative;
overflow: hidden;
}
.thing::before,
.thing::after {
content: '';
z-index: 1;
position: absolute;
}
.thing::before {
border-top: 2px solid black;
left: 0;
right: 0;
top: 0;
height: 2px;
}
.thing::after {
border-radius: 60%;
left: 20px;
right: 20px;
height: 300px;
border: 2px solid black;
top: -234px;
background-color: white;
}
html { margin: 3em; }
<div class="thing"></div>
jsFiddle
You can consider multiple background. A radial-gradient for the curve and linear-gradient for the small lines:
.box {
width:300px;
height:200px;
background:
linear-gradient(#000,#000) top left/70px 5px,
linear-gradient(#000,#000) top right/70px 5px,
radial-gradient(circle 100px, /*circle with 100px radius*/
transparent calc(100% - 6px), #000 calc(100% - 5px), /*around 5px border*/
#000 99%,transparent 100%)
0 -150px; /*we move the centre of the circle by -150px to top*/
background-repeat:no-repeat;
}
body {
background:pink;
}
<div class="box"></div>
You can add CSS variable to better control the different values. I will consider another syntax to better control the top lines using another radial-gradient that will be the same as the main one but with a reduce size so we only see a small part of it and we keep its last color to be black to have our lines.
.box {
--b:5px; /*border*/
--r:100px; /*radius*/
--p:50px; /*offset from top */
height:100px;
background:
radial-gradient(circle var(--r)
at 50% calc(-1*var(--p)),
transparent calc(100% - var(--b) - 1px), #000 calc(100% - var(--b)),
#000 100%)
0 0/100% var(--b),
radial-gradient(circle var(--r)
at 50% calc(-1*var(--p)),
transparent calc(100% - var(--b) - 1px), #000 calc(100% - var(--b)),
#000 99%,transparent 100%);
background-repeat:no-repeat;
}
body {
background:pink;
}
<div class="box"></div>
<div class="box" style="--rad:80px;--p:20px;"></div>
<div class="box" style="--rad:50px;--p:20px;--b:2px"></div>
<div class="box" style="--rad:100px;--p:70px;--b:8px"></div>
its not possible with border-radius , try with clip-path

How to create border-bottom with Multiple Different Colors? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm wondering how to make a border-bottom property have different colors, as shown in this image below.
There are several options:
Option 1
.box {
border-bottom: 5px solid red;
position: relative;
}
.box:after {
content: '';
position: absolute;
bottom: -10px;
height: 5px;
background: blue;
left: 0;
right: 0;
width: 100%;
}
.box:before {
content: '';
position: absolute;
bottom: -15px;
height: 5px;
background: yellow;
left: 0;
right: 0;
width: 100%;
}
<div class="box">
</div>
Options 2
Another option is to create an svg with 3 rectangles of your preferred colors, and use border-image option.
See w3schools docs for more information.
You can simply use border-image with gradient:
.box {
height:100px;
border-bottom:15px solid transparent;
border-image:linear-gradient(to bottom,pink 30%,red 30%,red 60%,green 0) 200;
}
<div class="box">
</div>
You can add after to your div and set its background color. Use linear-gradient to generate the colors you want;
Also check this Gradient Generator
#grad1 {
width: 80%;
height: 100px;
position: relative;
background-color: #e5e5e5;
}
#grad1:after {
content: "";
position: absolute;
bottom: -10px;
height: 20px;
left: 0;
width: 100%;
background: red;
/* For browsers that do not support gradients */
background: -webkit-linear-gradient(top, orange, yellow, green, cyan, blue, violet);
/* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(bottom, orange, yellow, green, cyan, blue, violet);
/* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(bottom, orange, yellow, green, cyan, blue, violet);
/* For Firefox 3.6 to 15 */
background: linear-gradient(to bottom, orange, yellow, green, cyan, blue, violet);
/* Standard syntax (must be last) */
}
}
<div id="grad1">Example</div>
use ::before instead of borber-bottom
.box {
width:600px;
height:100px;
background:#262626;
position:relative;
}
.box::before {
content:'';
position:absolute;
bottom:0;
left:0;
width:600px;
height:30px;
background:linear-gradient(to bottom, magenta 0px, magenta 6px, red 6px, red 12px, blue 12px, blue 18px, green 18px, green 24px, yellow 24px, yellow 30px);
}
<div class="box"></div>
for be outside
.box {
width:600px;
height:100px;
background:#262626;
position:relative;
}
.box::before {
content:'';
position:absolute;
bottom:-30px; /* same as minus height */
left:0;
width:600px;
height:30px;
background:linear-gradient(to bottom, magenta 0px, magenta 6px, red 6px, red 12px, blue 12px, blue 18px, green 18px, green 24px, yellow 24px, yellow 30px);
}
<div class="box"></div>

Gray out part of image

I have an image which covers an entire element using something like #myDiv {background-image: url("../images/background.jpg "); background-repeat: no-repeat; background-size: cover; background-position: center;}
Next, I would like to gray out the left side of the image similar to that shown below.
How can this be accomplished? It doesn't need to look exactly the same, but only similar.
You may use linear-gradients since you use background-image
html {
min-height: 100%;
background:
linear-gradient(to right, rgba(0, 0, 0, 0.75) 60px, transparent 60px), /* the gray, reset opacity to your needs : here 0.75 */
linear-gradient(to right, transparent 60px, red 60px, red 64px, transparent 64px), /* a red line ? */
url(http://lorempixel.com/200/200/fashion) /* and finally, image laying underneath gradients */;
background-size:
auto,
auto,
auto 100%;
}
you could play with a pseudoelement and a RGBA background, e.g.
#mydiv {
background: url(http://www.psdgraphics.com/file/cherry-wood.jpg);
width: 250px;
height: 400px;
position: relative;
}
#mydiv:before {
content: "";
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 30%;
background: rgba(255,255,255, .3);
}
Codepen: http://codepen.io/anon/pen/OyVYwe
Or you could simply add a transparent left border to the element, e.g.
box-sizing: border-box;
background-origin: border-box;
border-left: 50px rgba(255,255,255, .3) solid;
Codepen: http://codepen.io/anon/pen/ZbGNqL
Or you could use an inset box-shadow
box-shadow: 80px 0 0 0px rgba(255, 255, 255, .2) inset;
Codepen: http://codepen.io/anon/pen/avOrXE
Please do not vote for this answer as it was user3791372's comment (yet not yet an answer) and not mine. If you think it is the right approach, please provide a comment why you think so.
http://codepen.io/anon/pen/MaaWMB
<div id="mydiv">
<div id="sidebar"></div>
</div>
#mydiv {
background: url(http://www.psdgraphics.com/file/cherry-wood.jpg) bottom;
width: 230px;
height: 400px;
}
#sidebar {
background-color: white;
opacity: 0.2;
filter: alpha(opacity=20);
width: 50px;
height: 100%;
}

3D Box Shadow effect

So I know how to do a basic box shadow with CSS3. You can see that in the top of the graphic below.
The effect I'm trying to achieve is a 3D box shadow, as shown in the bottom of the graphic below.
Any ideas on how to do this with CSS3 box shadows?
Unfortunately box shadows are effectively just flat layers. However you can apply multiple box shadows to create this effect.
.box-shadow-3d{
box-shadow: 1px 1px 0px #999,
2px 2px 0px #999,
3px 3px 0px #999,
4px 4px 0px #999,
5px 5px 0px #999,
6px 6px 0px #999;
}
you can use pseudo element for as shadow
div {
background: black;
height: 100px;
width: 100px;
position: relative;
}
div:after,
div:before {
content: '';
background: grey;
position: absolute;
}
div:after {
width: 100%;
height: 20px;
left: 10px;
bottom: 0;
transform: translatey(100%) skewx(45deg);
}
div:before {
width: 20px;
height: 100%;
right: 0;
transform: translatex(100%) skewy(45deg);
top: 10px;
}
<div></div>
Here is a real 3D shadow using perspective and pseudo-element :before.
body {
background: lightblue;
}
.foo {
position: relative;
display: inline-block;
-webkit-perspective: 1000px;
-moz-perspective: 1000px;
persepctive: 1000px;
margin: 20px;
margin-top: 50px;
}
.foo .box {
transform: rotateY(-40deg);
height: 350px;
width: 250px;
background-color: black;
}
.foo:before {
content: "";
top: -15px;
position: absolute;
width: 50px;
height: 375px;
background-color: grey;
transform: translateX(215px) translateY(2.7px) rotateY(55deg)
}
<div class="foo">
<div class="box"></div>
</div>
You can stack the horizontal/vertical offsets of several box-shadows, each slightly bigger than the previous one. The more shadows you add, the more pronounced the effect. Here is a fiddle example.
div {
background: black;
height: 100px;
width: 100px;
box-shadow: 0 01px gray,
01px 0 gray,
01px 02px gray,
02px 01px gray,
02px 03px gray,
03px 02px gray,
03px 04px gray,
04px 03px gray,
04px 05px gray,
05px 04px gray,
05px 06px gray,
06px 05px gray;
}
I had some problems with these two options, so I adapted some diagonal gradients from Lea Verou's excellent book CSS Secrets. I thought about creating a gradient inside a right and bottom border via border-image, but that property does not allow edge targeting, à la border-right-image, etc.
So, I settled on using a pseudo element with two truncated corners, which seems to work pretty well. You have to be careful to adjust the width of the gradient to be 1.414 the size of half the padding, since this would be the diagonal of a square (square root of two). Also, since that's a pseudo element, be careful of the right placement. Interested to hear what you folks think.
div {
background: #bbb;
padding: 1em 1.2em;
width: 50%;
margin: 0 auto;
color: #111;
font: 150%/1.2 Georgia, Palatino, Times, serif;
position: relative;
}
div:after {
content:" ";
position:absolute;
top:0;
left: 0;
width:100%;
height:100%;
padding: 1.42em; /* (square root of gradient position) */
background: #000; /* Fallback if not supported */
background: linear-gradient(-135deg, transparent 2em, #000 0) top right,
linear-gradient(#000, #000) padding-box bottom right,
linear-gradient(45deg, transparent 2em, #000 0) bottom left;
/*I have avoided adding -webkit-, -moz and -0 prefixs for linear-gradient. You may put them in later to be extra safe*/
background-size: 50% 50%; /* There is no reason to paint the upper left quadrant, so I didn't. */
background-repeat: no-repeat;
-webkit-box-sizing: content-box; -moz-box-sizing: content-box; box-sizing: content-box;
/* Many people use border-box as default these days. Unfortunately, the box cannot be sized using border-box settings with the combination of padding in ems and percentages. So this is reset to content-box, just in case. */
z-index: -1; /* To keep the shadow behind the div*/
<div>This is a short sentence to demonstrate that our little div is responsive.</div>
Here's a little implementation, inspired by #Vitorino fernandes, in stylus...
offset = 10
border = 3
.offsetbox
margin offset
padding offset
text-align center
box-shadow inset 0 0 0 unit(border,px) black
background white
display inline-block
position relative
&:after,
&:before
content ''
background black
position absolute
&:after
width 100%
height offset
transform translatey(100%) skewx(-45deg)
right (offset/2)
bottom 0
&:before
height 100%
width offset
transform: translatex(-100%) skewy(-45deg)
left 0
top (offset/2)
I added some clip paths to #Vittorino fernandes code, to avoid white space between pseudos and make it sharper.
I added some 1px adjustments to avoid bad svg rendering problems.
You can use the variable called shadow-dimension to set the shadow width and height.
I Put it on a codePen:
https://codepen.io/silviamalavasi/pen/XWqeWEq
:root {
--shadow-dimension: 20px;
--blue: #0039a6;
}
.box-container {
position: relative;
}
.box-container>div {
border: 2px solid var(--blue);
}
.box-container>div:after, .box-container>div:before {
content: '';
background-color: var(--blue);
position: absolute;
}
.box-container>div:before {
width: calc(var(--shadow-dimension) + 1px);
height: calc(100% + 100px + 1px);
left: calc(var(--shadow-dimension) * -1);
transform: skewy(-45deg);
top: calc(0.5*var(--shadow-dimension));
clip-path: polygon(0% 0%, 100% 0%, 100% calc(100% - 100px - 2px + var(--shadow-dimension)), 0% calc(100% - 100px - 2px));
}
.box-container>div:after {
width: calc(100% + 100px);
height: calc(var(--shadow-dimension) + 1px);
left: calc(-0.5*var(--shadow-dimension) - 100px);
bottom: 1px;
transform: translateY(100%) skewx(-45deg);
clip-path: polygon(100px 0%, 100% 0%, 100% 100%, calc(100px + 2px) 100%);
}

Rectangle with two cut edges

I'm not sure what is specific name for this shape but can I just called it "half Parallelogram" ? I want make this shape purely using CSS/CSS3. Any help? or tutorial?
You can do it using pseudo-elements like below. The approach is to cut out a triangle shape from the left-bottom and top-right of the box. This method can be used with either a solid color an image inside the shape as long as the body background is a solid color. When the body background is a non-solid color this approach will not work because the border hack needs a solid color background.
The advantage of this method is that it can support cuts of different angles at each side (like in the question where the hypotenuse of the triangular cut on either side are not parallel to each other).
div {
background: red;
width: 200px;
height: 100px;
position: relative;
}
div:before {
position: absolute;
height: 0;
width: 0;
content: ' ';
border: 20px solid white;
border-color: transparent transparent white white;
border-width: 20px 0px 0px 15px;
left: 0;
top: 80px;
}
div:after {
position: absolute;
height: 0;
width: 0;
content: ' ';
border: 20px solid white;
border-color: white white transparent transparent;
left: 170px;
top: 0px;
}
.with-img {
background: url(http://lorempixel.com/100/100);
}
<div></div>
<br>
<div class="with-img"></div>
Sample 2: You can also achieve a similar effect using gradients. Just 1 gradient is enough to produce a cut of similar angle on both sides. If different angles are required then two gradients should be used. However the multiple gradient approach mentioned here will not work when the body background is a non-solid color.
div {
width: 200px;
height: 100px;
position: relative;
}
.with-single-gradient {
background: linear-gradient(45deg, transparent 5%, yellowgreen 5%, yellowgreen 90%, transparent 90.5%);
}
.with-single-gradient.image {
background: linear-gradient(45deg, white 5%, transparent 5%, transparent 90%, white 90.5%), url(http://lorempixel.com/100/100);
}
.with-multiple-gradient.image {
background: linear-gradient(45deg, transparent 0%, transparent 90%, white 90%), linear-gradient(60deg, white 10%, transparent 5%, transparent 100%), url(http://lorempixel.com/100/100);
}
<div class='with-single-gradient'></div>
<br>
<div class='with-single-gradient image'></div>
<br>
<div class='with-multiple-gradient image'></div>
Sample 3: This can also be created using SVG and is the best method yet. All that it requires is just a single path element which creates the required shape.
<svg viewBox='0 0 100 60' width='200px' height='120px'>
<path d='M0,0 80,0 100,16 100,60 10,60 0,54z' fill='yellowgreen' />
</svg>
Tested on Chrome v24, Firefox v19, Safari v5.1.7 (on Windows) and IE v10. They are older versions but should work in the latest versions also.
Note: IE versions less than 10 do not support gradients as mentioned in this SO thread.
there's no thing as straight radius, but here you have some tutorials. For weird shapes, you need to use a combination of shape and negative space, basically using figures with the same color of the background . The good news is you could use "transparent" as color, so you can "fake" this figures in an easy way. See tutorials Shapes of CSS or yuo can use a generator like CSS Shape Generator or CSS Shape Generator 2 but they will highly depend on your needs. Personally, I'd use a BG image and be a happy camper
to make this shape you have to use pseudo class.
and i hope it will help you
div { display: inline-block; margin: 20px; float: left; }
shape {
width: 208px;
height: 130px;
background: red;
position: relative; }
shape:before {
content: "";
position: absolute;
top: 0;
left: 0;
border-bottom: 29px solid red;
border-right: 29px solid #fff;
width: 179px;
height: 0; }
shape:after {
content: "";
position: absolute;
bottom: 0;
left: 0;
border-top: 29px solid red;
border-left: 29px solid #fff;
width: 42px;
height: 0; }
demo
2 gradients and background-size can be used too :
div {
width: 1440px;
height: 590px;
background:
linear-gradient(45deg, transparent 80px, #FF0000 80px) left no-repeat,
linear-gradient(-135deg, transparent 160px, #FF0000 160px) top right no-repeat;
background-size: 50% 100%;
}
<div>
</div>
1 gradients and calc() can be used too :
div {
width: 1440px;
height: 590px;
background:
linear-gradient(45deg, transparent 80px, #FF0000 80px, #FF0000 calc( 100% - 160px), transparent calc( 100% - 160px) );
}
<div>
</div>
Related to duplicate question https://stackoverflow.com/questions/36932294/how-can-i-create-the-object-in-picture-below-using-css-border-radius :
div {
width:980px;
height:460px;
background:linear-gradient(140deg,transparent 200px, #FFCB05 200px) left no-repeat,
linear-gradient(-40deg,transparent 80px, #FFCB05 80px) top right no-repeat;
background-size:50% 100% ;
}
<div>
div shape
</div>
image
<img src="http://i.stack.imgur.com/M48zP.png" />
For the second shape use this:
border-bottom-left-radius:50px;
border-top-right-radius:50px;
Check JSFiddle Demo
Edit:
Question is edited and second shape has been removed.
You can add an element with overflow: hidden;
skew transform the parent by desired angle. Unskew the pseudoelement by the negative of that angle.
Using this approach, you can also add images to background.
div {
height: 100px;
width: 220px;
overflow: hidden;
position: relative;
-webkit-transform: skewX(45deg);
-moz-transform: skewX(45deg);
transform: skewX(45deg);
}
div:before {
content: '';
position: absolute;
left: 10px;
height: 100px;
width: 200px;
background: red;
-webkit-transform: skewX(-45deg);
-moz-transform: skewX(-45deg);
transform: skewX(-45deg);
}
<div></div>
FIDDLE
FIDDLE (with image)

Resources