Bootstrap - Headers Demo
Bootstrap Headers
Bootstrap headers are navigation components from the Bootstrap framework. These headers give a consistent and professional look to your website's top navigation. You can customize them to match your website's design.
Creating a Basic Header
A basic Bootstrap header has a <nav>
element with the class .navbar
and a container element inside it. The container holds the header content, such as a logo and navigation links.
Example: Simple Bootstrap header with logo and navigation
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="#">Logo</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</div>
</nav>
-
The
<nav>
element has the class.navbar
to define it as a Bootstrap navbar. It also has the classes.navbar-expand-lg
to make it responsive and.navbar-light
and.bg-light
to set its color scheme. -
Inside the
<nav>
, there's a<div>
with the class.container
to center the header content and add padding. -
The
.navbar-brand
class is used on an<a>
element to create a logo or brand name. Replace "Logo" with your actual logo image or text. -
The
.navbar-toggler
button is used to toggle the navigation menu on small screens. It's associated with the.collapse
element using thedata-bs-target
attribute. -
The
.collapse.navbar-collapse
div contains the navigation menu items. It has anid
that matches thedata-bs-target
attribute of the toggler button. -
The navigation items are placed inside a
<ul>
element with the class.navbar-nav
. Each<li>
item has the class.nav-item
, and the<a>
elements inside them have the class.nav-link
.
This code creates a basic header with a logo on the left and a navigation menu on the right. The navigation menu collapses into a toggle button on small screens.
You can customize the header further by adding your own logo image, changing the color scheme, or modifying the navigation items to fit your website's structure.
Header Variations
Bootstrap provides header variations that you can use to create different types of headers for your website. These variations include fixed headers, sticky headers, headers with dropdown menus, and transparent headers.
Fixed Headers
Fixed headers stay at the top of the page, even when the user scrolls down. This makes the navigation easy to find at all times. To create a fixed header, add the .fixed-top
class to the <nav>
element:
Example: Fixed Header
<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top">
<!-- Header content -->
</nav>
body {
padding-top: 56px; /* Adjust this value based on your header's height */
}
The .fixed-top
class puts the header at the top of the viewport and gives it a fixed position. Keep in mind that fixed headers may overlap page content, so you should add some padding to the <body>
element to prevent this.
Sticky Headers
Sticky headers become fixed when the user scrolls past them. This allows the header to be first positioned within the page flow and then "stick" to the top of the viewport when scrolled past. To create a sticky header, add the .sticky-top
class to the <nav>
element:
Example: Sticky Header
<nav class="navbar navbar-expand-lg navbar-light bg-light sticky-top">
<!-- Header content -->
</nav>
The .sticky-top
class uses CSS's position: sticky
to do this. Note that sticky positioning may not work in some older browsers.
Transparent Headers
Transparent headers overlay page content and have a see-through background. They are often used on landing pages or hero sections to create a more immersive experience. To create a transparent header, remove the background color classes (e.g., .bg-light
) and add the .bg-transparent
class to the <nav>
element:
Example: Transparent Header
<nav class="navbar navbar-expand-lg navbar-dark bg-transparent">
<!-- Header content -->
</nav>
The .navbar-dark
class is used to make sure that the navigation items are visible on a transparent background. You may need to change the text color and other styles to ensure proper visibility and readability.
These are just a few of the header variations you can create with Bootstrap. By combining these variations and customizing the styles, you can create headers that fit your website's design and requirements.
Customizing Headers
You may want to customize Bootstrap headers to match your website's design. You can achieve this by modifying the header's CSS styles.
To customize a Bootstrap header, target the header elements using CSS selectors and apply your desired styles. Here are some common customizations:
- Changing header background color:
- Use Bootstrap's
.bg-*
classes to change the header's background color, such as.bg-primary
,.bg-secondary
,.bg-dark
, etc. - To use a custom color, override the default background color using CSS:
- Use Bootstrap's
Example: Changing header background color using inline CSS
<nav class="navbar navbar-expand-lg navbar-light" style="background-color: #e3f2fd;">
<!-- Header content -->
</nav>
Or, create a custom CSS class:
Example: Changing header background color using a custom CSS class
.custom-header {
background-color: #e3f2fd;
}
And apply it to the <nav>
element:
<nav class="navbar navbar-expand-lg navbar-light custom-header">
<!-- Header content -->
</nav>
- Changing header text color:
- Use
.navbar-light
or.navbar-dark
classes to set the header's text color based on the background color. - For custom text colors, use CSS to target the navigation items:
- Use
Example: Changing header text color using CSS
.navbar-nav .nav-link {
color: #333;
}
- Changing header font:
- To change the header's font, target the relevant elements and apply the desired font styles:
Example: Changing header font using CSS
.navbar-brand, .navbar-nav .nav-link {
font-family: 'Montserrat', sans-serif;
font-weight: 600;
}
- Adjusting header spacing:
- Bootstrap's
.navbar
class applies default padding to the header. To adjust the spacing, use CSS to modify the padding:
- Bootstrap's
Example: Adjusting header spacing using CSS
.navbar {
padding: 1rem 1.5rem;
}
- Adjust the spacing between navigation items by targeting the
.nav-link
class:
Example: Adjusting navigation item spacing using CSS
.navbar-nav .nav-link {
padding: 0.5rem 1rem;
}
Place your custom CSS code after the Bootstrap CSS file to make sure your styles override the default Bootstrap styles.
Responsive Headers
Bootstrap headers adapt to different screen sizes and devices. This provides a good user experience across desktop and mobile platforms.
Bootstrap headers use the .navbar-expand-*
classes to determine at which breakpoint the header should collapse into a mobile-friendly version.
Header Example
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<!-- Header content -->
</nav>
When the header collapses, the navigation items are hidden and replaced by a toggle button. Clicking the toggle button shows the navigation items in a vertical layout.
Example: Adjusting Logo Size
.navbar-brand img {
height: 30px; /* Adjust this value based on your logo size */
}
@media (max-width: 991px) {
.navbar-brand img {
height: 24px; /* Reduce logo size on smaller screens */
}
}
Tip: Use Scrollable Mobile Menu
If your header has a lot of navigation items, consider using a scrollable mobile menu to avoid a long list of items.
Example: Scrollable Mobile Menu
@media (max-width: 991px) {
.navbar-collapse {
max-height: 300px; /* Adjust this value based on your needs */
overflow-y: auto;
}
}