Navigation Bar With Dropdowns

1

How to start working with us.

Geolance is a marketplace for remote freelancers who are looking for freelance work from clients around the world.

2

Create an account.

Simply sign up on our website and get started finding the perfect project or posting your own request!

3

Fill in the forms with information about you.

Let us know what type of professional you're looking for, your budget, deadline, and any other requirements you may have!

4

Choose a professional or post your own request.

Browse through our online directory of professionals and find someone who matches your needs perfectly, or post your own request if you don't see anything that fits!

CSS becomes more powerful with features such as CSS grids and custom properties or CSS variables. These solutions focus on not just making websites more attractive, but more accessible, as well, making styling easier and more convenient.

Accessibility is not just about making

Your website is accessible for people with disabilities although it is incredibly important. A more subtle benefit of accessibility is that it makes your content more legible, more understandable. If you use Unicode characters to get professional-looking typography on the web, your users need a font pack to be able to read them properly - this is usually something you're not aware of, but it has a huge effect on your website's usability.

HTML & CSS are very powerful technologies that define the way content is displayed to users. To create websites for our audience, we have to know how these technologies work and how they behave in different browser environments. With some basic understanding of HTML and CSS (and JavaScript and PHP, for that matter), we can create these solutions.

Have you ever wanted to create a dropdown menu?

With the help of this tutorial, you'll learn how to build one from scratch. You'll also find out what each of the different classes available within Bootstrap is for and when they should be used. We’ll also show you some examples so that you can see exactly what your finished product will look like!

So let's get started with learning how to create a simple actual dropdown menu using Bootstrap. By the end of this article, we hope that you'll have all the knowledge necessary to start creating your menus today! Let's begin by looking at some example code that shows an unordered list being transformed into a navigation bar containing two buttons - one for showing and hiding its associated dropdown links. The first button is styled as active (meaning it has been pressed) while the second is not (meaning it hasn't). This gives us our basic starting point for building our menus later on in this tutorial. Here's what it looks like right now... And here’s another example where we've added more buttons inside our list items but haven't yet styled them or their associated dropdowns... Now let’s take a look at how these lists are built up using HTML markup before moving on to styling them with CSS rules afterward… As mentioned earlier, there are several different ways in which we can create our dropdowns depending

Navigation Bar With Dropdowns appeared first on Web Code Geeks

First, I would like to start with a simple image-based menu that will look identical across browsers and provide an accessible way for users to navigate through your site.

We have our main container, which is the .main-navigation element in our HTML markup. It has five children elements: a link, the image covering the whole area of our menu bar, two anchors, and another link.

The .menu-item element is the child to the previous elements and it has a mixture of text and images in its content. To make sure that this element covers all areas when we add something inside it, we set its width property to 100%. The properties defined for these four children elements provide them with their default dimensions so they look appropriate by default. Here's how our HTML looks:

In CSS, we have different rules for each of these elements depending on their content display property which is specified in our HTML markup.

For example, if you want to use an imagemap inside a certain element you have two choices: either use the object element (e.g., <object> ) or use the img element and define a hotspot inside it using CSS:

You can see that we target the .menu-item class and set an imagemap as its background image.

We apply this to all our menu items, so if you want to change something later on, you'll probably be adding some extra code here which might impact your menu behavior.

Another example is linked text. Let's suppose we have another menu item with the following HTML markup:

Here we have two anchors, both of them defined as children to one .menu-item element. One of these has a different href attribute value which will lead us to a different page when clicked by the user.

To make sure that the link to the different page looks like a regular text, we set its color property (and opacity if you want it to be more transparent) and define where this anchor should link to using its href attribute value.

The normal state for this element is when it has no class applied to it by default so, in our CSS code, we have defined two different states: one with no class assigned and another one with the .open class assigned. The latter will apply when JavaScript is used or you click on this menu item yourself. We'll see how JavaScript comes into play next!

The last example I would like to show you in regards to navigation elements is the text being displayed inside anchors. If you need your menu items to have descriptive text you can, of course, type a string in between the anchor tags yourself.

The better way would be to use an image as a menu item covering and apply some text to it using CSS. You can create this menu item by either providing a link or not. If you do provide one but also want your text to show up when no link is clicked, then just add another .menu-item element inside your list of children elements with no anchor defined. This will automatically create a hyperlink for the user without them having to click on any specific area of your menu bar! Here's what we mean:

This is what the CSS code looks like:

We target our first child element and set its default color to grey. We set its padding to 5px and then define that when this particular class is applied through JavaScript, the text should be dark blue (the value will depend on what you defined as your colors in your CSS file).

We also target the .menu-item element and we give it a different background color depending on if we use or not an anchor inside our list of children elements. If we do have one, we'll apply the style defined for our menu items with links and if not, we just set it to white.

One more thing: you can also add slices to your image so it looks like there's only one item displayed across all browsers! You just need to define them in your HTML markup:

This way, your image will be broken down into nine parts and you'll define the slice position using CSS. If you take a look at our menu bar example, you can see how we use this technique for a better-looking bar.

The whole idea behind this is to make sure that no matter what kind of device your website visitors are using (desktop or mobile phone), they all have access to the same content. This means that if your navigation has four items in it, no matter if the user views them on an iPhone or a 47" screen, they should always see those four items displayed across their browser! This makes life easier for them as well as for you since there's less code to write and maintain!

As you saw, the code is simple but can be difficult to understand unless you know what you're doing. If it doesn't look like something you could manage, there are ways of achieving this result without ever having to write any JavaScript by using menus that can be displayed on different sections of your website. Check them out here

Menu alignment

Next important aspect of your website design. What you want is to make sure that when people click on different items inside your menu bar, they are brought to the page you want them to visit (so it's good practice not to forget about adding an href attribute when creating links in between anchor tags).

You might even notice that some websites don't align their elements in the same way across different browsers; this usually happens when they try and resize content depending on what width layout they're using (for example, if they're using a fixed-width layout with one setting). If you'd like your menu bar to be resizable along with your text, then give Bootstrap a try! Once again, its fluidity makes spending time on creating responsive websites easier for you!

Now, let's talk about how navigation bar menus are created in HTML. We can go on and on with code samples but it will be better if we show you the menu items having visible links inside them; this way you'll be able to understand what's going on in the background when it comes down to structuring your lists of children elements with anchor tags.

This one is simple - when an item is clicked, its href attribute is called which means that if one of our pieces of text was "www.google.com", then the user would automatically be brought to Google's website (if they've added their bookmark in their browser). If you want your list element to display different styles based on whether an item is a link or not, you should use the <a> tag instead of the <li> one since it will also pass on some of its properties to your child element.

Create dropdown menus using Bootstrap

We've talked about creating lists of elements with links inside them - now it's time to take it up a notch and learn how to create awesome-looking dropdowns for your navigation bar (and any other place you might need to use them).

The possibilities are endless; all you have to do is make sure that each link points to the website or web page you want people who visit your site to see. What's even more awesome is that Bootstrap comes with built-in styles for dropdowns which means that if you know just slightly about CSS, then this should be no problem for you!

First, let's define what we mean by "dropdown" menus: their pieces of content are displayed inline by default until someone hovers their mouse over the parent element, which will cause the hoverable dropdown menu to expand vertically. The contents of this element are displayed in a box that appears right next to it when you hover your mouse over the list item.

You can use Bootstrap's classes for creating horizontal or inline menus without ever having to write any Javascript code! All you have to do is place your links inside their corresponding elements and set the kind of navigation bar you want them to be used with (if at all).

How easy was that? We'll tell you - VERY easy! Bootstrap makes it very simple for anyone who's never worked with HTML/CSS before since all they need to do is learn how each attribute works within their tags. It gets even easier once you're used to the code since all you'll need is your imagination.

Wouldn't it be awesome if there were pre-designed Bootstrap templates for your future projects? We thought so - which is why we've created thousands of free Bootstrap themes for everyone to enjoy! All you have to do is browse through them, pick one that fits your needs best and take it for a test drive by downloading it. Once you're done, show us what you made! Don't forget about sharing with us on social media :)

What's important in this page design example is the way the dropdown menus appear when they hover over; their background color changes as well as their child elements' colors (links included). This looks nice and is a great way to present your company's services or products in a clear, easy-to-understand manner.

