掌握 CSS3 核心視覺屬性,賦予網頁豐富互動體驗。本文整理四大高頻屬性的語法、瀏覽器前綴與實戰技巧。
.rotate-45 {
transform: rotate(45deg);
-ms-transform: rotate(45deg); /* IE 9 */
-webkit-transform: rotate(45deg); /* Safari/Chrome */
-moz-transform: rotate(45deg); /* Firefox */
-o-transform: rotate(45deg); /* Opera */
}
現代寫法:標準 transform 已全瀏覽器支援(含 IE10+),前綴僅為極舊版本兼容。transform 還支援 scale()、translate()、skew()、matrix() 等組合變換。
.rounded {
border-radius: 8px; /* 四角同半徑 */
border-top-left-radius: 8px; /* 單角控制 */
border-top-right-radius: 8px;
border-bottom-right-radius: 8px;
border-bottom-left-radius: 8px;
}
/* 橢圓角:水平半徑 / 垂直半徑 */
.elliptical {
border-radius: 50% / 25%; /* 橢圓形 */
}
單角屬性適合「標籤頁」「對話框」等非對稱圓角場景。
.box-shadow {
/* h-shadow v-shadow blur spread color inset */
box-shadow: 2px 2px 8px 0 rgba(41, 41, 41, 0.6);
/* 內陰影 */
box-shadow: inset 1px 1px 3px rgba(0,0,0,0.3);
}
.text-shadow {
/* h-shadow v-shadow blur color */
text-shadow: 0 -1px 0 #000; /* 刻字效果 */
text-shadow: 2px 2px 4px rgba(0,0,0,0.5); /* 立體字 */
}
參數順序固定,spread(擴展半徑)與 inset(內陰影)為可選。多重陰影用逗號分隔,構建豐富層次。
.transition-demo {
transition: property duration timing-function delay;
/* 例:背景色 0.5s 緩入緩出 */
transition: background-color 0.5s ease;
/* 多屬性 */
transition: background 0.3s, transform 0.2s ease-out;
}
| 關鍵字 | 行為 |
|---|---|
ease | 慢啟→快→慢停(預設) |
ease-in | 慢啟動 |
ease-out | 慢結束 |
ease-in-out | 慢啟慢停 |
linear | 勻速 |
cubic-bezier(n,n,n,n) | 自定義貝塞爾曲線 |
-ms- 前綴Autoprefixer 自動添加必要前綴,按 browserslist 策略輸出.card {
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.card:hover {
transform: translateY(-8px) scale(1.02);
box-shadow: 0 12px 24px rgba(0,0,0,0.15);
}
四大屬性組合,以極低成本打造專業級交互體驗。