Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

draw diagonal lines in div background with CSS

I have a div for a 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; } 

Question: how to add diagonal lines to div background like in the picture?

note: with CSS only if possible

preview

Thank you in advance.

like image 999
Ing. Michal Hudak Avatar asked Aug 02 '13 08:08

Ing. Michal Hudak


People also ask

How do I make a background line diagonal in CSS?

From a CSS point of view, a diagonal line is nothing but a horizontal line which is rotated at +45 or -45 degrees angle. So, to draw a diagonal line in CSS, we have to simply rotate a normal horizontal line at an angle of + or – 45 degrees. The rotation in CSS is done using the transform property.

How do I make an oblique line in CSS?

In order to make oblique lines with CSS, there are two main approaches. The first approach involves the clip-path property of CSS and in the second approach, we use transform property with skew() of CSS. Approach 1: Using clip-path property: The clip-path property is generally used to clip an element to a basic shape.


2 Answers

Solution that automatically scales to dimensions of an element would be usage of CSS3 linear-gradient connected with calc() as shown below. (There were some issues with Chrome in times of v30+ versions that this answer originally described but looks like they got resolved in the meantime and in v90+ it works as expected).

.crossed {      background:           linear-gradient(to top left,              rgba(0,0,0,0) 0%,              rgba(0,0,0,0) calc(50% - 0.8px),              rgba(0,0,0,1) 50%,              rgba(0,0,0,0) calc(50% + 0.8px),              rgba(0,0,0,0) 100%),          linear-gradient(to top right,              rgba(0,0,0,0) 0%,              rgba(0,0,0,0) calc(50% - 0.8px),              rgba(0,0,0,1) 50%,              rgba(0,0,0,0) calc(50% + 0.8px),              rgba(0,0,0,0) 100%); }
<textarea class="crossed"></textarea>
like image 147
RobertT Avatar answered Oct 04 '22 03:10

RobertT


You can do it something like this:

<style>     .background {         background-color: #BCBCBC;         width: 100px;         height: 50px;         padding: 0;          margin: 0     }     .line1 {         width: 112px;         height: 47px;         border-bottom: 1px solid red;         -webkit-transform:             translateY(-20px)             translateX(5px)             rotate(27deg);          position: absolute;         /* top: -20px; */     }     .line2 {         width: 112px;         height: 47px;         border-bottom: 1px solid green;         -webkit-transform:             translateY(20px)             translateX(5px)             rotate(-26deg);         position: absolute;         top: -33px;         left: -13px;     } </style> <div class="background">     <div class="line1"></div>     <div class="line2"></div> </div> 

Here is a jsfiddle.

Improved version of answer for your purpose.

like image 45
AxelPAL Avatar answered Oct 04 '22 02:10

AxelPAL