CSS double border (2 colors) without using outline?

前端 未结 7 956
小鲜肉
小鲜肉 2020-12-03 14:07

I was wondering what you guys think is the easiest way to get a double border with 2 colors around a div? I tried using border and outline together and it worked in Firefox

相关标签:
7条回答
  • 2020-12-03 15:06

    Late to the party for this question, but I feel like I should record this somewhere. You can make a scalable function in something like Less or Stylus which will create any number of borders (Stylus here):

    abs(n)
        if n < 0
            (-1*n)
        else
            n
    
    horizBorder(n, backgroundColor)
        $shadow = 0 0 0 0 transparent
        $sign = (n/abs(n))
        for $i in ($sign..n)
            /* offset-x | offset-y | blur-radius | spread-radius | color */
            $shadow = $shadow, 0 (2*$i - $sign)px 0 0 #000, 0 (2*$i)px 0 0 backgroundColor
        return $shadow
    

    Then,

    $background: #FFF // my background was white in this case and I wanted alternating black/white borders
    
    .border-bottom
        box-shadow: horizBorder(5, $background)
    .border-top
        box-shadow: horizBorder(-5, $background)
    .border-top-and-bottom
        box-shadow: horizBorder(5, $background), horizBorder(-5, $background)
    
    0 讨论(0)
提交回复
热议问题