Change the background color of a button with CSS

To change the background color of a button, use the background-color property in CSS. This property allows you to set any color value to customize the button's appearance.

Syntax

button {
    background-color: value;
}

Possible Values

Value Description
Color name Predefined color names like red, blue, yellow
Hex code Hexadecimal values like #FF0000, #00FF00
RGB RGB values like rgb(255, 0, 0)
HSL HSL values like hsl(120, 100%, 50%)

Example: Yellow Background Button

The following example demonstrates how to change a button's background color to yellow −

<!DOCTYPE html>
<html>
<head>
<style>
    .btn {
        background-color: yellow;
        color: black;
        padding: 10px 20px;
        border: none;
        border-radius: 5px;
        font-size: 16px;
        cursor: pointer;
    }
</style>
</head>
<body>
    <h2>Button Background Color</h2>
    <button class="btn">Click Me</button>
</body>
</html>
A yellow button with black text and rounded corners appears on the page. The button has padding and shows a pointer cursor on hover.

Example: Multiple Color Options

You can create buttons with different background colors using various CSS color formats −

<!DOCTYPE html>
<html>
<head>
<style>
    .btn {
        padding: 10px 20px;
        border: none;
        border-radius: 5px;
        color: white;
        font-size: 16px;
        margin: 5px;
        cursor: pointer;
    }
    
    .btn-red { background-color: #FF4444; }
    .btn-green { background-color: rgb(34, 139, 34); }
    .btn-blue { background-color: hsl(240, 100%, 50%); }
</style>
</head>
<body>
    <h2>Different Background Colors</h2>
    <button class="btn btn-red">Red Button</button>
    <button class="btn btn-green">Green Button</button>
    <button class="btn btn-blue">Blue Button</button>
</body>
</html>
Three buttons appear with different background colors: red (using hex), green (using RGB), and blue (using HSL). All buttons have white text and rounded corners.

Conclusion

The background-color property is the primary way to change a button's background color in CSS. You can use color names, hex codes, RGB, or HSL values to achieve the desired appearance for your buttons.

Updated on: 2026-03-15T12:54:25+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements