Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a perfect diagonal with linear-gradient

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:

enter image description here

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?

like image 662
Cain Nuke Avatar asked Jun 04 '16 08:06

Cain Nuke


1 Answers

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.

like image 70
Harry Avatar answered Nov 14 '22 09:11

Harry