james
james

Reputation: 5236

Fixed Bootstrap navbar to top of dynamic width DIV - not full width of page

My problem

I am using Bootstrap 3.0 and need to use a fixed navbar, which will stay at the top of a DIV on the left side and NOT extend all the way 100% of the width of the window (like I usually see Bootstrap's example do). I have some very simple code below, using ASP.NET MVC 4 which will explain the RenderSection areas.

I have included an image which shows what I am trying to do. Currently the navbar is only extending about 75% of the left side container, not the full 100% of that left side container like I want it to.

Like I said above, I want the navbar to be fixed so when you scroll it stays in one spot within that left DIV.

Any tips/hints would be greatly appreciated! Thanks!

I also saw this question (Navbar fixed to top but not full-width) but I do not want to have to dynamically maintain this navbar with a fixed pixel width in media queries. I'd prefer it to fill out the entire left side DIV that contains it.

image which outline my issue(s) (https://i.sstatic.net/FTNau.jpg)

My code

<div class="container">
    <div class="row">
        <!--LEFT SIDE-->
        <div class="col-md-9">
            <div data-spy="affix" data-offset-top="0">
            <nav class="navbar navbar-default" role="navigation">
                <div class="collapse navbar-collapse navbar-ex1-collapse">
                    <ul class="nav navbar-nav">
                        <li class="active"><a href="#">Link</a></li>
                        <li><a href="#">Link</a></li>
                        <li class="dropdown">
                            <a data-toggle="dropdown" class="dropdown-toggle" href="#">Dropdown <b class="caret"></b></a>
                            <ul role="menu" class="dropdown-menu">
                                <li><a href="#">Action</a></li>
                                <li><a href="#">Another action</a></li>
                                <li><a href="#">Something else here</a></li>
                                <li><a href="#">Separated link</a></li>
                                <li><a href="#">One more separated link</a></li>
                            </ul>
                        </li>
                    </ul>
                    <form role="search" class="navbar-form navbar-left">
                        <div class="form-group">
                            <input type="text" placeholder="Search" class="form-control">
                        </div>
                        <button class="btn btn-default" type="submit">Submit</button>
                    </form>
                </div>
            </nav>    
            </div> <!--end data-spy=affix-->

            <!--the LeftSide below just contains basic placeholder text in a <p> tag-->
            @RenderSection("LeftSide", true)

        </div>
        <!--RIGHT SIDE-->
        <div class="col-md-3">

            <!--the RightSide below just contains basic placeholder text in a <p> tag-->
            @RenderSection("RightSide", true)

        </div>
    </div>
</div>

Partially Solved (update)

I was able to solve my main problem that I listed above by using this bit of jQuery

 $(window).resize(function () {
     var w = $("#leftSide").width();
     $("#navWrapper").css("width", w);
 });

But this does not work within an iFrame.

If anyone has any insights on solving this for use in an iFrame, it'd be greatly appreciated.

Upvotes: 1

Views: 1588

Answers (1)

Mckay Multimedia
Mckay Multimedia

Reputation: 1096

Have you tried placing your navbar and main content area in one column and then your right sidebar in a column by itself? Then add a bit of CSS to correct for the navbar position and adjust for responsiveness.

LINK: http://www.bootply.com/ZJCGxiUD6g

Upvotes: 0

Related Questions