Tips for implementing multiple borders in CSS

Tips for implementing multiple borders in CSS

1. Multiple borders[1]

Background: box-shadow, outline
In view of the diversity of usage scenarios, there are more and more designs with multiple borders. In the past, we could use the border-image property to deal with it, but this is not very flexible at the CSS code level. Now we use the box-shadow and outline properties to implement multiple borders respectively.

1. Box-shadow solution

Idea: Use the size of the fourth parameter (expansion radius) of box-shadow, multiple projection code example:

  <div class="border-multiple">
        Multiple border implementation solution 1: box-shadow
    </div>

    .border-multiple {
        margin: 50px auto;
        padding: 20px;
        width: 600px; 
        background-color: #fff; 
        box-shadow: 0 0 0 10px #f0f,
                    0 0 0 15px #00f,
                    0 2px 15px 15px rgba( 0, 0, 0, .8);
    } 

insert image description here

Multiple borders--box-shadow

summary:

1. Shadows do not affect layout and are not affected by box-sizing
2. Support comma-separated syntax, you can create any number of projections
3. Disadvantages: Only solid borders can be realized, and other styles of borders cannot be realized

2. Outline solution

Code example:

  <div class="border-outline">
        Multiple border implementation solution 2: outline
    </div>
    .border-outline {
        margin: 200px auto;
        padding: 20px;
        width: 600px;
        background-color: #ff0;
        outline: 3px dashed #0f0;
        outline-offset: -10px;
    } 

insert image description here

Multiple borders – outline

summary:

1. The premise is to achieve two layers of borders
2. The outline-offset attribute value may be required
3. The default stroke of the outline is a rectangle. When there is a rounded corner, it will be considered a bug and cannot fit the rounded corner.
4. Comma syntax is not supported

2. Rounded corners of borders[2]

Background: box-shadow, outline
To solve the bug in Example 3 above, you can use the box-shadow expansion radius to fill the gap between the rounded corners and the outline.
Code example:

  <div class="inner-rounding">
         Need to use box-shadow, outline, "multiple borders" to achieve Note: The extension radius of box-shadow should be 0.5 times the radius of the corner</div> 
    .inner-rounding {
        background-color: #ccc;
        margin: 50px auto;
        padding: 20px;
        width: 600px; 
        padding: 20px;
        border-radius: 20px;
        box-shadow: 0 0 0 10px #f00;
        outline: 10px solid #f00;
    }

Note: The extension radius of box-shadow should be 0.5 times the radius of the corners; strictly speaking, it should be (√2 - 1) times, and 0.5 is taken here for better calculation

insert image description here

Border inner corners

3. Translucent border[3]

Background knowledge: rgba or hsla color attributes, background-clip
Idea: Let the border appear in the padding-box of the clipped background. Code example:

   <div class="border-opacity">
         Implementation of semi-transparent border</div>
    .border-opacity {
        margin: 50px auto;
        padding: 20px;
        width: 600px;
        border: 10px solid hsla(0, 0%, 100%, 0.5);
        background-color: #fff;
        background-clip: padding-box;
    }

summary:

The implementation of the semi-transparent border requires the use of the background-clip property of CSS3
background-clip has three properties:
1. border-box: The background is clipped to the border box (the border is not visible)
2. padding-box: The background is clipped to the padding box (the border can be seen)
3. content-box: the background is clipped to the content box (the border is close to the content)

insert image description here

Translucent border effect diagram

4. Continuous Image Borders[4]

Background knowledge: CSS gradient, basic border-iamge, background-clip
Let's first look at how border-image implements the image border:
The implementation principle of border-image is the nine-grid expansion method: cut the image into nine pieces and then apply them to the corresponding edges and corners of the element border.
Code example:

   <p class="border-image">The implementation principle of border-image is the nine-grid expansion method: cut the image into nine pieces, and then apply them to the corresponding edges and corners of the element border. </p>
        <p class="border-image">We don't want a specific part of the image to be fixed at the corner; instead, we want the image area that appears at the corner to change as the width, height, and border thickness of the element change. </p>
        .border-image {
            border: 40px solid transparent;
            border-image: 33.334% url("http://csssecrets.io/images/stone-art.jpg");
            padding: 1em;
            max-width: 20em;
            font: 130%/1.6 Baskerville, Palatino, serif;
        }
        .border-image:nth-child(2) {
            margin-top: 1em;
            border-image-repeat: round;
        } 

insert image description here

Border-image effect diagram

Disadvantages: We don’t want a specific part of the image to be fixed at the corner; instead, we want the image area that appears at the corner to change with the width, height, and border thickness of the element.
How to solve it?
Implementation ideas:
1. Use CSS gradient and background extensions
2. On the background image, superimpose a layer of pure white solid color background
3. In order to make the background of the lower layer show through the border area, you need to specify different background-clip values ​​for the two background layers.
4. Set the background color at the bottom of the multiple backgrounds. Use a CSS gradient from white to white to simulate the effect of a pure white solid background.

Code example:

 <p class="contituous-images">Implementation ideas:
            1. Use CSS gradients and background extensions 2. Overlay a pure white solid color background on the background image 3. In order to allow the lower background to show through the border area, you need to specify different background-clip values ​​for the two backgrounds 4. Set the background color at the bottom of the multiple backgrounds. You need to use a CSS gradient from white to white to simulate the effect of a pure white solid color background</p>
        .contituous-images {
            padding: 1em;
            border: 1em solid transparent;
            /* background: linear-gradient(white, white),
                            url(http://csssecrets.io/images/stone-art.jpg);
            background-size: cover;
            background-clip: padding-box, border-box;
            background-origin: border-box; */

            /* background can also be abbreviated as follows*/
            background: linear-gradient(white, white) padding-box,
            url(http://csssecrets.io/images/stone-art.jpg) border-box 0 / cover;

            width: 21em;
            padding: 1em;
            overflow: hidden;
            /* Border can be dragged*/
            resize: both;
            font: 100%/1.6 Baskerville, Palatino, serif;
        } 

insert image description here
Continuous image border

You can also use gradient patterns to achieve envelope-style border code examples

You can also use gradient patterns to create envelope-style borders

<p class="envelope-border">You can also use gradient patterns to create envelope-style borders</p>
.envelope-border {
    padding: 1em;
    border: 0.5em solid transparent;
    background: linear-gradient(white, white) padding-box, repeating-linear-gradient(-45deg, red 0, red 12.5%, transparent 0, transparent 25%, #58a 0, #58a 37.5%, transparent 0, transparent 50%) 0 / 3em 3em;
    max-width: 20em;
    font: 100%/1.6 Baskerville, Palatino, serif;} 

insert image description here

Envelope border effect diagram

This is the end of this article about tips for implementing various borders in CSS. For more relevant CSS border content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  How to modify the user and group of a file in Linux

>>:  Solution to the problem that elements with negative z-index cannot be clicked

Recommend

How to make a div height adaptive to the browser height

This old question has troubled countless front-end...

How to implement page screenshot function in JS

"Page screenshot" is a requirement ofte...

Detailed explanation of the usage of position attribute in HTML (four types)

The four property values ​​of position are: 1.rel...

How to use sed command to efficiently delete specific lines of a file

Preface Normally, if we want to delete certain li...

Vue implements left and right sliding effect example code

Preface The effect problems used in personal actu...

HTML table markup tutorial (4): border color attribute BORDERCOLOR

To beautify the table, you can set different bord...

URL representation in HTML web pages

In HTML, common URLs are represented in a variety ...

How to set the default value of a MySQL field

Table of contents Preface: 1. Default value relat...

Use Visual Studio Code to connect to the MySql database and query

Visual Studio Code is a powerful text editor prod...

Vue3.0 routing automatic import method example

1. Prerequisites We use the require.context metho...

Detailed explanation of the use of React.cloneElement

Table of contents The role of cloneElement Usage ...