Skip to content

Instantly share code, notes, and snippets.

@kingluddite
Created September 21, 2024 12:44
Show Gist options
  • Save kingluddite/ccdfcb55821cd95e84467912a17f2246 to your computer and use it in GitHub Desktop.
Save kingluddite/ccdfcb55821cd95e84467912a17f2246 to your computer and use it in GitHub Desktop.
custom css on Bootstrap

To override Bootstrap's default button color with custom styles, you can use an external stylesheet or inline styles. Let me show you an example of how to override the button's default styling using a custom CSS class.

Step 1: Create a Custom CSS Class

You’ll need to add your custom styles either in the <style> section of the HTML document or in a separate CSS file. For this example, we’ll override the .btn-primary button color.

Example Before Overriding Bootstrap Button Color:

Here’s the standard Bootstrap button without any custom styles:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Before Custom Button Styles</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container">
        <button class="btn btn-primary">Default Bootstrap Button</button>
    </div>
</body>
</html>

This will render a blue button because it’s using Bootstrap’s default .btn-primary styles.

Step 2: After Adding Custom Styles to Override Button Color

Now, let's override the default Bootstrap button color with our custom style.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>After Custom Button Styles</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- Custom Styles -->
    <style>
        .btn-primary {
            background-color: #ff5733; /* Custom orange color */
            border-color: #ff5733;     /* Matching border color */
        }

        .btn-primary:hover {
            background-color: #e74c3c; /* Darker shade on hover */
            border-color: #e74c3c;
        }
    </style>
</head>
<body>
    <div class="container">
        <button class="btn btn-primary">Custom Bootstrap Button</button>
    </div>
</body>
</html>

Explanation:

  • In the <style> section, we have overridden the .btn-primary class with a custom background color (#ff5733, an orange shade) and border color.
  • We also defined a hover effect by changing the button's background color to a slightly darker shade (#e74c3c) when the mouse hovers over it.

This method allows you to customize Bootstrap’s default components without needing to modify Bootstrap’s source files directly, keeping your project maintainable and scalable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment