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
Mary-Kate OlsenOriginal
2024-12-08 18:13:11203browse

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%);
}

Note: An HTML example would be:

<textarea class="crossed"></textarea>

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!

Statement:
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