Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Diagonal stripes that are 1px wide

Tags:

html

css

I'm looking to give an element a background with repeating, 1px wide diagonal stripes. It seems that repeating-linear-gradient should be able to do this, but when rendered in Safari this:

background-image: repeating-linear-gradient(     45deg, black, black 1px, transparent 1px, transparent 3px ); 

Looks like this:

#thing {    height: 200px;    background-image: repeating-linear-gradient( 45deg, black, black 1px, transparent 1px, transparent 3px);  }
<div id="thing"></div>

It looks as though the browser's doing a poor job of aliasing, resulting in an uneven banding pattern. Any ideas on how I might be able to fix this, or to accomplish what I'm looking to do another way?

like image 404
Kyle Cronin Avatar asked May 26 '14 22:05

Kyle Cronin


People also ask

How do I make a diagonal line in CSS?

In this example, we will draw a diagonal line using a “-45deg” angle. For this purpose, move to the CSS section, and change the value of the transform property and transform-origin property. Here, we will set the value of the transform property as “-45deg” and transform-origin as “right”.

How do you make stripes in HTML?

Gradient Diagonal Stripes Because of multiple backgrounds (and stacking order), you can do that all together on a single element: background: /* On "top" */ repeating-linear-gradient( 45deg, transparent, transparent 10px, #ccc 10px, #ccc 20px ), /* on "bottom" */ linear-gradient( to bottom, #eee, #999 ); HTML. SCSS.

How do you add stripes in CSS?

CSS has the properties repeating-linear-gradient and repeating-radial-gradient which we can use to create stripes. These two properties are widely supported in all browsers - Firefox 10+, Chrome 26+, Safari 6.1+, IE 10, 11 and Opera 11.6+ (use prefixes for ancient browsers).


2 Answers

A little more elabourate explanation of the conundrum here: according to the Pythagoras principle (and its triples), it is impossible to have a square (which is simply two right triangles fit together) whose sides are integers that has a diagonal whose length is an integer number, too.

This is because 12 + 12 = sqrt(2)2. Since the square root of 2 is an irrational number, all derivatives of this (a square of whatever side length that is an integer number) will have a diagonal of irrational length.

This is the closest I can get — specify an integer square, but the stripes will be of non-integer width: http://jsfiddle.net/teddyrised/SR4gL/2/

#thing {     height: 200px;     background-image: linear-gradient(-45deg, black 25%, transparent 25%, transparent 50%, black 50%, black 75%, transparent 75%, transparent);     background-size: 4px 4px; } 

The side lengths of the imaginary square, tiled over your element, will be 4px wide. This means the diagonal length would be sqrt(32), and the stripe will be 1.414...px when measured diagonally across (close to 1, but not quite there), or 2px wide when measured parallel to the x or y axis.

like image 72
Terry Avatar answered Sep 19 '22 12:09

Terry


Many thanks to Terry for his suggested approach of using a standard linear-gradient with percentages and a background-size. With a bit of playing around, I have managed to obtain the exact gradient I was looking for:

enter image description here

background-image: linear-gradient(     to right top,     transparent 33%,     black 33%,     black 66%,     transparent 66% ); background-size: 3px 3px; 
like image 43
Kyle Cronin Avatar answered Sep 18 '22 12:09

Kyle Cronin