美文网首页
Border 圆角边框

Border 圆角边框

作者: 夹板儿孩 | 来源:发表于2025-07-24 18:04 被阅读0次

border 圆角渐变边框,实现方式


<div class="border"><h1>hello</h1></div>
        .border {
            --border-width: 5px;
            --rounded: 50px;

            width: 700px;
            height: 320px;
            position: relative;
            background: #fff7eb;
            border-radius: var(--rounded);
        }
        .border::before {
            content: '';
            background: linear-gradient(135deg, red, blue);
            position: absolute;
            top: calc(0px - var(--border-width));
            right: calc(0px - var(--border-width));
            bottom: calc(0px - var(--border-width));
            left: calc(0px - var(--border-width));
            border-radius: calc(var(--rounded) + var(--border-width));
            z-index: -1;
        }

border 圆角渐变虚线边框,实现方式1
适应性比实现方式2更强,在边框足够细的时候,左上角和右下角的这种残缺效果会被弱化。
不适合做粗边框


<div class="border2"><h1>hello</h1></div>
.border2 {
            --border-width: 5px;
            --rounded: 50px;
            --border-dot-line: calc(2px + var(--border-width));

            width: 700px;
            height: 320px;
            position: relative;
            background: #fff7eb;
            border-radius: var(--rounded);
        }
        .border2::before {
            content: '';
            background: linear-gradient(135deg, red, blue);
            position: absolute;
            top: calc(0px - var(--border-width));
            right: calc(0px - var(--border-width));
            bottom: calc(0px - var(--border-width));
            left: calc(0px - var(--border-width));
            border-radius: calc(var(--rounded) + var(--border-width));
            z-index: -1;
            -webkit-mask: repeating-linear-gradient(
                    -45deg,
                    black 0,
                    black calc(var(--border-dot-line)),
                    transparent calc(var(--border-dot-line)),
                    transparent calc(var(--border-dot-line) * 2 + 1px)
            );
        }

border 圆角渐变虚线边框,实现方式2
使用 SVG 实现,比方式1要更好
但是比方式1控制起来要复杂一些。当页面动态变化的时候,可能需要重新计算一些东西


    <div class="border3">
        <svg class="svg-content" viewBox="0 0 100% 100%" preserveAspectRatio="none">
            <!-- 边框渐变颜色 -->
            <defs>
                <linearGradient id="strokeGradient" x1="0%" y1="0%" x2="100%" y2="100%">
                    <stop offset="0%" stop-color="red" />
                    <stop offset="100%" stop-color="blue" />
                </linearGradient>
            </defs>
            <!-- 边框线条样式 -->
            <!-- stroke-dasharray 虚线长度。前一个数组是有颜色的长度,后一个数字是空白长度 -->
            <!-- x, y 需要设置为线条宽度的一半 -->
            <!-- stroke-width 线条宽度 -->
            <rect
                class="svg-rect"
                stroke-dasharray="4,4"
                stroke-opacity="1"
                x="2.5"
                y="2.5"
                rx="50px" ry="50px"
                style="fill: transparent; stroke-width: var(--border-width);  stroke: url(#strokeGradient);"
            />
        </svg>
        <div class="border3-wrapper">
            <h1>hello</h1>
        </div>
    </div>
        .border3 {
            --border-width: 5px;
            --rounded: 50px;

            width: 700px;
            height: 320px;
            position: relative;
            overflow: hidden;


        }
        .border3-wrapper {
            content: '';
            position: absolute;
            top: 50%;
            left: 50%;
            right: 0;
            bottom: 0;
            transform: translate(-50%, -50%);
            width: calc(100% - var(--border-width) * 2);
            height: calc(100% - var(--border-width) * 2);
            border-radius: calc(var(--rounded) - var(--border-width));
            background: #fff7eb;
        }
        .svg-content {
            z-index: -1;
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }
        .svg-rect {
            width: calc(100% - var(--border-width));
            height: calc(100% - var(--border-width));
        }

相关文章

  • Flutter-Border

    边框(Border) 单侧边框 全部边框 圆角(borderRadius) 全部圆角 单侧圆角 阴影(BoxSha...

  • CSS3

    边框 border-radius - 圆角边框 box-shadow - 边框阴影 border-image - ...

  • 25.边框圆角渐变

    边框 什么是边框圆角?将直角的边框变为圆角的边框 边框圆角的格式?border-radius: 左上 右上 右下 ...

  • 前端培训机构—css源码笔记(三)

    一、边框拓展 设置边框圆角 border-radius 边框折叠 border-collapse: collaps...

  • CSS边框圆角--跟着李南江学编程

    1.什么是边框圆角? 就是把矩形边框变成圆角边框,就叫做边框圆角。 2.设置边框圆角的格式 2.1 border-...

  • CSS3简明教程之边框

    第2章 边框 圆角效果 border-radius border-radius是向样式添加圆角边框,使用方法: 不...

  • Tailwind Border

    边框半径(border radius) border-redius属性用于为元素设置圆角边框,可使用border-...

  • CSS3

    1、边框(border) div{ border-radius; /*圆角*/ box-shadow;...

  • 常用CSS3 一目了然

    1.css3边框 圆角边框 border-radius: 5px;(圆角半径) 边框阴影 box-shadow: ...

  • 边框圆角

    边框:border圆角:border-radius让四个角都成圆角 示例:HTML代码 style样式代码 圆角:...

网友评论

      本文标题:Border 圆角边框

      本文链接:https://www.haomeiwen.com/subject/dkcgnjtx.html