前端常用样式组元SCSS

本文详细介绍了如何在CSS中使用预定义变量和混合函数来管理主题颜色,包括颜色等级的变化,以及如何应用到文本、边框、内边距/外边距、字体、行高、圆角和按钮样式,同时还展示了flex布局的基本用法。
摘要由CSDN通过智能技术生成

初始化元素


* {margin: 0; padding: 0; border: 0;box-sizing: border-box;}

基础色


$primary: #183ee4;
$success: #0cce63;
$danger: #f00c63;
$brder-color: #adacae;

设置主题色


/**
*@method 设置主题色类
*@param $name 颜色分类
*@param $oClor 原色
*@param $start 颜色等级开始
*@param $start 颜色等级结束
**/
@mixin setThemeBgColor($name, $oClor, $start, $end) {
    .#{$name}-color {
        color: $oClor
    }
    .#{$name}-color-active {
        color: darken($oClor, 20%)
    }
    .#{$name}-bg {
        background-color: $oClor
    }
    @for $num from $start through $end {
        $color: lighten($oClor, percentage($num/10));
        .#{$name}-color-#{$num} {
            color: $color
        }
        .#{$name}-bg-#{$num} {
            background-color: $color
        }
    }
}
/**
*@method 设置主题色组元
*@param $name 颜色分类
*@param $oClor 原色
*@param $start 颜色等级开始
*@param $start 颜色等级结束
**/
@mixin setTheme($name, $oClor, $start, $end) {
    --#{$name}:#{$oClor};
    --#{$name}-active: #{darken($oClor, 20%)};
    @for $num from $start through $end {
        $color: lighten($oClor, percentage($num/10));
        --#{$name}-#{$num}: #{$color};
    }
}

/**设置组元至根节点**/
:root {
    @include setTheme(primary, $primary, 1, 4);
    @include setTheme(success, #0cce63, 1, 4);
    @include setTheme(danger, #f00c63, 1, 4);
}
/**调用设置主题类**/
@include setThemeBgColor(primary, $primary, 1, 4);
@include setThemeBgColor(success, #0cce63, 1, 4);
@include setThemeBgColor(danger, #f00c63, 1, 4);

设置文本色


@for $num from 1 through 7 {
    .clor#{$num} {
       color: lighten(#000, percentage($num/10));
    }
}

设置不同颜色等级边框


@for $index from 1 through 3 {
    $num: $index+5;
    $bcolor:1px solid lighten(#000, percentage($num/10))
    .borderL#{$index} {
        border: #{$bcolor};
    }
    .borderL#{$index} {
        border-left: #{$bcolor};
    }
    .borderR#{$index} {
        border-right: #{$bcolor};
    }
    .borderT#{$index} {
        border-top: #{$bcolor};
    }
    .borderB#{$index} {
        border-bottom: #{$bcolor};
    }
}

设置内外边框


@mixin setPdMg($cName, $dName) {
    @for $index from 1 through 10 {
        $num: 5*$index;
        .#{$cName}L#{$num} {
            #{$dName}-left:#{$num}px;
        }
        .#{$cName}R#{$num} {
            #{$dName}-right:#{$num}px;
        }
        .#{$cName}T#{$num} {
            #{$dName}-top:#{$num}px;
        }
        .#{$cName}B#{$num} {
            #{$dName}-bottom:#{$num}px;
        }
        .#{$cName}H#{$num} {
            @extend .#{$cName}L#{$num};
            @extend .#{$cName}R#{$num};
        }
        .#{$cName}V#{$num} {
            @extend .#{$cName}T#{$num};
            @extend .#{$cName}B#{$num};
        }
        .#{$cName}#{$num} {
            @extend .#{$cName}V#{$num};
            @extend .#{$cName}H#{$num};
        }
    }
}
@include setPdMg(mg, margin);
@include setPdMg(pd, margin);

字体、行高、圆角


@mixin setComm($cName, $aName, $start, $end, $stup) {
    @for $num from $start through $end {
        $val: $num+$stup;
        .#{$cName}#{$val} {
            #{$aName}:#{$val}px
        }
    }
}
/**字体*/
@include setComm(fs, font-size, 10, 40, 2);
/**行高*/
@include setComm(lh, line-height, 10, 40, 6);
/**圆角*/
@include setComm(rd, border-radius, 4, 20, 2);

文本粗细


@for $num from 1 through 8 {
    $val: $num*100;
    .fw#{$num} {
        font-weight:#{$val}
    }
}

flex布局


.flex {
    display: flex;
}
.flex-c {
    flex-direction: column;
}
.flex-wrap {
    flex-wrap: wrap;
}
.jcs {
    justify-content: flex-start;
}
.jcc {
    justify-content: center;
}
.jce {
    justify-content: flex-end;
}
.jca {
    justify-content: space-around;
}
.jcb {
    justify-content: space-between;
}

.ais {
    align-items: flex-start;
}
.aic {
    align-items: center;
}
.aie {
    align-items: flex-end;
}

按钮样式:


$lh: 38px;
$dh: 32px;
$sh: 26px;
$mh: 20px;
$lp: 0 10px;
$dp: 0 15px;
$sp: 0 10px;
$mp: 0 6px;
$dr: 4px;
$fs: 14px;
$fss: 12px;
$fms: 10px;
button,
.button{
    height: $dh;
    line-height: $dh;
    padding: $dp;
    border-radius: $dr;
    font-size: $fs;
    background-color: #cdcdcd;
    cursor: pointer;
    &.small {
        height: $sh;
        line-height: $sh;
        padding: $sp;
        font-size: $fss;
    }
    &.mini {
        height: $mh;
        line-height: $mh;
        padding: $sp;
        font-size: $fms;
    }
    &.small.around {
        border-radius: 10px;
    }
    &.mini.around {
        border-radius: 8px;
    }
    &.around {
        border-radius: 15px;
    }
    &.block {
        width: 100%;
        display: block;
    }
    @each $type in primary, success, danger {
        &.#{$type} {
            background-color: var(--#{$type});
            color: #fff;
        }
        &.#{$type}.plan {
            border: 1px solid var(--#{$type});
            background-color: #fff;
            color: var(--#{$type});
        }
    }
    &[disabled] {
        opacity: .6;
        user-select: none;
        cursor: not-allowed;
    }
}

按钮效果:

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值