效果演示
實現了一個彩色按鈕特效,包括一個按鈕(button)和一個前景色(::before)。按鈕具有四種不同的顏色,當鼠標懸停在按鈕上時,前景色會出現漸變效果,并且按鈕的顏色、文本陰影和邊框陰影會發生變化。整個按鈕具有立體感,使其看起來更加美觀。
Code
HTML
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>彩色浮雕按鈕</title><link rel="stylesheet" href="./25-彩色浮雕按鈕.css">
</head><body><button>求點贊</button><button>求關注</button><button>求收藏</button><button>求轉發</button>
</body></html>
CSS
* {margin: 0;padding: 0;
}body {height: 100vh;display: flex;justify-content: center;align-items: center;flex-direction: column;background-color: #e8e8e8;
}button {margin: 10px;width: 280px;height: 90px;font-size: 35px;font-weight: bold;background: transparent;border: 1px solid transparent;cursor: pointer; position: relative;box-shadow: inset 1px 1px 2px #000,inset -1px -1px 2px #808080;color: #333;text-shadow: 1px 1px 0 #808080;overflow: hidden;transition: 0.3s linear 0.15s;
}button:nth-child(1) {--c: #ff4757;
}button:nth-child(2) {--c: #ffa502;
}button:nth-child(3) {--c: #2ed573;
}button:nth-child(4) {--c: #1e90ff;
}button::before {content: "";position: absolute;width: 100px;height: 8px;top: 0;left: 50%;transform: translateX(-50%);border-radius: 0 0 50% 50%;filter: blur(5px);transition: 0.3s;
}button:hover::before {background: var(--c);box-shadow: 0 0 10px var(--c),0 0 20px var(--c),0 0 30px var(--c),0 0 40px var(--c),0 0 50px var(--c);
}button:hover {color: #fff;text-shadow: 0 0 10px var(--c),0 5px 5px #000;box-shadow: inset 1px 1px 2px #000,inset -1px -1px 2px var(--c);
}
實現思路拆分
好的,下面是每行代碼的詳細講解及注釋:
* {margin: 0;padding: 0;
}
這段代碼是設置所有元素的外邊距和內邊距為0。
body {height: 100vh;display: flex;justify-content: center;align-items: center;flex-direction: column;background-color: #333;
}
這段代碼是設置body元素的高度為100vh,使用flex布局,使其水平和垂直居中。同時設置flex-direction為column,使其內部元素垂直排列。并且設置背景顏色為#333。
button {margin: 10px;width: 280px;height: 90px;font-size: 35px;font-weight: bold;background: transparent;border: 1px solid transparent;position: relative;box-shadow: inset 1px 1px 2px #000,inset -1px -1px 2px #808080;color: #333;text-shadow: 1px 1px 0 #808080;overflow: hidden;transition: 0.3s linear 0.15s;
}
這段代碼是設置按鈕的樣式。包括外邊距、寬度、高度、字體大小、字體粗細、背景、邊框、相對定位、內部陰影效果、字體顏色和文本陰影效果。并且設置過渡效果。
button:nth-child(1) {--c: #ff4757;
}button:nth-child(2) {--c: #ffa502;
}button:nth-child(3) {--c: #2ed573;
}button:nth-child(4) {--c: #1e90ff;
}
這段代碼是為每個按鈕設置不同的顏色。使用CSS變量(–c)來存儲顏色值。
button::before {content: "";position: absolute;width: 100px;height: 8px;top: 0;left: 50%;transform: translateX(-50%);border-radius: 0 0 50% 50%;filter: blur(5px);transition: 0.3s;
}
這段代碼是設置前景色的樣式。使用偽元素::before,設置寬度、高度、相對定位、頂部和左側偏移、圓角和模糊效果。并且設置過渡效果。
button:hover::before {background: var(--c);box-shadow: 0 0 10px var(--c),0 0 20px var(--c),0 0 30px var(--c),0 0 40px var(--c),0 0 50px var(--c);
}
這段代碼是設置當鼠標懸停在按鈕上時,前景色的漸變效果。使用:hover偽類,設置背景顏色和陰影效果。
button:hover {color: #fff;text-shadow: 0 0 10px var(--c),0 5px 5px #000;box-shadow: inset 1px 1px 2px #000,inset -1px -1px 2px var(--c);
}
這段代碼是設置當鼠標懸停在按鈕上時,按鈕的顏色、文本陰影和邊框陰影的變化。使用:hover偽類,設置字體顏色、文本陰影效果和內部陰影效果。