Home > Web Front-end > CSS Tutorial > How Can I Create Diagonal Lines in a DIV Background Using CSS?

How Can I Create Diagonal Lines in a DIV Background Using CSS?

DDD
Release: 2024-12-21 14:51:16
Original
760 people have browsed it

How Can I Create Diagonal Lines in a DIV Background Using CSS?

Creating Diagonal Lines in a DIV Background Using CSS

Problem Statement

Consider the following HTML and CSS code:

<div class="preview-content">
  PREVIEW
</div>
Copy after login
.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;
}
Copy after login

The objective is to add diagonal lines to the background of the preview-content div, as seen in the following image:

[Image of a DIV with diagonal lines]

Solution

A scalable solution that dynamically adjusts to the element's dimensions can be achieved using CSS3 linear-gradients and the calc() function.

.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%);
}
Copy after login
<textarea class="crossed"></textarea>
Copy after login

The above is the detailed content of How Can I Create Diagonal Lines in a DIV Background Using CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template