Skip to main content

CSS Button Hover Effects

Beyond hover effects, this page also looks at button shadows, disabled states and width.


CSS Hoverable Buttons

Moving the mouse over a button can restyle it through the :hover pseudo-class.

Tip: How fast that change happens is up to the transition-duration property.

Example

Each button here fades smoothly into its new color on hover.

Prompt
Create a page with five buttons (Green, Blue, Red, Grey, Black) that change background color on hover. Use the CSS transition-duration property set to 0.4s for a smooth hover effect.
Output: complete HTML page

CSS Buttons With Shadow

Shadows on buttons come from the box-shadow property.

Example

Of these two buttons, one always has its shadow, while the other gains it only on hover.

Prompt
Create a page with two buttons: one with a permanent box-shadow, and one that only shows a box-shadow on hover. Use rgba(0,0,0,0.6) for the shadow color.
Output: complete HTML page

CSS Disabled Button

Lowering opacity makes a button look partly transparent, and that appearance reads as "disabled".

Tip: Hovering over the button shows a "no parking sign" if you also set cursor to not-allowed.

Example

Here opacity and cursor work together to make a button look disabled.

Prompt
Create a page with a normal button and a disabled-looking button. The disabled button should use opacity 0.6 and cursor not-allowed to indicate it cannot be clicked.
Output: complete HTML page

CSS Button Width

Buttons normally take their size from their text. To choose a specific width instead, set the width property.

Tip: A percentage, such as 50% of the parent element, keeps the width responsive, while pixels hold it fixed.

Example

Three button widths to compare: 250px, 50% and 100%.

Prompt
Create a page with three buttons stacked vertically, each with a different width: 250px, 50%, and 100%. Show how the CSS width property controls button sizing.
Output: complete HTML page

Button on Image

Example

CSS positioning places this button over the top of an image.

Prompt
Create a page with a container holding an image and a centered button overlaid on top of it. Use CSS positioning to place the button in the middle of the image.
Output: complete HTML page