CSS Button Style – Hover, Color, and Background (2024)

In this article you'll see how to style a button using CSS.

My goal here is mostly to showcase how different CSS rules and styles are applied and used. We won't see much design inspiration nor will we discuss ideas for styling.

Instead, this will be more of an overview of how the styles themselves work, what properties are commonly used, and how they can be combined.

You'll first see how to create a button in HTML. Then you'll learn how to override the default styles of buttons. Lastly, you'll get a glimpse of how to style buttons for their three different states.

Here's an Interactive Scrim of CSS Button Style

  1. Create a button in HTML
  2. Change default styling of buttons
    1. Change the background color
    2. Change text color
    3. Change the border style
    4. Change the size
  3. Style button states
    1. Style hover state
    2. Style focus state
    3. Style active state
  4. Conclusion

Let's get started!

How to Create a Button in HTML

To create a button, use the <button> element.

This is a more accessible and semantic option compared to using a generic container which is created with the <div> element.

In the index.html file below, I've created the basic structure for a webpage and added a single button:

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="style.css"> <title>CSS Button Style</title></head><body> <button type="button" class="button">Click me!</button></body></html>

Let's break down the line <button type="button" class="button">Click me!</button>:

  • You first add the button element, which consists of an opening <button> and closing </button> tag.
  • The type="button" attribute in the opening <button> tag explicitly creates a clickable button. Since this particular button is not used for submitting a form, it is useful for semantic reasons to add it in order to make the code clearer and not trigger any unwanted actions.
  • The class="button" attribute will be used to style the button in a separate CSS file. The value button could be any other name you choose. For example you could have used class="btn".
  • The text Click me! is the visible text inside the button.

Any styles that will be applied to the button will go inside a spearate style.css file.

You can apply the styles to the HTML content by linking the two files together. You do this with the <link rel="stylesheet" href="style.css"> tag which was used in index.html.

In the style.css file, I've added some styling which only centers the button in the middle of the browser window.

Notice that the class="button" is used with the .button selector. This is a way to apply styles directly to the button.

* { box-sizing: border-box;} body { display:flex; justify-content: center; align-items: center; margin:50px auto;}.button { position: absolute; top:50%}

The code from above will result in the following:

CSS Button Style – Hover, Color, and Background (1)

The default styling of buttons will vary depending on the browser you're using.

This is an example of how the native styles for buttons look on the Google Chrome browser.

How to Change the Default Styling of Buttons

How to Change the Background Color of Buttons

To change the background color of the button, use the CSS background-color property and give it a value of a color of your taste.

In the .button selector, you use background-color:#0a0a23; to change the background color of the button.

.button { position: absolute; top:50%; background-color:#0a0a23;}

CSS Button Style – Hover, Color, and Background (2)

How to Change the Text Color of Buttons

The default color of text is black, so when you add a dark background color you will notice that the text has disappeared.

Another thing to make sure of is that there is enough contrast between the button's background color and text color. This helps make the text more readable and easy on the eyes.

Next, use the color property to change the color of text:

.button { position: absolute; top:50%; background-color:#0a0a23; color: #fff;}

CSS Button Style – Hover, Color, and Background (3)

How to Change the Border Style of Buttons

Notice the grey around the edges of the button? That is the default color of the button's borders.

One way to fix this is to use the border-color property. You set the value to be the same as the value of background-color. This makes sure the borders have the same color as the background of the button.

Another way would be to remove the border around the button entirely by using border:none;.

.button { position: absolute; top:50%; background-color:#0a0a23; color: #fff; border:none;}

CSS Button Style – Hover, Color, and Background (4)

Next, you can also round-up the edges of the button by using the border-radius property, like so:

.button { position: absolute; top:50%; background-color:#0a0a23; color: #fff; border:none; border-radius:10px; }

CSS Button Style – Hover, Color, and Background (5)

You could also add a slight dark shadow effect around the button by using the box-shadow property:

 position: absolute; top:50%; background-color:#0a0a23; color: #fff; border:none; border-radius:10px; box-shadow: 0px 0px 2px 2px rgb(0,0,0);

CSS Button Style – Hover, Color, and Background (6)

How to Change the Size of Buttons

The way to create more space inside the button's borders is to increase the padding of the button.

Below I added a value of 15px for the top, bottom, right, and left padding of the button.

I also set a minimum height and width, with the min-height and min-width properties respectively. Buttons need to be large enough for all different kind of devices.

.button { position: absolute; top:50%; background-color:#0a0a23; color: #fff; border:none; border-radius:10px; padding:15px; min-height:30px; min-width: 120px; }

CSS Button Style – Hover, Color, and Background (7)

How to Style Button States

Buttons have three different states:

  • :hover
  • :focus
  • :active

It's best that the three states are styled differently and don't share the same styles.

In the following sections I'll give a brief explanation on what each one of the states mean and what triggers them. You'll also see some ways you can style the button for each separate state.

Here's an interactive scrim about styling button states:

How to Style :hover States

The :hover state becomes present when a user hovers over a button, by bringing their mouse or trackpad over it, without selecting it or clicking on it.

To change the button's styles when you hover over it, use the :hover CSS
pseudoclass selector.

A common change to make with :hover is switching the background-color of the button.

To make the change less sudden, pair :hover with the transition property.

The transition property will help make the transition from no state to a :hover state much smoother.

The change of background color will happen a bit slower than it would without the transition property. This will also help make the end result less jarring for the user.

.button:hover { background-color:#002ead; transition: 0.7s; }

In the example above, I used a Hex color code value to make the background color a lighter shade for when I hover over the button.

With the help of the transition property I also caused a delay of 0.7s when the transition from no state to a :hover state happens. This caused a slower transition from the original #0a0a23 background color to the #002ead background color.

CSS Button Style – Hover, Color, and Background (8)

Keep in mind that the :hover pseudoclass does not work for mobile device screens and mobile apps. Choose to use hover effects only for desktop web applications and not touch screens.

How to Style :focus States

The :focus state takes effect for keyboard users - specifically it will activate when you focus on a button by hitting the Tab key ().

If you're following along, when you focus on the button after pressing the Tab key, you'll see the following:

CSS Button Style – Hover, Color, and Background (9)

Notice the slight light blue outline around the button when it's gained focus?

Browsers have default styling for the :focus pseudoclass, for accessibility keyboard navigation purposes. It's not a good idea to remove that outline altogether.

You can however create custom styles for it and make it easily detectable.

A way to do so is by setting the outline color to first be transparent.

Following that, you can maintain the outline-style to solid. Lastly, using the box-shadow property, you can add a color of your liking for when the element is focused on:

 .button:focus { outline-color: transparent; outline-style:solid; box-shadow: 0 0 0 4px #5a01a7;}

CSS Button Style – Hover, Color, and Background (10)

You can also again pair these styles with the transition property, depending on the effect you want to achieve:

 .button:focus { outline-color: transparent; outline-style:solid; box-shadow: 0 0 0 4px #5a01a7; transition: 0.7s;}

CSS Button Style – Hover, Color, and Background (11)

How to Style for the :active State

The :active state gets activated when you click on the button by either clicking the computer's mouse or pressing down on the laptop's trackpad.

That being said, look at what happens when I click the button after I've applied and kept the styles for the :hover and :focus states:

CSS Button Style – Hover, Color, and Background (12)

The :hover state styles are applied before clicking when I hover over the button.

The :focus state styles are applied also, because when a button is clicked it also gains a :focus state alongside an :active one.

However, keep in mind that they are not the same thing.

:focus state is when an element is being focused on and :active is when a user clicks on an element by holding and pressing down on it.

To change the style for when a user clicks a button, apply styles to the :active CSS pseudoselector.

In this case, I've changed the background color of the button when a user clicks on it

.button:active { background-color: #ffbf00;}

CSS Button Style – Hover, Color, and Background (13)

Conclusion

And there you have it! You now know the basics of how to style a button with CSS.

We went over how to change the background color and text color of buttons as well as how to style buttons for their different states.

To learn more about web design, check out freeCodeCamp's Responsive Web Design Certification. In the interactive lessons, you'll learn HTML and CSS by building 15 practice projects and 5 certification projects.

Note that the above cert is still in beta - if you want the latest stable version, check here.

Thanks for reading and happy coding!

CSS Button Style – Hover, Color, and Background (2024)

FAQs

How do you change the background color of a button on hover CSS? ›

How to Change the Background Color of Buttons. To change the background color of the button, use the CSS background-color property and give it a value of a color of your taste. In the .button selector, you use background-color:#0a0a23; to change the background color of the button.

How to style change color on hover CSS? ›

Changing link color on hover using CSS

To change the color of your link on hover, use the :hover pseudo property on the link's class and give it a different color.

How do you add a hover effect to a button in CSS? ›

Steps To Implement The Hover Effect
  1. Create your webpage.
  2. Add your big blue action button just below the h1 element.
  3. Style your big button by adding a style sheet. ...
  4. Identify and select the element that will fire the hover effect. ...
  5. Style your button.

How do I change the button style in hover? ›

Hoverable Buttons

Use the :hover selector to change the style of a button when you move the mouse over it.

How to change hover color in inline CSS? ›

In this case, the inline code: "background-color: red;" is the switch colour on hover. Use the colour you need and then this solution works.

How do I change the color of my hover icon? ›

You can do the following:
  1. Export 2 images > one for the black and one for the hover.
  2. Add both images and make the hover image absolute.
  3. Change display settings on hover using interactions.
Feb 25, 2022

How do you write a hover style in CSS? ›

The :hover selector is used to select elements when you mouse over them.
  1. Tip: The :hover selector can be used on all elements, not only on links.
  2. Tip: Use the :link selector to style links to unvisited pages, the :visited selector to style links to visited pages, and the :active selector to style the active link.

How do I change the color of text when hover in HTML? ›

If you want to change the link color when moving the mouse over a link, you only need the A:hover line. hover - The hover option is the color that the text changes to when the mouse is over the link. In this example, the link changes to a blue color when a mouse cursor is hovering over a link.

How do you overlay hover in CSS? ›

HTML-CSS: Image overlay on hover
  1. Use the :before and :after pseudo-elements for the top and bottom bars of the overlay respectively. ...
  2. Use the <figcaption> for the text of the overlay. ...
  3. Use the :hover pseudo-selector to update the opacity and transform of all the elements and display the overlay.
Aug 19, 2022

How do you hover effects in CSS? ›

Use Keyframes to Define the CSS Hover Animation Sequence.

Create a @keyframes rule with a name to use keyframes, and the animation-name property will use that name to link an animation to a keyframe declaration.

How do you keep hover effect in CSS? ›

To apply the hover effect to the selected element, you need to use the :hover pseudo-class in your CSS code. The styles that should be used when the element hovers over are specified by this pseudo-class, which is added to the element's selector.

How do I change the background on hover? ›

To change the color when hovering in CSS, you will use the CSS selector called :hover . The :hover is a CSS pseudo-class that will select the HTML element when the user hovers over with the mouse. The hover selector will work on almost all HTML elements. Try hovering over the button and see what will happen.

How do you change the color of the button on click in CSS? ›

To change the button color on click in CSS, the “:active” pseudo-class can be used. More specifically, it can represent the button element when it gets activated. Using this class, you can set different button colors when the mouse clicks on it.

What is the difference between button active and button hover? ›

Hover: It is the state that occurs by putting your cursor over the button. You cannot see this state using the keyboard. Active: Very simply, it is the state of an element that is active. For example, in our example, it is the state of interacting with the button.

Should button hover color be darker or lighter? ›

Darker seems more natural. If anything, a physical button would appear slightly darker when you touch it because your hand is casting a shadow. Lighter (illuminated) might signal that the button is waking up at the moment it's needed, like a fog light, refrigerator light, or moving sidewalk.

How do I change the background color of my icon button? ›

To change the icon button background color in Flutter, you can wrap your IconButton widget inside the CircleAvatar widget, and inside the CircleAvatar you can provide the background color.

Where do I change the background color and icon color? ›

Turn on Wallpaper Colors

If you want the app icon colors to be balanced against the wallpaper, open Settings > Wallpaper & style > Wallpaper colors and pick one of those color combinations (the combos you see there change depending on the wallpaper you're using).

How to change color of image on hover using CSS? ›

If the product team was kind enough to also provide a white version of the image, you can simply toggle the image's src on hover. This can be done using JavaScript. You can use an onmouseover function that sets the image's src to white. png and then an onmouseleave function that sets the image's src to black.

How to change image after hover in CSS? ›

The best way to swap images on hover is to place both images in the same container, with the "rollover" image transparent by default. When the user hovers over the container, the "rollover" image becomes opaque. It is fast loading, easy to implement and works on all browsers.

What color should my button hover be? ›

When you hover over an item, you want to make it stand out (or “pop”) from the rest of the clutter. To make the button stand out on a white background, use a darker color for the text. To make the button stand out against the black background, use LIGHTEN to increase the contrast.

How do I change the background color of an icon in CSS? ›

You can change the colour by using CSS-styling. The icons are actually text, so setting the "color" property changes the colour.

How to remove hover background color button in CSS? ›

To remove the CSS hover effect from a specific element, you can set the pointer-events property of the element (the hover behavior of which you want to disable) to “none”.

How do you add color to a button in HTML? ›

All style elements in your HTML button tag should be placed within quotation marks. Type background-color: in the quotation marks after "style=". This element is used to change the background color of the button. Type a color name or hexadecimal code after "background-color:".

How do you style another element on hover? ›

To affect other elements when one element is hovered, an element should be inside another element i.e. parent-child or sibling. On placing the mouse cursor on one element, the other's property should change i.e. the hover affect is then visible.

How do you inspect hover style? ›

Right-click the Hover over me! text and select Inspect. In the Styles pane, click :hov. Check the :hover checkbox.

What is link hover color? ›

The link-hover-color style sets the color of the link text when the mouse cursor is over the link.

How do you add inline hover style? ›

There are no methods to use hover in inline CSS. However, to apply the hover effect on an element using its id, check out the given example. In HTML, add a div element having the class name “main-content”. Inside this div, add an “<h2>” element to specify the heading.

What is the default color for a hover link? ›

By default, a link will appear like this (in all browsers): An unvisited link is underlined and blue. A visited link is underlined and purple. An active link is underlined and red.

How do you add text when hovering in HTML? ›

You can add hover text (also known as a tooltip) to a link in HTML using the title attribute. The title attribute specifies extra information about an element, and is displayed as a tooltip when the user hovers over the element.

How do you add a background overlay in CSS? ›

Using the linear-gradient() CSS function

With the linear-gradient() CSS function, we can achieve a background overlay with just one line of code. It is simple to implement and, like the blend mode approach, eliminates the need to use the pseudo-selector.

What are the different types of hover in CSS? ›

Types. In MDB there are 3 types of the hover effects: overlay, zoom and shadow.

Why can't i use hover in CSS? ›

Wrong CSS Format

Also, if you incorrectly write a selector and wish to apply a hover effect on it, you will notice that the styling does not work as expected in any browser. To solve the issue, you need to go over the CSS hover code to establish if you use the right selector.

How to make background transparent on hover CSS? ›

Transparent Hover Effect

The CSS for this is opacity:1; . When the mouse pointer moves away from the image, the image will be transparent again.

How do you show overlay on hover? ›

Displays an image overlay effect on hover. Use the ::before and ::after pseudo-elements for the top and bottom bars of the overlay respectively. Set their opacity , transform and transition to produce the desired effect.

What is the standard button color in CSS? ›

By default, a button has a white background and black text. Using the CSS background-color property, we can change a button's background color.

How do you make something look like a button in CSS? ›

We can style an anchor tag to look like a button using CSS. This is the default HTML styling for an anchor tag. We can add a class to the anchor tag and then use that class selector to style the element. Now we have an anchor tag that looks like a button.

How do I make a button color transparent in CSS? ›

To add transparency to a button, use the CSS opacity property. This creates a disabled look for the button.

What is the difference between hover and mouseover? ›

The hover() function is more high level - it's built to call functions to handle both a mouseenter event and a mouseleave event. It's very convenient for a UI element that has a hover and normal state (e.g. a button.) The mouseover() function specifically binds to the mouseover event.

Is hover and mouseover the same thing? ›

A mouse-over (also known as a mouse hover) is an action that happens when the user stops or "hovers" the on-screen mouse pointer above a specific element on a computer screen. Holding, or "hovering" the mouse in one location then reveals additional information on the screen.

What is the difference between hover and focus button? ›

Hover: by putting your cursor over it. A hovered element is ready to be activated with a mouse or any mouse-emulating technology (such as eye and motion tracking). Focus: a focused element is ready to be activated with a keyboard or any keyboard-emulating technology (such as switch devices).

How do I change the color of the button on hover material UI? ›

To change mui button color on hover in React js, you can use inline css sx prop with :hover It will change mui button color on hover. See a short example of using the sx prop to change the button color on hover.

How to disable button background in CSS? ›

To do this, simply add the keyword "disabled" to the button. Using HTML tags, we can enable or disable the working of a button on a webpage.

How do I turn off hover color? ›

To apply CSS to disable the hover effect, use the CSS “pointer-events” property and set the value of this property as “none”.

How do I change the color of my floating action button? ›

To change background color of Floating Action Button in Kotlin Android, set the backgroundTint attribute (in layout file) or backgroundTintList property (in Kotlin file) of FAB with the required color.

How do I change the background color after clicking the button? ›

Steps
  1. Step 1 − Let's define the style for the div id of the HTML Document which is going to change the color of the background after clicking the button. ...
  2. Step 2 − Under the div section, we defined the form, button and script elements.
  3. Step 3 − For the button element, the changeBackgroundColor() method is defined.
Mar 17, 2023

How do you add overlay color to an image on hover in CSS? ›

Displays an image overlay effect on hover.
  1. Use the ::before and ::after pseudo-elements for the top and bottom bars of the overlay respectively. ...
  2. Use the <figcaption> for the text of the overlay. ...
  3. Use the :hover pseudo-selector to update the opacity and transform of all the elements and display the overlay.
Oct 11, 2021

What is the best way to change image on hover? ›

The best way to swap images on hover is to place both images in the same container, with the "rollover" image transparent by default. When the user hovers over the container, the "rollover" image becomes opaque. It is fast loading, easy to implement and works on all browsers.

How do I add color to material UI button? ›

Material UI has theme colors you can include to add styles to buttons in your component. Below are the default theme colors MUI supports. To add colors to buttons, we use the “color” prop and specify the palette you wish to use.

How do I make a button glow on hover CSS? ›

How to achieve glowing effect:
  1. set dark background-color property,
  2. set text opacity using color property ( rgba() the fourth argument) to 80% ( 0.8 ), so you can disable it on hover effect,
  3. use the box-shadow property with a bright color to make it appear like it is glowing,
  4. use transition property for a better effect.

Should button hover be dark or light? ›

A darker color commonly makes a button feel like it can be pressed while a lighter color on hover may create a kind of feel that suggests the button is being activated. Whatever color effects you choose for your button, it really doesn't matter so long as the user can identify it as one (a button).

References

Top Articles
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 6411

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.