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

How to Create Diagonal Lines in a DIV Background Using Only CSS?

Linda Hamilton
Linda HamiltonOriginal
2024-11-29 09:20:13566browse

How to Create Diagonal Lines in a DIV Background Using Only CSS?

Create Diagonal Lines in DIV Background with CSS

Problem:

Enhance a DIV with a preview box to display diagonal lines in the background, as seen in the accompanying image. The request specifies using CSS only if feasible.

Solution using CSS3 Linear-Gradient:

To automatically scale the lines to the element's dimensions, employ CSS3 linear-gradient combined with calc(). This solution is compatible with various versions of Chrome:

.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>

The above is the detailed content of How to Create Diagonal Lines in a DIV Background Using Only 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