border-radius
的用法)长宽固定的元素,能够经过指定宽高的一半,变为椭圆形,格式为两个值用/
分开。css
width: 100px; height: 80px; border-radius: 50px/40px; background: orange;
效果以下:css3
宽高不固定的元素,还能够经过指定百分比值来实现自适应椭圆的效果。wordpress
border-radius: 50%/50%;
还能够简写为函数
border-radius: 50%;
### 为每一个角指定不一样的值字体
border-radius的展开属性为:动画
也能够直接简写:border-radius: [top-left-horizon] [top-right-horizon] [bottom-right-horizon] [bottom-left-horizon] / [top-left-vertical] [top-right-vertical] [bottom-right-vertical] [bottom-left-vertical]
spa
固然,各个值也能够省略,1~4个都可,会以CSS常规方法重复。code
利用这个特性,就能够实现半个椭圆或四分之一个椭圆的效果。orm
border-radius:50%/ 100% 100% 0 0;
border-radius:100% 0 0 100%/ 50%;
border-radius:100% 0 0;
形变,用transform: skewX(-45deg)
,能够获得平行四边形,可是,元素中的字体也会跟着形变,就像这样:图片
添加一个标签,使用嵌套的方案,并非很好的方法。
能够选择伪元素的方式:
.button{ position: relative; } .button::before{ content: ''; position: absolute; top: 0; bottom: 0; left: 0; right: 0; background: orange; transform: skewX(-45deg); z-index: -1; }
这种方法,能够应用在想变形一个元素而不想变形它的内容的场景。
把skew
改为rotate
,就变成了这样:
实现边框内圆角
.inner-radius{ position: relative; border-radius: 20px; background: white; } .inner-radius::before{ content: ''; position: absolute; top: -10px; bottom: -10px; left: -10px; right: -10px; background: orange; z-index: -1; }
多重边框
.borders{ position: relative; border: 2px dashed orange; } .borders::before{ content: ''; position: absolute; top: -10px; bottom: -10px; left: -10px; right: -10px; border: 2px dashed green; z-index: -1; }
还有IE8下实现多重背景、为某一层“背景”单独设置相似opacity这样的属性等等。
<div class="picture"> <img src="./image/girl.jpg" alt="girl"> </div> .picture{ width:300px; transform: rotate(45deg); overflow: hidden; } .picture>img{ transform: rotate(-45deg) scale(1.42); }
可是这种方法须要一个多余的div,而且对正方形的图片比较适用。下面的方法很是适应非正方形的图片。
/*直接在img元素上应用该属性,将图片裁切成想要的样子,用多边形函数polygon()指定一个菱形*/ clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
还能够将clip-path参与到过渡动画。(动画须要在同一种形状函数,而且点相同)
.clip-img{ clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%); transition: 1s clip-path; } .clip-img:hover { clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%); }
这样就有了鼠标悬停时,图片扩展为原来的形状和大小。
直切角
background: linear-gradient(45deg, transparent 15px, orange 0) left bottom, linear-gradient(-45deg, transparent 15px, orange 0) right bottom, linear-gradient(135deg, transparent 15px, orange 0) left top, linear-gradient(-135deg, transparent 15px, orange 0) right top; background-size: 50% 50%; background-repeat: no-repeat;
注意,这个角度让我一度很困惑,它是这样的:
参考: 张鑫旭-深刻理解CSS3 gradient斜向线性渐变
弧形切角
把线性渐变改成径向渐变,就变成了内凹圆角。
background: radial-gradient(circle at left bottom, transparent 15px, orange 0) left bottom, radial-gradient(circle at right bottom, transparent 15px, orange 0) right bottom, radial-gradient(circle at left top, transparent 15px, orange 0) left top, radial-gradient(circle at right top, transparent 15px, orange 0) right top; background-size: 50% 50%; background-repeat: no-repeat;
裁切路径方案
background: orange; clip-path: polygon(20px 0, calc(100%-20px) 0, 100% 20px, 100% calc(100% -20px), calc(100% - 20px) 100%, 20px 100%,0 calc(100% - 20px), 0 20px); 这种方案,当内边距不够宽时,会裁切掉文本。由于它是对整个元素进行裁切。
使用3D形变,以x轴旋转,造成梯形的效果。为了不标签中的字体也变形,还使用“平行四边形”实现方式(伪元素)。
.tab{ position: relative; text-align: center; line-height: 30px; } .tab::before{ position: absolute; content: ""; top: 0; left: 0; bottom: 0; right: 0; transform-origin: bottom; /*以元素底边旋转,底边和原元素底部等长。(默认以中轴线旋转,元素大小容易不受控制)*/ transform: scaleY(1.3) perspective(.5em) rotateX(5deg); /*旋转后,比原元素矮了,在Y轴方向上拉伸至原来的长度。(尽可能不改变原元素,保证回退后的效果)*/ background-color: orange; z-index: -1; }
这种方式很容易添加其余样式,如,在伪元素中加入如下样式:
background-image: linear-gradient(hsla(0,0%,100%,.6), hsla(0,0%,100%,0)); border: 1px solid rgba(0,0,0,.4); border-bottom: none; border-radius: .5em .5em 0 0; box-shadow: 0 .15em white inset;
就变成了这个样子: