CSS 笔记 - 第10天
CSS 基础知识笔记 - 第10天
平面转换-平移
通过 CSS transform 属性和 translate() 函数实现元素的平面平移。
transform: translate(x, y);
:元素在水平方向平移 x,垂直方向平移 y。transform: translateX(x);
:元素在水平方向平移 x。transform: translateY(y);
:元素在垂直方向平移 y。
单位可以是 px, %, em 等。
样例:
1 | <div class="box translate-example"></div> |
平面转换-旋转
通过 CSS transform 属性和 rotate() 函数实现元素的平面旋转。
transform: rotate(angle);
:元素顺时针旋转指定的角度。角度单位可以是 deg (度), grad (梯度), rad (弧度), turn (圈)。
默认旋转中心是元素的中心点 (50% 50%)。可以通过transform-origin
属性修改旋转中心。- 旋转会改变坐标轴指向
样例:
1 | <div class="box rotate-example"></div> |
平面转换-组合
可以将多个转换函数组合在一起使用,用空格分隔。
transform: translate(50px, 100px) rotate(45deg);
:元素先平移,然后旋转。
注意:转换的顺序会影响最终结果。
样例:
1 | <div class="box combined-example"></div> |
平面转换-缩放
通过 CSS transform 属性和 scale() 函数实现元素的平面缩放。
transform: scale(x, y);
:元素在水平方向缩放 x 倍,垂直方向缩放 y 倍。如果只提供一个值,则水平和垂直方向等比例缩放。transform: scaleX(x);
:元素在水平方向缩放 x 倍。transform: scaleY(y);
:元素在垂直方向缩放 y 倍。
缩放因子大于 1 表示放大,小于 1 表示缩小。默认缩放中心是元素的中心点 (50% 50%)。可以通过transform-origin
属性修改缩放中心。
样例:
1 | <div class="box scale-example"></div> |
颜色渐变
线性渐变
通过 linear-gradient()
函数创建线性渐变背景。
background-image: linear-gradient(direction, color-stop1, color-stop2, ...);
direction
:渐变的方向。可以是角度 (e.g.,90deg
),也可以是关键词 (e.g.,to right
,to bottom right
)。默认为to bottom
。color-stop
:颜色断点,可以指定颜色和位置 (e.g.,red
,blue 50%
)。
样例:
1 | <div class="gradient-box linear-gradient-example"></div> |
1 | <div class="gradient-box linear-gradient-example-angle"></div> |
径向渐变
通过 radial-gradient()
函数创建径向渐变背景。
background-image: radial-gradient(shape size at position, start-color, ..., last-color);
shape
:渐变的形状,可以是circle
(圆形) 或ellipse
(椭圆形,默认)。size
:渐变的大小。可以是关键词 (e.g.,farthest-corner
(默认),closest-side
,closest-corner
,farthest-side
) 或具体的长度/百分比。position
:渐变的中心位置,类似于background-position
。默认为center
。start-color, ..., last-color
:颜色断点。
样例:
1 | <div class="gradient-box radial-gradient-example"></div> |
1 | <div class="gradient-box radial-gradient-example-shape-size-pos"></div> |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 iehtian!