Is it possible to make a squiggly line?

前端 未结 12 1990
无人及你
无人及你 2021-02-01 19:04

If I wanted to make a horizontal line, I would do this:




        
12条回答
  •  没有蜡笔的小新
    2021-02-01 19:33

    Thank @yeerk for such a wonderful solution!

    But I would like to suggest some improvements to his first variants — to those of waves what seem to be more triangular. I would suggest to use :before and :after pseudo-elements instead of hard-coded enclosed divs.

    It may look like this (html):

    — where triangly-line is a target decorated element (not "waved" but "triangled").

    Corresponding styles (using LESS notation) will look like this:

    @line-width: 300px;
    @triangled-size: 6px;
    @triangly-color: red;
    @double-triangled-size: (@triangled-size * 2 - 1px);
    .linear-gradient (@gradient) {
        background: -webkit-linear-gradient(@gradient);
        background: -o-linear-gradient(@gradient);
        background: linear-gradient(@gradient);
    }
    .triangly-gradient (@sign, @color) {
        .linear-gradient(~"@{sign}45deg, transparent, transparent 49%, @{color} 49%, transparent 51%");
    }
    .triangly-line {
        overflow: hidden;
        position: relative;
        height: @triangled-size;
        &:before {
            .triangly-gradient("", @triangly-color);
        }
        &:after {
            .triangly-gradient("-", @triangly-color);
        }
        &:before,
        &:after {
            content: "";
            display: block;
            position: absolute;
            width: @line-width;
            height: @triangled-size;
            background-size: @double-triangled-size @double-triangled-size !important;
        }
    }
    

    Resulted CSS (using specified above parameters):

    .triangly-line {
        overflow: hidden;
        position: relative;
        height: 6px;
    }
    .triangly-line:before {
        background: -webkit-linear-gradient(45deg, transparent, transparent 49%, red 49%, transparent 51%);
        background: -o-linear-gradient(45deg, transparent, transparent 49%, red 49%, transparent 51%);
        background: linear-gradient(45deg, transparent, transparent 49%, red 49%, transparent 51%);
    }
    .triangly-line:after {
        background: -webkit-linear-gradient(-45deg, transparent, transparent 49%, red 49%, transparent 51%);
        background: -o-linear-gradient(-45deg, transparent, transparent 49%, red 49%, transparent 51%);
        background: linear-gradient(-45deg, transparent, transparent 49%, red 49%, transparent 51%);
    }
    .triangly-line:before,
    .triangly-line:after {
        content: "";
        display: block;
        position: absolute;
        width: 300px;
        height: 6px;
        background-size: 11px 11px !important;
    }
    

提交回复
热议问题