加入收藏 | 设为首页 | 会员中心 | 我要投稿 衡阳站长网 (https://www.0734zz.cn/)- 数据集成、设备管理、备份、数据加密、智能搜索!
当前位置: 首页 > 服务器 > 系统 > 正文

利用CSS的Sass预处理器(框架)来制作居中效果

发布时间:2020-03-19 12:05:42 所属栏目:系统 来源:站长网
导读:副标题#e# 虽然使用 CSS 创建居中效果需要耍一些花招,特别是垂直居中效果,但我认为由此生出的诋毁,对于 CSS 则是不公平的。实际上我们有太多的方式使用 CSS 创建居中效果了,而且作为一名前端开发者,你真的有必要对其中的原理了解一二。 写这篇文章的目

在这里让我们暂停一下,深入分析后续逻辑的层次:
width height solution
null   null   translate  
defined   defined   margin  
defined   null   margin-left + translateY  
null   defined   margin-right + translateX  

秀代码:

CSS Code复制内容到剪贴板

@mixin center($width: null, $height: null) {   

    position: absolute;   

    top: 50%;   

    left: 50%;   

  

    @if not $width and not $height {   

        // Go with `translate`   

    } @else if $width and $height {   

        // Go width `margin`   

    } @else if not $height {   

        // Go with `margin-left` and `translateY`   

    } @else {   

        // Go with `margin-top` and `translateX`   

    }   

}  

通过上面的代码,我们已经搭好了 mixin 的骨架,只需要再添加上具体的逻辑代码即可:

CSS Code复制内容到剪贴板

@mixin center($width: null, $height: null) {   

    position: absolute;   

    top: 50%;   

    left: 50%;   

  

    @if not $width and not $height {   

        transform: translate(-50%, -50%);   

    } @else if $width and $height {   

        width: $width;   

        height: $height;   

(编辑:衡阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读