常用的css小技巧

  1. 清除浮动
    文档元素浮动情况下,会出现各种问题,这时候需要清除浮动:
.clearfix:after {
    content: ".";
    display: block;
    clear: both;
    visibility: hidden;
    line-height: 0;
    height: 0;
}
.clearfix { display: inline-block; }
html[xmlns] .clearfix { display: block; }
* html .clearfix { height: 1%; }
  1. 文本省略显示...
    文本超过一定宽度时,需要显示省略号。
    单行省略(需要设置外层宽度):
.text-overflow {
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
}

多行省略(css多行省略在chrome生效需要设置高度、宽度):

.text-overflow-more {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
}
  1. 透明效果,兼容不同浏览器
.transparent {
    filter: alpha(opacity=50); /* internet explorer */
    -khtml-opacity: 0.5; /* khtml, old safari */
    -moz-opacity: 0.5; /* mozilla, netscape */
    opacity: 0.5; /* fx, safari, opera */
}
  1. 制作三角形,三角形效果如下:


    三角形
.triangle {
    border-color: transparent transparent green transparent;
    border-style: solid;
    border-width: 0px 300px 300px 300px;
    height: 0px;
    width: 0px;
}
  1. 禁止换行, 文字只在一行内显示不换行
.nowrap {white-space:nowrap;}
  1. 文字渐变效果
.text-gradient{
    background-image: linear-gradient(135deg, deeppink, deepskyblue);
    -webkit-background-clip: text;
    color: transparent;
}
  1. 背景渐变
.gradient{
    background: #e6e6e6; //当浏览器不支持背景渐变时该语句将会被执行
    background: -o-linear-gradient(top, #fdfdfd, #e6e6e6);
    background: -moz-linear-gradient(top, #fdfdfd, #e6e6e6);
    background: -webkit-linear-gradient(top, #fdfdfd, #e6e6e6); //最新发布语法
    background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#fdfdfd), #e6e6e6); //老式语法
}
  1. 下划线动效,鼠标移动到文字时出现下划线效果,效果如下:


    下划线动效
.hover-underline-animation {
    display: inline-block;
    position: relative;
    color: #0087ca;
}

.hover-underline-animation::after {
    content: '';
    position: absolute;
    width: 100%;
    transform: scaleX(0);
    height: 2px;
    bottom: 0;
    left: 0;
    background-color: #0087ca;
    transform-origin: bottom right;
    transition: transform 0.25s ease-out;
}

.hover-underline-animation:hover::after {
    transform: scaleX(1);
    transform-origin: bottom left;
}
  1. 甜甜圈loading效果, 效果如下:


    甜甜圈loading
@keyframes donut-spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

.donut {
    display: inline-block;
    border: 4px solid rgba(0, 0, 0, 0.1);
    border-left-color: #7983ff;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    animation: donut-spin 1.2s linear infinite;
}
  1. 弹跳的loading,效果如下:


    弹跳的loading
<div class="bouncing-loader">
    <div></div>
    <div></div>
    <div></div>
</div>

@keyframes bouncing-loader {
    from {
        opacity: 1;
        transform: translateY(0);
    }

    to {
        opacity: 0.1;
        transform: translateY(-1rem);
    }
}

.bouncing-loader {
    display: flex;
    justify-content: center;
}

.bouncing-loader div {
    width: 1rem;
    height: 1rem;
    margin: 3rem 0.2rem;
    background: #8385aa;
    border-radius: 50%;
    animation: bouncing-loader 0.6s infinite alternate;
}

.bouncing-loader div:nth-child(2) {
    animation-delay: 0.2s;
}

.bouncing-loader div:nth-child(3) {
    animation-delay: 0.4s;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容