Up to this point, you've learned how to create a basic Bootstrap navbar and incorporate additional components like dropdown menus and forms. You've already seen some options for customizing your navbar, and now it's time to delve deeper into making your navbar stand out with unique colors and styles. These customizations will help ensure that your navbar design aligns with the overall theme of your website.
In this lesson, you will learn how to customize the colors and styles of your Bootstrap navbar:
-
Changing Navbar Colors: You will learn how to change the background and text colors of your navbar. A common way to achieve this is by using Bootstrap's predefined classes, or you can create your own styles using CSS.
Bootstrap provides a range of predefined classes for background and text colors. Here are some examples:
HTML, XML1<!-- Dark background with light text --> 2<nav class="navbar navbar-expand-lg navbar-dark bg-dark"> 3 <!-- Navbar content goes here --> 4</nav> 5 6<!-- Light background with dark text --> 7<nav class="navbar navbar-expand-lg navbar-light bg-light"> 8 <!-- Navbar content goes here --> 9</nav> 10 11<!-- Primary background with white text --> 12<nav class="navbar navbar-expand-lg navbar-dark bg-primary"> 13 <!-- Navbar content goes here --> 14</nav> 15 16<!-- Secondary background with white text --> 17<nav class="navbar navbar-expand-lg navbar-dark bg-secondary"> 18 <!-- Navbar content goes here --> 19</nav> 20 21<!-- Success background with white text --> 22<nav class="navbar navbar-expand-lg navbar-dark bg-success"> 23 <!-- Navbar content goes here --> 24</nav>
In the above code:
navbar-dark
: Ensures that the text color adapts well to dark backgrounds.navbar-light
: Ensures that the text color adapts well to light backgrounds.bg-dark
,bg-light
,bg-primary
,bg-secondary
,bg-success
: Set different background colors.navbar-expand-lg
: Makes the navbar responsive, expanding it for larger screens and collapsing it for smaller ones.
-
Customizing Styles with CSS: Additionally, you'll explore how to apply custom styles using your own CSS rules to have greater control over the appearance of your navbar.
Here's an example:
HTML, XML1<!-- Custom CSS styles defined in a <style> tag or an external stylesheet --> 2<style> 3 .custom-navbar { 4 background-color: #ff5733; /* Sets custom background color */ 5 color: white; /* Sets text color */ 6 border-bottom: 2px solid #000; /* Adds a bottom border */ 7 padding: 10px; /* Adds padding around the navbar */ 8 font-size: 18px; /* Sets font size */ 9 font-weight: bold; /* Sets font weight */ 10 box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* Adds a subtle shadow */ 11 transition: background-color 0.3s ease; /* Smooth transition effect */ 12 } 13 14 .custom-navbar:hover { 15 background-color: #ff4512; /* Changes background color on hover */ 16 } 17</style> 18 19<!-- Navbar element with custom styles --> 20<nav class="navbar navbar-expand-lg custom-navbar"> 21 <!-- Navbar content goes here --> 22</nav>
In the above code:
.custom-navbar
is a CSS class that sets a custom background color, text color, border, padding, font size, font weight, box shadow, and transition effect.- You define styles like
background-color
,color
,border-bottom
,padding
,font-size
,font-weight
,box-shadow
, andtransition
within a<style>
tag or an external stylesheet. - The
hover
pseudo-class is used to change the background color when the user hovers over the navbar. - Apply the custom class by setting
class="custom-navbar"
on your<nav>
element. navbar-expand-lg
: Ensures the navbar is responsive, expanding it for larger screens and collapsing it for smaller ones.
Customizing the colors and styles of your navbar is crucial for maintaining brand consistency and enhancing the visual appeal of your website. A well-designed navbar can significantly improve the user experience by making navigation intuitive and visually pleasing.
Consider this scenario: You’re developing a website for a high-end fashion brand. A basic, unstyled navbar wouldn’t convey the luxury and attention to detail that the brand represents. By customizing the navbar colors and styles, you can create a cohesive look that reflects the brand’s identity and offers a more engaging user experience.
Colors and styles do more than just improve aesthetics — they can also influence how users perceive and interact with your website. Customizing these elements ensures that your navbar is both functional and aligned with your company's branding.
Ready to make your navbar look amazing? Let’s dive into the practice section and start customizing!