Creating a complex banner area

Let's start from the top and create our complex banner area with the following features:

  • A site logo positioned above the navbar for desktops and larger viewports
  • A navbar with many menu items, including dropdowns
  • A utility navigation area
  • A login form with username and password
  • An option to register

Here is the mockup of our desired end goal on a desktop-width viewport:

Creating a complex banner area

On a narrow viewport, it will adjust to this:

Creating a complex banner area

We'll start by working on a new arrangement for our top logo.

Placing a logo above the navbar

In this new design, we need a logo in two spots, for two contexts:

  • For desktop and widescreen viewports, we want the logo to display above the navbar
  • For tablet and phone viewports, we want the logo to display within the responsive navbar

Thanks to Bootstrap's responsive utility classes, we can do both! Here's how:

  1. Open index.html in your editor.
  2. From the navbar, copy the navbar-brand link and image. It looks like this:
    <a class="navbar-brand" href="EB9781782164524_3.html"><img src="img/logo.png" alt="Bootstrappin'" width="120"></a>
  3. Paste a copy of it up above, just after the <header role="banner"> tag and before <nav role="navigation" class="navbar navbar-default">.
  4. Wrap the logo with <div class="container">...</div> to constrain it within Bootstrap's centered grid space.
  5. Edit the class on your new logo link, so that it reads banner-brand rather than navbar-brand. And let's change the image width attribute to 180.

Note

Recall that our original logo image is large, about 900px wide. We've resized it to 120px wide via the width attribute (we could alternatively use CSS rules) in order to pack its pixels tighter for retina screens.

The resulting code should look like this:

<header role="banner">
  <div class="container">
    <a class="banner-brand" href="EB9781782164524_3.html"><img src="img/logo.png" alt="Bootstrappin'" width="180"></a>
  </div><!-- /.container -->
  <nav role="navigation" class="navbar navbar-default">

Save the changes and refresh the page in your browser. You should see the new copy of the logo above the navbar.

Placing a logo above the navbar

Now let's adjust our logos so that they are displayed only when needed.

In _variables.less, we need to double-check the value of the @grid-float-breakpoint variable. You'll find it by searching for the variable name. This value is set as follows in the default less/bootstrap/variables.less file:

// Point at which the navbar stops collapsing
@grid-float-breakpoint:     @screen-sm-min; 

This variable determines the point at which the navbar collapses for narrower viewports and expands for wider viewports. In our case, given the complexity of our navigation, we need to ensure that the navbar collapses at the next, larger breakpoint. Therefore, we need to ensure our variable is set at the @screen-md-min breakpoint. If you began with the 04_Code_BEGIN files, you should see this already set as follows. (If not, you'll need to update it.)

// Point at which the navbar stops collapsing
@grid-float-breakpoint:     @screen-md-min;

With this variable set appropriately, we now want the banner-brand class to display for medium and large viewports only and navbar-brand to display only for small and extra-small viewports. Bootstrap provides a set of helpful responsive utility classes to address just this need. You can see the documentation on these classes at http://getbootstrap.com/css/#responsive-utilities.

Let's put them to use for our purposes:

  1. Add the class visible-md visible-lg to the banner-brand class:
    <a class="banner-brand visible-md visible-lg" href="EB9781782164524_3.html"><img src="img/logo.png" alt="Bootstrappin'" width="180"></a>
  2. Add the class visible-xs visible-sm to the navbar-brand class:
    <a class="navbar-brand visible-xs visible-sm" href="EB9781782164524_3.html"><img src="img/logo.png" alt="Bootstrappin'" width="120"></a>

Save the changes and refresh the page, and you should see these results! In medium and large viewports, only the banner-brand class will appear:

Placing a logo above the navbar

In small and extra-small viewports, only the navbar-brand will appear:

Placing a logo above the navbar

Ah, the beauty of Bootstrap!

Note

If you are concerned about having both sets of tags for these alternately hidden logo images cluttering up your markup, it is possible to get the same result with a bit of JavaScript work to swap out the elements as needed.

An advantage of the current approach, however, is that it does not rely upon JavaScript. It doesn't slow page loading times either, since we are using the same image in both places and require no new HTTP requests. All in all, our current approach is a credible, defensible solution; in addition to being easy to implement.

Now, let's make some adjustments to our navbar.

Reviewing and checking navbar dropdown items

The navbar, with its seven items and submenus, reflects the needs of a large complex website.

The markup for the dropdown menus is taken directly from the Bootstrap navbar documentation at http://getbootstrap.com/components/#navbar.

If you look at our resulting markup, you'll notice these special classes and attributes:

  • class="dropdown" on the parent li
  • class="dropdown-toggle" on the link
  • attribute="data-toggle" also on the link
  • class="dropdown-menu" on the submenu ul element

Here is the resulting markup:

<li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Shoes <b class="caret"></b></a>
    <ul class="dropdown-menu">
      <li><a href="#">Action</a></li>
      <li><a href="#">Another action</a></li>
      <li><a href="#">Something else here</a></li>
      ...
    </ul>
</li>

Also note the special tag and class used to display a small dropdown indicator: <b class="caret"></b>. (You'll see the CSS used to create this indicator in less/bootstrap/dropdowns.less.)

Tip

If you happen to be making use of files from a previous project rather than this chapter's starter files (04_Code_BEGIN), you may need to double-check your import lines in _main.less to ensure that it imports bootstrap/dropdowns.less. Also double-check plugins.js to ensure that it includes the plugin bootstrap-dropdown.js.

With the LESS, JavaScript, and markup in place, our navbar and its dropdowns should presently look and work as shown in the following screenshot. (Note that Bootstrap dropdowns respond on click.)

Reviewing and checking navbar dropdown items

Now that we're familiar with the markup structure and have ensured everything's working as it should, let's move the All Departments menu to the right-hand end of the navbar, setting it apart from the others.

To do this, we need to nest this list item within its own unordered list as follows:

  1. Before the All Departments list item, close the ul tag for ul class="nav", which surrounds all previous menu items.
  2. Start a new ul tag with the classes nav and navbar-nav before the All Departments menu item. Once this opening tag is added, it will nest this list item in the standard structure for navigation menus.
  3. In addition to the classes nav and navbar-nav, add a third class, pull-right, which is a convenient Bootstrap utility class, to float an element to the right.

The newly added lines are highlighted in the following snippet—after which I'll include the original list item and link in context:

</ul>
<ul class="nav navbar-nav pull-right">
  <li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown">All Departments <b class="caret"></b></a>

Save the changes and refresh the page, and you should see the All Departments drop-down menu item float to the right-hand end of the navbar as follows:

Reviewing and checking navbar dropdown items

So far so good! Now, let's add our utility navigation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset