如何实现类似目标网站‘start voting’按钮的CSS悬停过渡效果?
Got it! Since you already have a grasp on CSS transitions, let's focus on building out that specific polished hover effect for your "start voting" button. Here's a step-by-step breakdown with code examples:
1. First, Analyze the Core Hover Effects
Before writing code, take a quick look at the target button (use your browser's dev tools to inspect it if you can!) to note key details like:
- Does the background fade to a darker/lighter shade?
- Does the text color shift?
- Is there a subtle scale or shadow change for depth?
- Does the border color transition alongside other properties?
Most large call-to-action buttons mix a few of these to feel interactive and polished. Let's use a common high-quality version as our example.
2. Basic HTML Structure
Start with a simple button element (swap for an anchor tag if it's meant to link somewhere):
<button class="start-voting-btn">Start Voting</button>
3. Base Button Styles (Make It "Large")
First, style the button to match the size and base look of the target:
.start-voting-btn { /* Large button sizing */ padding: 16px 48px; font-size: 20px; font-weight: 700; /* Base visual styles */ background-color: #2563eb; /* Adjust to match the target's base color */ color: #ffffff; border: none; border-radius: 8px; /* Critical: Enable smooth transitions */ transition: background-color 0.3s ease, transform 0.3s ease, box-shadow 0.3s ease; cursor: pointer; }
Note: Listing specific properties instead of using all is better for performance, but feel free to use transition: all 0.3s ease if you want a quicker setup.
4. Hover State Styles
Add the hover effects to match the target button. Let's include a color shift, subtle scale, and soft shadow for depth:
.start-voting-btn:hover { /* Darken the background */ background-color: #1d4ed8; /* Subtle scale to make it feel "responsive" */ transform: scale(1.05); /* Add a soft shadow for depth */ box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3); }
If the target button uses a border transition instead of a shadow, swap in something like this:
.start-voting-btn { border: 2px solid #2563eb; background-color: transparent; color: #2563eb; } .start-voting-btn:hover { background-color: #2563eb; color: #ffffff; }
5. Optional: Active State for "Click" Feedback
To make the button feel even more tactile, add an active state for when it's clicked:
.start-voting-btn:active { transform: scale(0.98); box-shadow: 0 2px 6px rgba(37, 99, 235, 0.2); }
Pro Tip: Match the Exact Effect
If you're still not getting the exact transition, right-click the target button in your browser and select "Inspect". In the Styles tab, toggle the :hover state to see exactly which properties are transitioning, their values, and the timing. That's the fastest way to replicate it perfectly!
内容的提问来源于stack exchange,提问作者ripjuce

