I am trying to draw a decent diagonal with linear-gradient but I can't figure out how to do it when the container is small> I am trying to get a diagonal that fits inside a 10x10px container and looks like this:
This is my best attempt.
div {
background: linear-gradient(50deg, transparent 4px, red 4px, red 5px, transparent 5px) no-repeat 0px 25px / 10px 10px;
display:block;
width:100px;
height:100px;
}
<div></div>
What I am doing wrong?
You can use a to [side] [side] linear gradient which is transparent except for the thickness of the diagonal like in the below snippet.
(Border is added only for demo and is not actually required for the gradient to work.)
div {
display: block;
width: 100px;
height: 100px;
border: 1px solid;
margin: 10px;
}
.border-2px {
background: linear-gradient(to bottom left, transparent calc(50% - 2px), red calc(50% - 1px), red calc(50% + 1px), transparent calc(50% + 2px)) no-repeat 0px 0px / 100px 100px;
}
.border-1px {
background: linear-gradient(to bottom left, transparent calc(50% - 1px), red 50%, transparent calc(50% + 1px)) no-repeat 0px 0px / 100px 100px;
}
.border-1px.small {
height: 10px;
width: 10px;
background: linear-gradient(to bottom left, transparent calc(50% - .5px), red 50%, transparent calc(50% + .5px)) no-repeat 0px 0px / 10px 10px;
}
.border-1px.small-2 {
height: 10px;
width: 10px;
background: linear-gradient(to bottom left, transparent calc(50% - 1px), #EEE calc(50% - .5px), red 50%, #EEE calc(50% + .5px), transparent calc(50% + 1px)) no-repeat 0px 0px / 10px 10px;
}
.border-1px.small-3 {
background: linear-gradient(to bottom left, transparent calc(50% - .5px), red 50%, transparent calc(50% + .5px)) no-repeat 0px 0px / 10px 10px;
}
.border-1px.small-4 {
background: linear-gradient(to bottom left, transparent calc(50% - 1px), #EEE calc(50% - .5px), red 50%, #EEE calc(50% + .5px), transparent calc(50% + 1px)) no-repeat 0px 0px / 10px 10px;
}
<div class='border-2px'></div>
<div class='border-1px'></div>
<div class='border-1px small'></div>
<div class='border-1px small-2'></div>
<div class='border-1px small-3'></div>
<div class='border-1px small-4'></div>
Your approach was not wrong but it is better to avoid angular linear gradients when creating diagonals because angular linear gradients don't always produce diagonals. Depending on the dimensions of the container, the line that is produced can be a diagonal line (or) a line anywhere within the box. You can find more information about that in my answer here. Another advantage of using the to [side][side] gradients is that it is responsive.
If gradients don't work for you then you can have a look at using SVG but I don't think you can create lines with exact thickness as you need when it comes to diagonal lines. Diagonals are not as simple as straight lines to create.
div {
position: relative;
height: 100px;
width: 100px;
}
svg {
position: absolute;
height: 10px;
width: 10px;
top: 0px;
left: 0px;
}
path {
stroke-width: 1.05;
stroke: red;
fill: none;
}
<div>
<svg viewBox='0 0 10 10'>
<path d='M0,0 10,10' />
</svg>
</div>
A demo of how to use the SVG diagonal line as a background image is available here. SVG images can be layered on top of other background images also.
You can use ::after pseudo class to do this.
div{
width:28px;
height:28px;
position:relative;}
div:after{
content:"";
position:absolute;
border-top:1px solid red;
width:40px;
transform: rotate(45deg);
transform-origin: 0% 0%;
}
<div>
</div>
White line
direction: top,left => bottom,right
.line {
background: linear-gradient(
45deg,
transparent,
transparent 45%,
#fff 45%,
#fff 55%,
transparent 55%,
transparent 100%
);
}
Related
I am trying to move the left position of the gradient. But it is not working.
Even after adding background-position property, it does not works.
header{
height:100px;
border:1px solid blue;
background: linear-gradient(to top, #e20e0e 50px, #0000 50px);
background-position-x: 50px;
}
<header></header>
Turn off background-repeat.
header{
height:100px;
border:1px solid blue;
background: linear-gradient(to top, #e20e0e 50px, #0000 50px);
background-position-x: 50px;
background-repeat: no-repeat;
}
<header></header>
You should add background-repeat: no-repeat; property.
Check out the similar question which is answered already.
CSS Background Gradient with offset
header{
height:100px;
border:1px solid blue;
background: linear-gradient(to top, #e20e0e 50px, #0000 50px);
background-position: 50px;
background-repeat: no-repeat;
}
<header></header>
Was trying to make a multi-color bottom border like in below image, but failed.
Tried to use border-image: linear-gradient(), but didn't manage. it only gets one of the colors: #707070.
a {
color: #707070 !important;
font-size: 20px;
text-decoration: none;
margin-right: 50px;
border-bottom: 5px solid;
border-image: linear-gradient(to bottom, #707070 20%, #a4c0e9 20%, #a4c0e9 80%, #707070 80%) 5;
}
<div id="nav">
HOME
PROJECTS
RESUME
</div>
The issue is that percentages are relative to the element not the border which will make the 20% bigger than 5px.
You need to consider pixel values. You also need to start from the bottom because the top reference is also the top of the element:
a {
color: #707070 !important;
font-size: 20px;
text-decoration: none;
margin-right: 50px;
border-bottom: 5px solid;
border-image:
linear-gradient(to top, #707070 1px, #a4c0e9 1px, #a4c0e9 4px, #707070 4px) 5;
}
<a>A link element</a>
Or use it as background and it will be easier to handle:
a {
color: #707070 !important;
font-size: 20px;
text-decoration: none;
margin-right: 50px;
border-bottom: 5px solid transparent;
background:
linear-gradient(to bottom, #707070 20%, #a4c0e9 20%, #a4c0e9 80%, #707070 80%) bottom/100% 5px border-box no-repeat;
}
<a>A link element</a>
Related: border-image-slice for gradient border image
Here is an example to better illustrate the issue you were having:
.box {
width:100px;
height:100px;
display:inline-block;
border-bottom:10px solid rgba(0,0,0,0.2);
}
<div class="box" style="background:linear-gradient(to bottom, #707070 20%, #a4c0e9 20%, #a4c0e9 80%, #707070 80%) border-box">
</div>
<div class="box" style="border-image:linear-gradient(to bottom, #707070 20%, #a4c0e9 20%, #a4c0e9 80%, #707070 80%) 10 fill">
</div>
<div class="box" style="border-image:linear-gradient(to bottom, #707070 20%, #a4c0e9 20%, #a4c0e9 80%, #707070 80%) 10 ">
</div>
In this example, the first box show the gradient we will be using. In the second one, we are applying it to the border using fill to also color the middle area and in the last one we are coloring only the border-bottom
It's always good to post your code, or a link to jsfiddle or codepen. You can refer help section on how to ask?
In your case, the design you have shown here doesn't look like a gradient. I have a solid color and borders on it. Gradients can be used on border, you can see it here
To achieve what you have shown I would have used :after and following style,
a{
position: relative;
padding-bottom: 20px;
&:after{
position: absolute;
content: '';
height: 10px;
width: 100%;
left: 0;
bottom: 0;
background: #a4c0e9;
border-top: 1px solid #707070;
border-bottom: 1px solid #707070;
}
}
Hello I am having trouble trying to come up with a way of adding a border of 1px to a container with a jagged border such as:
https://codepen.io/swizenfeld/pen/ZyBybW
body {
background: #f4f4f4;
}
.edge {
width: 100%;
height: 400px;
background: #fff;
margin-top: 30px;
}
.edge:before {
content: " ";
display: block;
position: relative;
width: 100%;
top:-30px;
height:30px;
background: linear-gradient(135deg, transparent 75%, white 76%) 0 50%,
linear-gradient(-135deg, transparent 75%, white 76%) 0 50%;
background-repeat: repeat-x;
background-size: 30px 30px, 30px 30px;
}
<div class="edge"></div>
Any ideas?
You need to add more linear-gradient() to show jagged border
body {
background: #f4f4f4;
}
.edge {
width: 100%;
height: 400px;
background: #fff;
margin-top: 30px;
}
.edge:before {
content: " ";
display: block;
position: relative;
width: 100%;
top:-30px;
height:30px;
background: linear-gradient(135deg, transparent 75%, white 76%) 0 50%, linear-gradient(-135deg, transparent 75%, white 76%) 0 50%, linear-gradient(45deg, red 30%, transparent 0%), linear-gradient(-45deg, red 30%, transparent 0%);
background-repeat: repeat-x;
background-size: 30px 30px, 30px 30px;
}
<div class="edge"></div>
For border-left, -bottom, -right, try to play with below snippet and see the comment also given for css properties.
body {
background: #ccc;
}
.edge {
width: 100%;
height: 400px;
background: white;
margin-top: 30px;
border-left:2px solid red;
border-bottom:2px solid red;
border-right:2px solid red;
position:relative; /*make it relative*/
}
.edge:after {
content: " ";
display: block;
position:absolute; /*make it absolute*/
width: 100%;
top:-6px; /* play with top and height too*/
height:23px;
/*background: linear-gradient(135deg, transparent 75%, white 76%) 0 50%, linear-gradient(-135deg, transparent 75%, white 76%) 0 50%, linear-gradient(45deg, red 30%, transparent 0%), linear-gradient(-45deg, red 30%, transparent 0%);*/
background: linear-gradient(45deg,white 14px, red 16px, transparent 17px), linear-gradient(-45deg, white 14px, red 16px, #ccc 17px);
background-repeat: repeat-x;
background-size: 30px 30px, 30px 30px;
}
<div class="edge"></div>
I am trying to build a box, and I want that each line/div to be fully transparent in the middle and opaque at the edges. (In the image, the circled parts should be transparent...) I wrote something like this:
.box {
height: 100px;
width: 100px;
border-left: solid green;
border-right: solid green;
border-top: solid red;
border-bottom: solid red;
}
but, of course, it does not give me the result I want.... I can not figure out any way to fix that "opaque-transparent" problem.. Any help? Thanks in advance!
You need to draw your borders via a gradient eventually: (untill border gradient is avalaible through all major browser ... chrome can do it for ages, FF still not )
.box {
height: 100px;
width: 100px;
padding:3px;
background:
linear-gradient(to left, red 33.33%, transparent 33.33%, transparent 66.66%, red 66.66%) top left no-repeat,
linear-gradient(to left, red 33.33%, transparent 33.33%, transparent 66.66%, red 66.66%) bottom left no-repeat,
linear-gradient(to top, green 33.33%, transparent 33.33%, transparent 66.66%, green 66.66%) top left no-repeat,
linear-gradient(to top, green 33.33%, transparent 33.33%, transparent 66.66%, green 66.66%)top right no-repeat;
background-size: 100% 3px, 100% 3px, 3px 100%, 3px 100%;
}
<div class="box"></div>
Box-shadow on a known sized box can do some things too :
.boxbis {
margin: 3em;
height: 200px;
width: 200px;
background: gray;
box-shadow: 70px 70px 0 -60px turquoise, 70px -70px 0 -60px pink, -70px 70px 0 -60px tomato, -70px -70px 0 -60px orange, inset 0 0 0 3px white
}
<div class="boxbis">
<div>
Is it possible to create this shape in CSS3? How?
I am stuck: http://dabblet.com/gist/2962169
h1 {
background-color: #434b82;
border-radius: 20px 0 0 20px;
transform: skew(-20deg);
}
<h1>TEST</h1>
You mean somthing like this
h1 {
background-color: #434b82;
border-radius: 20px 0 0 20px;
width:500px;
height:40px;
border-right: 40px solid transparent;
}
h1:after{
position:absolute;
width: 80px;
border-top: 40px solid #434b82;
margin-left:500px;
border-right: 20px solid transparent;
content:"";
}
<h1></h1>
We can use linear-gradient() to draw this shape on rectangular element.
This trick uses the idea of dividing whole shape in two parts and then draws each part on the background independently.
div {
background-image: linear-gradient(to left, #434b82, #434b82),
linear-gradient(to left top, transparent 50%, #434b82 50%);
background-position: top right 20px, 100% 100%;
background-size: 100% 100%, 20px 100%;
background-repeat: no-repeat;
}
div {
background-image: linear-gradient(to left, #434b82, #434b82),
linear-gradient(to left top, transparent 50%, #434b82 50%);
background-position: top right 20px, 100% 100%;
background-size: 100% 100%, 20px 100%;
background-repeat: no-repeat;
border-radius: 30px 0 0 30px;
line-height: 50px;
padding: 0 25px;
height: 50px;
width: 200px;
color: #fff;
}
<div>
Some Text Here...
</div>