The vast majority of websites have at least some sort of navbar (navigation bar). Magic comes with some simple but powerful tools to build one. Here's an example:
Looks familiar? Of course - it's on the top of every page within this website!
To build a simple navbar, use the following code:
<nav class="default">
<ul class="nav-left">
<li><a href="#" class="nav-brand-text">My Website</a></li>
</ul>
<ul class="nav-right">
<li class="nav-item"><a href="#">Home</a></li>
<li class="nav-item"><a href="#">About</a></li>
<li class="nav-item"><a href="#">Contact</a></li>
<li class="nav-item"><a href="#">Pricing</a></li>
</ul>
</nav>
On the first line, replace the default class name with any standard Magic colour (all colours (including dark and light) except darkens and lightens). This will make several colours change automatically.
As you saw in the example code above, a navbar has two main sections: nav-left and nav-right. Below is an explanation of their uses and components.
This section contains items that appear on the left side of the navbar. This typically contains two items.
One of the most important parts of a website is the 'brand' within the navbar. In Magic, this can either be text or an image. It is defined as a list item within the nav-left unordered list. To make a textual navbar brand, use the following markup inside your navbar:
<ul class="nav-left">
<li><a href="#" class="nav-brand-text">My Brand</a></li>
</ul>
Most websites, however, would want to add an image as their icon. This can be done with the following markup:
The other item found in the nav-left section is the mobile 'hamburger' menu button. This is a button that opens a mobile navigation menu, and is only visible on mobile and tablet devices. The following markup adds the button (required FontAwesome):
<ul class="nav-left">
<li><a href="/magic" class="nav-brand-text">Magic.CSS</a></li>
<li class="nav-mobile-open" onClick="openNav()"><i class="fas fa-bars"></i></li>
</ul>
The nav-right section contains links within an unordered list. It comes directly after the nav-right section in your HTML. To create a simple set of links, use the following markup:
<ul class="nav-right">
<li class="nav-item"><a href="#">Home</a></li>
<li class="nav-item"><a href="#">About</a></li>
<li class="nav-item"><a href="#">Contact</a></li>
<li class="nav-item"><a href="#">Pricing</a></li>
</ul>
To make a navbar link appear 'active', just add the active class to the nav-item.
Your navbar markup must be the first thing within the body of your website, outside of any container. For example:
<body>
<nav class="default">
<ul class="nav-left">
<li><a href="#" class="nav-brand-text">My Website</a></li>
</ul>
<ul class="nav-right">
<li class="nav-item"><a href="#">Home</a></li>
<li class="nav-item"><a href="#">About</a></li>
<li class="nav-item"><a href="#">Contact</a></li>
<li class="nav-item"><a href="#">Pricing</a></li>
</ul>
</nav>
<div class="container">
...
Magic also has a variety of tools for making your navbar work well on tablet and mobile devices. There is a minimal amount of JS involved, as well as some additional markup. Firstly, add something like the following, before the main navbar:
<div id="nav-mobile" class="default"> <!--Replace default with any Magic colour-->
<a href="javascript:void(0)" class="close" onClick="closeNav()">×</a>
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
<a href="#">Pricing</a>
</div>
Make sure that your main navbar contains something like <li class="nav-mobile-open" onClick="openNav()"><i class="fas fa-bars"></i></li>
, with which mobile and tablet users can open your navbar.
Next, add this to your page head:
<script>function openNav(){document.getElementById("nav-mobile").style.width = "100%";}function closeNav(){document.getElementById("nav-mobile").style.width = "0";}</script>
That's it! You should have a functioning mobile navbar! This page also has one. If you are using a desktop device, you can test it out with the button below:
Open navbarYou can also make the mobile navbar 'slide' rather than appear, by adding the animate class to the div:
Open animated navbarTo add a dropdown to your navbar, see the Dropdown docs. Here's a preview: