CSS知识点速记

219 阅读1分钟

滑动展开/收起

<style>
    .element { 
        max-height: 20px; 
        width: 100px;
        overflow: hidden; 
        transition: max-height 2s; 
    } 
    .element:hover { 
        max-height: 666px;
    }
</style>
<div class="element">
    <div>鼠标放上去会展开,使用max-height自适应高度。</div>
    <div>max-height需要设置一个最大值,但是不能太大,否则收起动画效果延时明显</div>
</div>

网页logo图片显示

<style>
h1 {
    content: url(logo.png);
}
</style>
<h1>LOGO</h1>

loading...三点动画效果

<style>
       dot {
            display: inline-block; 
            height: 1em;
            line-height: 1;
            text-align: left;
            vertical-align: bottom;
            overflow: hidden;
        }
        dot::after {
            display: block;
            content: '...\A..\A.';
            white-space: pre-wrap;
            animation: dot 3s infinite step-start both;
        }
        @keyframes dot {
            33% { transform: translateY(-2em); }
            66% { transform: translateY(-1em); }
        }
    </style>
    
    <span>正在加载中<dot /></span>

两段文字中间管道符|

image.png

<style>
a + a:before { 
 content: ""; 
 font-size: 0; 
 padding: 10px 3px 1px; 
 margin-left: 6px;
 border-left: 1px solid gray; 
}
</style>
<a href="">登录</a><a href="">注册</a>

元素居中

// 块级子元素宽高自适应填充
.father {
    width: 300px; height:150px;
    position: relative;
}
.son { 
    position: absolute; 
    top: 0; right: 0; bottom: 0; left: 0; 
    width: 200px; height: 100px; 
    margin: auto; 
 }

绘制三角形

div { 
    width: 0; 
    border: 10px solid; 
    border-color: #f30 transparent transparent; 
}

弹框居中

<style>
.container { 
     position: fixed; 
     top: 0; right: 0; bottom: 0; left: 0; 
     background-color: rgba(0,0,0,.5); 
     text-align: center; 
     font-size: 0; 
     white-space: nowrap; 
     overflow: auto; 
} 
.container:after { 
     content: ''; 
     display: inline-block; 
     height: 100%; 
     vertical-align: middle; 
} 
.dialog { 
     display: inline-block; 
     vertical-align: middle; 
     text-align: left; 
     font-size: 14px; 
     white-space: normal; 
     background-color: white;
}
</style>
<div class="container"> 
    <div class="dialog">vertical-align居中</div> 
</div>

文字超出省略

.ellipsis { 
    text-overflow: ellipsis; 
    white-space: nowrap; 
    overflow: hidden; 
}
.ellipsis-2 { 
    text-overflow: ellipsis; 
    overflow: hidden; 
    display: -webkit-box; 
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
}

placeholder-shown实现Matrial Design风格输入框placeholder

image.png image.png

<div class="wrapper">
    <input class="input" placeholder="s" />
    <label class="input-label">邮箱</label>
</div>

<style>
    .wrapper {
        position: relative;
        margin-top: 20px;
    }
    .input-label {
        position: absolute;
        left: 10px;
        pointer-events: none;
        transition-duration: .5s;
    }
    .input:placeholder-shown::placeholder {
        color: transparent;
    }
    .input:focus + .input-label,
    .input:not(:placeholder-shown) + .input-label {
        transform: scale(0.6) translate(0, -20px);
        background-color: white;
    }
</style>