Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS Framework Basics Prototyping with Bootstrap Building a Carousel

Waqar Ashraf
Waqar Ashraf
3,743 Points

Responsive nav bar

<div class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span>

    </button>
    <a href="#" class="navbar-brand text-muted"> Ribbit</a>
    <div class="navbar-collapse collapse" >
        <ul class="nav navbar-nav navbar-right ">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#">About Ribbit</a></li>
            <li class="dropdown " ><a data-toggle="dropdown"  data-target="#">Treehouse <b class="caret"></b></a>
                <ul class="dropdown-menu">
                    <li><a href="#">About Treehouse</a></li>
                    <li><a href="#">Video Library</a></li>
                    <li><a href="#">Learning Adventures</a></li>
                    <li class="divider"></li>
                    <li><a href="#">Prices &amp; Pricing  </a></li>
                </ul>
            </li>

        </ul>

    </div>
    <div>

    </div>




</div>

</div>

Can some one please explain me the code below?

data-toggle="collapse" data-target=".navbar-collapse"

2 Answers

Hi Waqar,

I'll try my best to explain the voodoo.

<button ... data-toggle="collapse"></button>

This, essentially, toggles an element being shown or hidden on it's data-target.

<button ... data-target="#navMenu"></button>

...

<div ... id="navMenu"></div>

The button will target the element with an id of navMenu. If you watch the element in the browser's developer tools, you'll see the div's class changing from collapse to collapsing to collapse in. The pairing of collapse with in forces the element to be displayed.

<div ... class="navbar-collapse"></div>

On it's own, it's just some styling rules - only when it gets paired with the class collapse does it become special.

<div ... class="navbar-collapse collapse"></div>

At screen widths 768px and larger, this element gets display: block !important making it visible until the screen reaches what Bootstrap defines as small. Here is a typical example similar to what can be found on Bootstrap's website.

<div class="navbar-collapse collapse" id="navMenu">
    <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Item1</a></li>
        <li><a href="#">Item2</a></li>
        <li><a href="#">Item3</a></li>
    </ul>
</div>

The id is a value I chose and is targeted by the data-target attribute on the button that becomes visible when the screen is (max width: 767px). The button starts off hidden because of the class navbar-toggle. All together, here is a simple fixed top navigation bar.

<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navMenu">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a href="#" class="navbar-brand">Brand</a>
        </div>

        <div class="navbar-collapse collapse" id="navMenu">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">Item1</a></li>
                <li><a href="#">Item2</a></li>
                <li><a href="#">Item3</a></li>
            </ul>
        </div>
    </div>
</nav>

The documentation on Bootstraps website leaves a lot to be desired for understanding what is going on under the hood; it's only just enough to get you using the framework, but to really know 'what is collapse really doing?` you need to open up the source file and do a search for the class you want to know more about (in both the css and js files).

Good luck!

Shaker Advertising
Shaker Advertising
5,395 Points

They are data-attributes.

To read up on how you can utilize data-attributes take a look at this: https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes