Home > Web Front-end > CSS Tutorial > How to Create a Diagonal Line Background on a DIV using CSS?

How to Create a Diagonal Line Background on a DIV using CSS?

Mary-Kate Olsen
Release: 2024-12-08 18:13:11
Original
135 people have browsed it

How to Create a Diagonal Line Background on a DIV using CSS?

Adding Diagonal Lines to a DIV Background with CSS

Question:

How can you add diagonal lines to the background of a DIV element using only CSS?

Note: The desired effect is as shown in the provided image.

Solution:

To automatically adjust to the element's dimensions, utilize CSS3 linear-gradient and the calc() function, as illustrated below:

.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

Note: An HTML example would be:

<textarea class="crossed"></textarea>
Copy after login

The above is the detailed content of How to Create a Diagonal Line Background on a DIV 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template