I have div for preview box
HTML:
<div class="preview-content">PREVIEW</div>
CSS:
.preview-content {
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAYAAACp8Z5+AAAAGklEQVQIW2NkYGD4D8SMQAwGcAY2AbBKDBUAVuYCBQPd34sAAAAASUVORK5CYII=) repeat;
width: 100%;
min-height: 300px;
max-height: 300px;
line-height: 300px;
text-align: center;
vertical-align: middle;
font-size: 2em;
}
Q: how to add diagonal lines to div background like in picture
note: with CSS only if possible
Thank you in advance
You can use SVG to draw the lines.
Have a play here: http://jsfiddle.net/tyw7vkvm
Almost perfect solution, that automatically scales to dimensions of an element would be usage of CSS3 linear-gradient connected with calc() as shown below. Main drawback is of course compatibility. Code below works in Firefox 25 and Explorer 10 and 11, but in Chrome (I've tested v30 and v32 dev) there are some subtle problems with lines disappearing if they are too narrow. Moreover disappearing depends on the box dimensions – style below works for
div { width: 100px; height: 100px}
, but fails fordiv { width: 200px; height: 200px}
for which in my tests0.8px
in calculations needs to be replaced with at least1.1048507095px
for diagonals to be shown and even then line rendering quality is quite poor. Let's hope this Chrome bug will be solved soon.Please check the following.
JS:
CSS:
jsfiddle
You can do it something like this:
Here is a jsfiddle.
Improved version of answer for your purpose.
All other answers to this 3-year old question require CSS3 (or SVG). However, it can also be done with nothing but lame old CSS2:
Explanation, as requested:
Rather than actually drawing diagonal lines, it occurred to me we can instead color the so-called negative space triangles adjacent to where we want to see these lines. The trick I came up with to accomplish this exploits the fact that multi-colored CSS borders are bevelled diagonally:
To make things fit the way we want, we choose an inner rectangle with dimensions 0 and LINE_THICKNESS pixels, and another one with those dimensions reversed:
Finally, use the
:before
and:after
pseudo-selectors and position relative/absolute as a neat way to insert the borders of both of the above rectangles on top of each other into your HTML element of choice, to produce a diagonal cross. Note that results probably look best with a thin LINE_THICKNESS value, such as 1px.