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; }
note: with CSS only if possible
Thank you in advance.
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.
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.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With