6

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?

3
  • You might want to use this tool: colorzilla.com/gradient-editor Commented Jun 4, 2016 at 12:26
  • Have you tried 45deg instead of 50deg? Commented Jun 4, 2016 at 13:18
  • Yes, I tried many angles and had the same results. Commented Jun 4, 2016 at 14:14

3 Answers 3

11

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.

Sign up to request clarification or add additional context in comments.

26 Comments

To be fair, I can get the diagonal right with big containers but I cant with small ones like in my example. I need a diagonal that is 1px width across a 10x10px square but the line is either too wide or blurred. Can you make an example with a diagonal that small? Thank you.
Can you make it thinner as in the image I posted on my question? Thats exactly my issue.
No, you've got to use either the second last or the last. I think the last div in the snippet is the closest to the image in question (which I saw just now). If you try to make anything in between it would get blurred. That's how gradients work.
@CainNuke: I've added a slightly different variant for .border-1px.small-4 and .border-1px.small-2 by introducing another whitish color in between. It avoids blurs to an extent and looks slightly thicker than .border-1px.small-1 but thinner than the original .border-1px.small-4. Check if it helps. I don't think there can be anything closer than this.
Thats incredible. I think this is th solution to all my problems. Do you use any special tool to draw the SVG or do it manually?
|
1

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>

1 Comment

Thanks but that would not work in this case because I will have multiple linear-gradients on the same element and using rotate will rotate all of them which is not what I intend. Is it impossible to achieve a single diagonal line only with linear-gradient?
0

White line

direction: top,left => bottom,right

.line { background: linear-gradient( 45deg, transparent, transparent 45%, #fff 45%, #fff 55%, transparent 55%, transparent 100% ); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.