search
HomeWeb Front-endCSS TutorialSample code sharing for making borders using CSS3's linear gradient

Linear-gradient lines are quite powerful for making borders, especially using their strokes to create some copied border effects. Here we will take a look at the sample code sharing of using CSS3 linear gradient linear-gradient to make borders.

Generally, the stroke line of the app border is less than one pixel, so I directly traced the 1px border as usual. Although it is 1px, the result is completely different from the stroke in the app, "thick" , so I searched online to see if there was a solution, but I couldn’t find it after searching for a while. What should I do? If the demand side doesn’t want to be so rough, then I have to solve it by myself.
So I used the previous method to think of linear-gradient

.line li{  border: none;   
  background-image: -webkit-linear-gradient(#222 50%,transparent 50%);   
  background-image: -moz-linear-gradient(#222 50%,transparent 50%);   
  background-image: -o-linear-gradient(#222 50%,transparent 50%);   
  background-image: linear-gradient(#222 50%,transparent 50%);   
  background-size:  100% 1px;   
  background-repeat: no-repeat;   
  background-position: bottombottom;}
<ul class="line">
    <li>linear-gradient</li>
    <li>linear-gradient</li>
    <li>linear-gradient</li>
</ul>

OK, it’s out again, but it’s still a little flawed. Then the problem comes, which is to change the stroke. The position (left, top, right, bottom) needs to modify the parameters

For example, the left stroke needs to be changed:

background-image: -webkit-linear-gradient(left ,transparent 50%,#222 50%);   
background-size:  1px 100%;   
background-position: left;

The specific ones are not listed one by one.


Use linear to create complex border effects
In addition, I saw a method on the Internet to use the linear-gradient attribute to create a gorgeous border effect. First, the code is given. You can check the effect on your computer:

<!DOCTYPE html>     

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">     
<head>     
    <meta charset="utf-8" />     
    <title></title>     
    <style>     
        .box {     
            margin: 80px 30px;     
            width: 200px;     
            height: 200px;     
            position: relative;     
            background: #fff;     
            float: left;     
        }     

            .box:before {     
                content: &#39;&#39;;     
                z-index: -1;     
                position: absolute;     
                width: 220px;     
                height: 220px;     
                top: -10px;     
                left: -10px;     
            }     

        .first:before {     
            background-image: linear-gradient(90deg, yellow, gold);     
        }     

        .second:before {     
            background-image: linear-gradient(0deg, orange, red);     
        }     

        .third:before {     
            background-image: repeating-linear-gradient(-45deg,#cc2a2d,#cc2a2d 30px,#f2f2f2 30px,#f2f2f2 40px,#0e71bb 40px,#0e71bb 70px,#f2f2f2 70px,#f2f2f2 80px);     
        }     
    </style>     
</head>     
<body>     
    <p class="box first"></p>     
    <p class="box second"></p>     
    <p class="box third"></p>     
</body>     
</html>


As you can see from the code, we actually do not use borders. So how is this border effect achieved?
The general idea is that we first define a white p, and then define a colored p that is a circle larger than a white square. Overlap the two and let the white p cover the colored p to achieve the effect of a border.
There are many css knowledge points used here.
1. :before pseudo-class
From the above code, we can see that we actually define a :before pseudo-class in the defined white p, and put all the styles of the colored squares here. This is because using the :before definition can make positioning more convenient. Just adjust top and left to the width of the border. At the same time, the two become a whole.
2. Linear-gradient
Many browsers now support this css method. This method has the following three usage modes:
①background:linear-gradient(top,#fff,#000)
This code means that it starts from white at the top and transitions to black at the bottom.
②background:linear-gradient(top,right,#fff,#000)
This code passes two parameters about the position, top and right, which means starting from the upper right and changing to the lower left. Other reasons Same as the first one.
③background:linear-gradient(30deg,#fff,#000)
The first parameter of this code passes the angle. In fact, the principle and position are the same, but it does not change from the standard position. . So what is the corresponding relationship between angle and position? According to experiments, 0 degrees corresponds to bottom, 90 degrees corresponds to left, 180 degrees corresponds to top, and 360 degrees corresponds to right.
The above is the code and explanation for using the linear method to achieve gorgeous borders. You can implement it locally to discover more novel combination implementation methods.

The above is the detailed content of Sample code sharing for making borders using CSS3's linear gradient. 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
What does margin: 40px 100px 120px 80px signify?What does margin: 40px 100px 120px 80px signify?Apr 28, 2025 pm 05:31 PM

Article discusses CSS margin property, specifically "margin: 40px 100px 120px 80px", its application, and effects on webpage layout.

What are the different CSS border properties?What are the different CSS border properties?Apr 28, 2025 pm 05:30 PM

The article discusses CSS border properties, focusing on customization, best practices, and responsiveness. Main argument: border-radius is most effective for responsive designs.

What are CSS backgrounds, list the properties?What are CSS backgrounds, list the properties?Apr 28, 2025 pm 05:29 PM

The article discusses CSS background properties, their uses in enhancing website design, and common mistakes to avoid. Key focus is on responsive design using background-size.

What are CSS HSL Colors?What are CSS HSL Colors?Apr 28, 2025 pm 05:28 PM

Article discusses CSS HSL colors, their use in web design, and advantages over RGB. Main focus is on enhancing design and accessibility through intuitive color manipulation.

How can we add comments in CSS?How can we add comments in CSS?Apr 28, 2025 pm 05:27 PM

The article discusses the use of comments in CSS, detailing single-line and multi-line comment syntaxes. It argues that comments enhance code readability, maintainability, and collaboration, but may impact website performance if not managed properly.

What are CSS Selectors?What are CSS Selectors?Apr 28, 2025 pm 05:26 PM

The article discusses CSS Selectors, their types, and usage for styling HTML elements. It compares ID and class selectors and addresses performance issues with complex selectors.

Which type of CSS holds the highest priority?Which type of CSS holds the highest priority?Apr 28, 2025 pm 05:25 PM

The article discusses CSS priority, focusing on inline styles having the highest specificity. It explains specificity levels, overriding methods, and debugging tools for managing CSS conflicts.

In how many ways can we add CSS to our HTML file?In how many ways can we add CSS to our HTML file?Apr 28, 2025 pm 05:24 PM

Article discusses three methods to add CSS to HTML: inline, internal, and external. Each method's impact on website performance and suitability for beginners is analyzed.(159 characters)

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software