MST
星途 面试题库

面试题:CSS 中过渡属性的基本使用

请描述 CSS 过渡(transition)属性的基本语法和常用属性值,并举例说明如何实现一个简单的元素过渡效果,比如当鼠标悬停在一个按钮上时,按钮的背景颜色和边框半径发生过渡变化。
37.5万 热度难度
前端开发CSS

知识考点

AI 面试

面试题答案

一键面试

CSS过渡(transition)属性基本语法

transition: property duration timing-function delay;

常用属性值

  1. property:指定应用过渡效果的 CSS 属性名称,如 widthheightbackground - color 等,可使用 all 表示所有属性。
  2. duration:过渡效果持续的时间,单位为秒(s)或毫秒(ms)。
  3. timing - function:规定过渡效果的速度曲线,常见值有 ease(默认,慢 - 快 - 慢)、linear(匀速)、ease - in(慢开始)、ease - out(慢结束)、ease - in - out(慢开始和慢结束)。
  4. delay:过渡效果开始前的延迟时间,单位为秒(s)或毫秒(ms)。

示例

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        button {
            padding: 10px 20px;
            background-color: blue;
            color: white;
            border: none;
            border - radius: 5px;
            transition: background-color 0.5s ease - in - out, border - radius 0.5s ease - in - out;
        }
        button:hover {
            background-color: red;
            border - radius: 15px;
        }
    </style>
</head>
<body>
    <button>悬停我</button>
</body>
</html>

上述代码中,当鼠标悬停在按钮上时,按钮的背景颜色和边框半径会在 0.5 秒内以 ease - in - out 的速度曲线进行过渡变化。