We hope that this article has given you a good idea of how navigation bar menus work along with some examples to get you started on building one yourself. This doesn't end here - there's much more waiting for you in the world of Bootstrap!

If you'd like to read even more about what Bootstrap can do for your website, then take a look at our other articles which show its amazing power. Remember - no matter what kind of project you have in mind, there's bound to be something useful coming from us soon so stay tuned!

Create the right aligned dropdown menus just like the image above using Bootstrap and our simple tutorial.

There are several different ways in which you can create your drop-down menu which is why we'll start by showing you what each of these methods looks like before explaining how they work. You'll learn about the various kind of classes available within Bootstrap for creating navigation bars with dropdowns, including what their differences are between them.

We're certain that there's plenty more to this than just laying out the HTML code - but don't worry since we've explained everything else that you should know to be able to successfully use these features! Once you're finished reading, all that's left is for you to download the source files along with step-by-step guidelines that will help you implement the JavaScript plugin in no time.

How To Create Navigation Menus With Dropdowns Using Bootstrap

You'll need to start by including two files: jQuery and Bootstrap's Javascript file. These should be added right before your closing </body> tag so that PHP can pick them up and use them for code execution.

$(document).ready(function(){ // Your Javascript goes here });

Bootstrap does a great job at keeping its stylesheets very organized which is why we're going to take advantage of this when creating our dropdown menu. First, let's add one class name - "navbar" - within the <nav></nav> tags in order to define where our dropdown will appear.

<nav class="navbar"> <ul class="dropdown-menu"> <li><a href="#">Nav Item 1</a></li> <li><a href="#">Nav Item 2</a></li> </ul> </nav>

After this, we'll add one more class name called "dropdown" along with the attribute "data-toggle" that Bootstrap provides since it tells PHP to execute some Javascript code whenever someone hovers over the aforementioned dropdown. This is where the menu shows up and disappears when you hover on/off of it later on.

<nav class="navbar"> <ul class="dropdown-menu" data-toggle="dropdown"> <li><a href="#">Nav Item 1</a></li> <li><a href="#">Nav Item 2</a></li> </ul> </nav>

The next step in creating a navigation bar is to add another class name called "btn-default" which Bootstrap includes. This will style the dropdown button that appears when your visitors want to hover over something - imagine it as a box that pops up from the top of the screen with some text inside.

<nav class="navbar"> <ul class="dropdown-menu" data-toggle="dropdown"> <li><a href="#"><span class="glyphicon glyphicon-chevron-down"></span> Nav Item 1</a></li> <li><a href="#"><span class="glyphicon glyphicon-chevron-down"></span> Nav Item 2</a></li> </ul> <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"> Dropdown <span class="caret"></span></button> </nav>

Now, we'll write a Javascript file called "bootstrap.js" which contains all of the code that you'll need to get your pulldown menu working properly. You can see below that, each time a visitor hovers on top of one of the links from our navigation bar, this will trigger some Javascript code and make the dropdown appear.

$(document).ready(function(){ $('nav .dropdown-toggle').click(function() { $('.dropdown-menu').show(); }); });

If you want to learn how to create these dropdowns using HTML markup, then go here instead. We'll be covering the Javascript version of this technique today.

Bootstrap also offers us another way in which we can make our pulldown menus appear - let's take a look at what other options are available for us!

How To Use Bootstrap Dropdowns With An Anchor Link Instead Of A Button Tag/Input Field Combination

When creating your navigation menu, you're not required to use Javascript nor an <input type="submit"/> button. Sure, doing this is easier and will produce the same results but you're free to come up with your solution too.

Anchor Link Or Button Tag/Input Field Combination

Bootstrap also allows us to use a combination of <a> and an <input type="submit"/> button so that we can manually trigger our pulldowns instead of using Javascript. Remember: you'll need to include both jQuery and Bootstrap's Javascript file before your closing </body> tag so that PHP can properly execute these scripts as seen below. In addition, you'll also need to add a class called "dropdown-menu" which was the code used in all of those previous examples from above! Here's what that would look like ...

<ul class="dropdown-menu" data-toggle="dropdown"> <li><a href="#"><span class="glyphicon glyphicon-chevron-down"></span> Nav Item 1</a></li> <li><a href="#"><span class="glyphicon glyphicon-chevron-down"></span> Nav Item 2</a></li> </ul>

Add Dropdowns via Data Attributes

You can also use more data attributes with Bootstrap's dropdowns to make even more magic happen! We can accomplish this by using the "data-toggle" attribute on our anchor links.

For example, if you place a class called "caret" within your <a> tag then Bootstrap will automatically add some CSS code that animates it for you - this is what gives us that nice animation effect whenever our menu pops up. Here's how it looks ...

<ul class="dropdown-menu" data-toggle="dropdown"> <li><a href="#"><span class="glyphicon glyphicon-chevron-down caret"></span> Nav Item 1</a></li> <li><a href="#"><span class="glyphicon glyphicon-chevron-down caret"></span> Nav Item 2</a></li> </ul>

In addition, you can also use more attributes to change the model of how your menu behaves. For instance, if you don't want Bootstrap's dropdowns to automatically appear then you can use "data-toggle" with a value of "none"! Here's an example ...

<ul class="dropdown-menu" data-toggle="none"> <li><a href="#"><span class="glyphicon glyphicon-chevron-down caret"></span> Nav Item 1</a></li> <li><a href="#"><span class="glyphicon glyphicon-chevron-down caret"></span> Nav Item 2</a></li> </ul>

Another attribute that you can use is called "data-target" which allows us to tell Bootstrap what the id is of the HTML tag that we want to apply our dropdowns too. Here's an example of how this looks ...

<ul class="dropdown-menu" data-toggle="none"> <li><a href="#"><span class="glyphicon glyphicon-chevron-down caret"></span> Nav Item 1</a></li> <li><input type="text" value="" placeholder="First Name" data-toggle="dropdown"><span class="glyphicon glyphicon-chevron-down caret"></span> Nav Item 2</li> <li><input type="text" value="" placeholder="Last Name" data-toggle="dropdown"><span class="glyphicon glyphicon-chevron-down caret"></span> Nav Item 3</li> </ul>

Since Bootstrap's dropdowns are so flexible, it makes them very easy to style. If you want to check out some code examples of how to use the "data attributes" above then I recommend checking out this Github repo which gives us a ton of free templates! The HTML versions are included in that distribution file too if you're looking for more templates!

How To Add Dropdowns Using Only CSS And No Javascript

Since Bootstrap's dropdowns are relatively easy to use, then it makes sense that we can accomplish this without using Javascript. All you have to do is add a class called "dropdown-toggle" onto your link/button tag which will trigger the functionality when clicked upon. Here's an example of how to implement these in HTML ...

<ul class="dropdown-menu" class="dropdown-menu"> <li><a href="#"><span class="glyphicon glyphicon-chevron-down"></span> Nav Item 1</a></li> <li><a href="#"><span class="glyphicon glyphicon-chevron-down"></span> Nav Item 2</a></li> </ul>

Then, we can add our CSS to target it. We'll use the ": target" selector along with some of Bootstrap's pseudoclasses and then we'll apply a nice box shadow around it so that it blends into the background nicely - this is just a bonus! Here's how that looks ...

ul.dropdown-menu:target { display: block; position: relative; z-index: 9999; padding-bottom: 1px; margin-bottom: 5px; line-height: 0.8; color:#fff !important; } ul.dropdown-menu li a span.glyphicon { color:#fff !important;} ul.dropdown-menu a span.caret { border-top-color:#fff !important;} ul.dropdown-menu li a:hover, ul.dropdown-menu li a:focus { color:#666; text-decoration: none; background-color: #f9f9f9;}

We'll also add some CSS for padding and line height to make space for our dropdowns - this is just a bonus so that we can see our menus more clearly! This will result in something like the following.

Dark dropdowns

Using Javascript, Of course, we can take this one step further and use javascript to mimic the dropdown behavior. After all, if you're using Bootstrap then it's likely that you're doing client-side scripting too! The main benefit of using Javascript is that this will help us implement some other cool Bootstrap UI components like collapsible (accordions). If you want to see how to do this then check out my video here.

Menu

Items will be hidden unless they are active If you want your dropdowns to only show up if a certain condition is met then we can do that with Bootstrap. There are a couple of ways to do this, but I think the most common way is to use jQuery. The advantage of this method over CSS is that it will also allow us to implement accordions and other advanced UI components too! To see an example of how this works then check out my video here.

Dropdowns in Button Groups

The Bootstrap button groups are great for one-click access because they give you a lot of control over the spacing. It's even better because it saves us from having to write JavaScript or use images all the time! You can achieve this by using two types of HTML elements - the "div" element and the <button> element.

Always show menu item Here, I'm showing how you can always have your dropdown open for users who don't know about it so that they can discover what's inside too. This is done through some extra CSS which uses an attribute selector to target items with data-toggle="dropdown".

Add Headers to Dropdown Items

We can also add headers and dividers to the items inside our dropdowns by using some extra HTML tags. We'll use "ul" and then we'll wrap each item into a <li> tag along with an additional <div> that will help us format the content for this list. To do this, we'll use Bootstrap's classes such as .text-muted and .list-inline so that they look nice against the page!

Size Dropdown Menu

Another thing we can do with dropdowns is made them bigger or smaller. We can customize the positioning and sizing of these items by using Bootstrap's CSS. This uses a combination class of.pull-right and .col-xs-8 for example. An advantage of using col classes like this is that they will automatically resize to fit on smaller screens!

Vertical Dropdown Menu We can also display vertical dropdown menus if we want by simply adding an anchor command onto our HTML - "data-toggle=" dropdown". This allows us to use the same HTML structure as before so no new code needs to be written! Check out my video below where I show this in action ...

Dropdown Actions Definition

This is another advantage of using Bootstrap's JavaScript to implement these menus. Here, we can show some actions for the user that are related to the content inside the dropdown menu - for example, if it were an address book then you might have buttons like "call" or "email". This uses Bootstrap's classes.btn-default and .caret . By adding this class onto our button element (or anchor command), the element will automatically receive all these styles without having to write any extra code ourselves!

Dropdowns within buttons

Bootstrap allows us to use the same HTML structure as our dropdowns to create buttons that open up a menu. The only difference is that you will need to wrap it within an element with class="btn", like so,

Open dropdown menu for this button This can be done using JavaScript by following the previous examples or if you don't want these behaviors then you can simply use CSS to do it! This uses some classes from Bootstrap's btn module - .btn-default and .dropup are good ones to start with here. The advantage of doing this is that it helps keep our code DRY (don't repeat yourself) where possible! I've also made a video on how you can do this too...

DROPDOWN MENU

Menu 1 Menu 2 Menu 3

This is an example of using this method to get Bootstrap dropdown menus working. These are styled normally and not within the context of a button like in some of my previous examples. I'm also using icons on these items (font awesome) just so that it looks nice! Changing your font family or font size will allow you to change the style here too. So, if you want one of these without icons then just go ahead and remove them - it's as easy as that!

Menu 4 Menu 5 Menu 6

These last three menus are really interesting because they use Bootstrap's cards which allows us to show our UI components in different ways around the page. This uses classes like.card-header , .card-body and .card-footer to allow us to do this. You can also use these cards outside of dropdowns too - for example, you may want to list an address book in a grid of names rather than having them all on one page.

Bootstrap Split Button Dropdowns

Another thing we can do is split buttons, where menu items are shown in two separate groups. This is great for creating a "more information" button - which will reveal more details about the items inside the dropdown menu.

Open drop up Item 1 Menu Content

This uses Bootstrap JavaScript to add classes like .dropup and .dropdown-item so that the layout is right and working properly. The advantage of this method isn't just cosmetic either... Because these menus work with HTML/CSS too then you can do other things that aren't possible using the previous examples such as: positioning them within other components or even customizing how they look using CSS rules! I've made a video on this below which demonstrates how to position these elements within other components on your page...

Dropdowns in navigation menus

As a final example, I want to show you how you can use these dropdown menus within a navigation menu! Most websites out there have a simple list of links that consist of headings and sub-headings. The problem with this is that they aren't very "tappable" - which means users will be less likely to click on them!

Dropdown navigation menu To fix this, we should make each heading link off to somewhere else. One way of doing this would be to create a mega dropdown from the top-level link allowing us access to all the levels underneath it. This is what Bootstrap's JavaScript does for us and all we need to do is add our custom component HTML underneath here.

Open dropdown menu for this button This is a really good example of re-usability as we can use the same HTML inside your main navigation list as well as inside your mega dropdowns! The only difference is that you will need to wrap each link in an element with class="btn", like so,

This uses some classes from Bootstrap's btn module - .btn-default and .dropup are good ones to start with here. The advantage of doing this is that it helps keep our code DRY (don't repeat yourself) where possible! I've also made a video on how you can do this too...

Dropdowns within a navigation bar

These are styled normally and not within the context of a button like in some of my previous examples. I'm also using icons on these items (font awesome) just so that it looks nice! Changing your font family or font size will allow you to change the style here too. So, if you want one of these without icons then just go ahead and remove them - it's as easy as that!

These last three menus are really interesting because they use Bootstrap's cards which allows us to show our UI components in different ways around the page. This uses classes like.card-header , .card-body and .card-footer to allow us to do this. You can also use these cards outside of dropdowns too - for example, you may want to list an address book in a grid of names rather than having them all on one page.

Bootstrap Split Button Dropdowns

Another thing we can do is split buttons, where menu items are shown in two separate groups. This is great for creating a "more information" button - which will reveal more details about the items inside the dropdown menu.

Open drop up Item 1 Menu Content

This uses Bootstrap JavaScript to add classes like .dropup and .dropdown-item so that the layout is right and working properly. The advantage of this method isn't just cosmetic either... Because these menus work with HTML/CSS too then you can do other things that aren't possible using the previous examples such as: positioning them within other components or even customizing how they look using CSS rules! I've made a video on this below which demonstrates how to position these elements within other components on your page...

Item 2 You can also add toggle switches here too, like this example where we need the menu items to expand and collapse. This is done easily with JavaScript by adding an extra attribute called data-toggle=" collapse" inside the <li> element - it's that easy!

Dropup + Toggle Example You can also do more than just toggle switches and dropups. Bootstrap's JavaScript allows you to use all sorts of different HTML/CSS in your menus too — not just list items — so put whatever you want in there and see what happens!

Menu Toggle I've made a video on these menu examples too, which shows how to use things outside of lists for dropdowns.

We can also do split buttons with dropdowns, like this example where the "more information" button is separated into two columns. This is done by adding two extra classes to your HTML - .dropdown-content and another one that you create yourself. Here's an example of how to create our custom class using CSS...

Dropup Button Within Card This makes it easy for us to select each part individually so that we can move them around the page quickly, without having to edit any JavaScript or anything else in our plugin because it lives inside your website. You should note the following though...

You need to have your dropup buttons above any other elements that you want to select with CSS, otherwise, it will be difficult for the browser to determine which element you're using. You can use it! important in your CSS too - just before your class name if you need to force things into place.

Styling Dropdown Items Using Firefox I've found out recently that Firefox doesn't support all of Bootstrap's JavaScript by default - it uses its version which is slightly older than what everyone else is using! This means that some of the responsive features are broken until they update their browser or you enable scroll manually by typing about config in Firefox's address bar and setting dev tools.chrome.enabled to true. Then restart.

We can do many things with drops and split buttons within the box-shadow property, and we've already seen that we can add custom HTML/CSS into our dropdowns too. This is great for positioning them wherever you want... But the majority of your website's visitors will probably be using a mouse to navigate around so it helps if they're close to each other! If this sounds familiar, don't worry - Bootstrap has another class called .dropdown-item-text which will position text next to your dropdown item instead of showing an icon. You can even use icons here too if you want, but I think the text looks better! There are a few other classes in there that let you do more things like .caret for adding a caret (^) next to the text too.

Geolance is an on-demand staffing platform

We're a new kind of staffing platform that simplifies the process for professionals to find work. No more tedious job boards, we've done all the hard work for you.


Geolance is a search engine that combines the power of machine learning with human input to make finding information easier.

© Copyright 2022 Geolance. All rights reserved.