CSS相关

584 阅读3分钟

画一条0.5px的线

采用meta viewport的方式

这个也是淘宝触屏采用的方式,常用的移动html viewport的设置如下

<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

具体意思就不多提,它就是让页面的高宽度即为设备的高宽像素,而为了方便绘制0.5像素的viewport的设置如下

<meta name="viewport" content="initial-scale=0.5, maximum-scale=0.5, minimum-scale=0.5, user-scalable=no" />

这样html的宽高就是设备的2倍,此时依然使用css board为1像素的话,肉眼看到页面线条就相当于transform:scale(0.5)的效果,即为0.5像素

但是这种方式涉及到页面整体布局规划以及图片大小的制作,所以若采用这个方式还是事先确定为好

采用 border-image的方式

这个其实就比较简单了,直接制作一个0.5像素的线条和其搭配使用的背景色的图片即可

复制代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>boardTest</title>
    <style>
            p{
                margin: 50px auto;
                padding: 5px 10px 5px 10px;
                color: red;
                text-align: center;
                width: 60px;
            }
            
            p:first-child{
                border-bottom: 1px solid red;
            }
            p:last-child{
                border-width: 0 0 1px 0; border-image: url("img/line_h.gif") 2 0 round;
            }
        
    </style>
</head>
<body>
    <div>
        <p>点击1</p>
        <p>点击2</p>
    </div>
</body>
</html>

采用background-image的方式

我这里采用的是渐变色linear-gradient的方式,代码如下

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>boardTest</title>
    <style>
            p{
                margin: 50px auto;
                padding: 5px 10px 5px 10px;
                color: red;
                text-align: center;
                width: 60px;
            }
            
            p:first-child{
                border-bottom: 1px solid red;
            }
            p:last-child{
                background-image: -webkit-linear-gradient(bottom,red 50%,transparent 50%);
            background-image: linear-gradient(bottom,red 50%,transparent 50%);
            background-size:  100% 1px;
            background-repeat: no-repeat;
            background-position: bottom right;
            }
        
    </style>
</head>
<body>
    <div>
        <p>点击1</p>
        <p>点击2</p>
    </div>
</body>
</html>

复制代码

linear-gradient(bottom,red 50%,transparent 50%);的意思是从底部绘制一个渐变色,颜色为红色,占比为50%,而总宽度已经设置为100%而总高度为一个像素background-size: 100% 1px;

这样显示出来就是0.5像素的线条

采用transform: scale()的方式

就是将绘制出来的线条的高度进行半倍的缩放,代码如下

复制代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>boardTest</title>
    <style>
            p{
                margin: 50px auto;
                padding: 5px 10px 5px 10px;
                color: red;
                text-align: center;
                width: 60px;
            }
            p:first-child{
                border-bottom: 1px solid red;
            }
            p:last-child{
                position: relative;
            }
        p:last-child:after {
            position: absolute;
            content: '';
            width: 100%;
            left: 0;
            bottom: 0;
            height: 1px;
            background-color: red;
            -webkit-transform: scale(1,0.5);
            transform: scale(1,0.5);
            -webkit-transform-origin: center bottom;
            transform-origin: center bottom
        }
        
    </style>
</head>
<body>
    <div>
        <p>点击1</p>
        <p>点击2</p>
    </div>
</body>
</html>

transition和animation的区别

Animation和transition大部分属性是相同的,他们都是随时间改变元素的属性值,他们的主要区别是transition需要触发一个事件才能改变属性,而animation不需要触发任何事件的情况下才会随时间改变属性值,并且transition为2帧,从from .... to,而animation可以一帧一帧的。

垂直居中的方法

(1)margin:auto法

div{
width: 400px;
height: 400px;
position: relative;
border: 1px solid #465468;
}
img{
position: absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
}

定位为上下左右为0,margin:0可以实现脱离文档流的居中.

(2)margin负值法

.container{
    width: 500px;
    height: 400px;
    border: 2px solid #379;
    position: relative;
}
.inner{
    width: 480px;
    height: 380px;
    background-color: #746;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -190px; /*height的一半*/
    margin-left: -240px; /*width的一半*/
}

补充:其实这里也可以将marin-top和margin-left负值替换成,transform:translateX(-50%)和transform:translateY(-50%)

(3)table-cell(未脱离文档流的)

设置父元素的display:table-cell,并且vertical-align:middle,这样子元素可以实现垂直居中。

css:
div{
width: 300px;
height: 300px;
border: 3px solid #555;
display: table-cell;
vertical-align: middle;
text-align: center;
}
img{
vertical-align: middle;
}

(4)利用flex

将父元素设置为display:flex,并且设置align-items:center;justify-content:center;

.container{
width: 300px;
height: 200px;
border: 3px solid #546461;
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}
.inner{
border: 3px solid #458761;
padding: 20px;
}

其他

元素的文本省略号

单行元素的文本省略号

overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;

多行元素的文本省略号

display: -webkit-box
-webkit-box-orient:vertical
-webkit-line-clamp:3
overflow:hidden

用css实现一个硬币旋转的效果

css代码

#euro {
    width: 150px;
    height: 150px;
    margin-left: -75px;
    margin-top: -75px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform-style: preserve-3d;
    animation: spin 2.5s linear infinite;
}

.back {
    border-radius: 50%;
    width: 150px;
    height: 150px;
    background: red;
}

.middle {
    border-radius: 50%;
    width: 150px;
    height: 150px;
    background: green;
    transform: translateZ(1px);
    position: absolute;
    top: 0;
}

.front {
    border-radius: 50%;
    height: 150px;
    width: 150px;
    background: purple;
    position: absolute;
    top: 0;
    transform: translateZ(10px);
}

@keyframes spin {
    0% {
        transform: rotateY(0deg);
    }

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

html代码

<div id="euro">
        <div class="back"></div>
        <div class="middle"></div>
        <div class="front"></div>
    </div>